orionis 0.512.0__py3-none-any.whl → 0.514.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ from abc import ABC, abstractmethod
2
+ from datetime import datetime
3
+ from orionis.console.contracts.schedule import ISchedule
4
+
5
+ class IBaseScheduler(ABC):
6
+
7
+ # Pause Global Scheduler at a specific time
8
+ PAUSE_AT: datetime = None
9
+
10
+ # Resume Global Scheduler at a specific time
11
+ RESUME_AT: datetime = None
12
+
13
+ # Finalize Global Scheduler at a specific time
14
+ FINALIZE_AT: datetime = None
15
+
16
+ @abstractmethod
17
+ def tasks(self, schedule: ISchedule):
18
+ """
19
+ Defines and registers scheduled tasks for the application.
20
+
21
+ Parameters
22
+ ----------
23
+ schedule : ISchedule
24
+ The schedule object used to define and register scheduled commands.
25
+ This object provides methods to add tasks to the scheduler.
26
+
27
+ Returns
28
+ -------
29
+ None
30
+ This method does not return any value. It is intended to be overridden
31
+ by subclasses to specify scheduled tasks.
32
+
33
+ Notes
34
+ -----
35
+ This method serves as a contract for subclasses to implement task registration
36
+ logic. Subclasses should use the provided `schedule` object to define and
37
+ register tasks that the scheduler will execute.
38
+ """
39
+ # Abstract method to be implemented by subclasses for task registration
40
+ pass
41
+
42
+ @abstractmethod
43
+ def onStarted(self):
44
+ """
45
+ Called when the scheduler is started.
46
+
47
+ Returns
48
+ -------
49
+ None
50
+ This method does not return any value.
51
+
52
+ Notes
53
+ -----
54
+ Subclasses should override this method to implement custom behavior that
55
+ should occur when the scheduler starts running. This can include initializing
56
+ resources or logging the start event.
57
+ """
58
+ # Abstract method to define behavior when the scheduler starts
59
+ pass
60
+
61
+ @abstractmethod
62
+ def onPaused(self):
63
+ """
64
+ Called when the scheduler is paused.
65
+
66
+ Returns
67
+ -------
68
+ None
69
+ This method does not return any value.
70
+
71
+ Notes
72
+ -----
73
+ Subclasses should override this method to define custom behavior that occurs
74
+ when the scheduler enters a paused state. This can include saving the current
75
+ state or logging the pause event.
76
+ """
77
+ # Abstract method to define behavior when the scheduler is paused
78
+ pass
79
+
80
+ @abstractmethod
81
+ def onResumed(self):
82
+ """
83
+ Called when the scheduler is resumed from a paused state.
84
+
85
+ Returns
86
+ -------
87
+ None
88
+ This method does not return any value.
89
+
90
+ Notes
91
+ -----
92
+ Subclasses should override this method to implement any actions that need to
93
+ occur when the scheduler resumes operation. This can include restoring the
94
+ state or logging the resume event.
95
+ """
96
+ # Abstract method to define behavior when the scheduler is resumed
97
+ pass
98
+
99
+ @abstractmethod
100
+ def onFinalized(self):
101
+ """
102
+ Called when the scheduler has completed its execution and is being finalized.
103
+
104
+ Returns
105
+ -------
106
+ None
107
+ This method does not return any value.
108
+
109
+ Notes
110
+ -----
111
+ Subclasses can override this method to perform any necessary cleanup or
112
+ finalization tasks, such as releasing resources or logging the finalization
113
+ event.
114
+ """
115
+ # Abstract method to define behavior when the scheduler is finalized
116
+ pass
117
+
118
+ @abstractmethod
119
+ def onError(self, error: Exception):
120
+ """
121
+ Handles errors that occur within the scheduler.
122
+
123
+ Parameters
124
+ ----------
125
+ error : Exception
126
+ The exception instance representing the error that occurred.
127
+
128
+ Returns
129
+ -------
130
+ None
131
+ This method does not return any value.
132
+
133
+ Notes
134
+ -----
135
+ Subclasses can override this method to implement custom error handling logic
136
+ for the scheduler. This can include logging the error, notifying stakeholders,
137
+ or attempting to recover from the error.
138
+ """
139
+ # Abstract method to define behavior for handling errors in the scheduler
140
+ pass
@@ -4,7 +4,7 @@ import re
4
4
  from typing import Any, List, Optional
5
5
  from orionis.console.args.argument import CLIArgument
6
6
  from orionis.console.base.command import BaseCommand
7
- from orionis.console.base.contracts.command import IBaseCommand
7
+ from orionis.console.contracts.command import IBaseCommand
8
8
  from orionis.console.contracts.reactor import IReactor
9
9
  from orionis.console.enums.command import Command
10
10
  from orionis.console.exceptions import CLIOrionisValueError
@@ -19,5 +19,4 @@ class JobModified(JobEventData):
19
19
  This class does not return a value; it is used as a data container.
20
20
  """
21
21
 
22
- # This class inherits from JobEventData and does not add additional fields or methods.
23
- # It serves as a specific type of event for job modifications.
22
+ next_run_time: str | None = None
@@ -4,32 +4,37 @@ from datetime import datetime
4
4
  from apscheduler.triggers.cron import CronTrigger
5
5
  from apscheduler.triggers.date import DateTrigger
6
6
  from apscheduler.triggers.interval import IntervalTrigger
7
- from orionis.support.entities.base import BaseEntity
7
+ from orionis.console.contracts.schedule_event_listener import IScheduleEventListener
8
8
 
9
9
  @dataclass(kw_only=True)
10
- class Event(BaseEntity):
10
+ class Event:
11
11
  """
12
12
  Represents an event with scheduling and execution details.
13
13
 
14
14
  Attributes
15
15
  ----------
16
16
  signature : str
17
- The unique identifier or signature of the event.
17
+ The unique identifier or signature of the event, used to distinguish it from other events.
18
18
  args : Optional[List[str]]
19
- A list of arguments associated with the event.
19
+ A list of arguments associated with the event. Defaults to an empty list if not provided.
20
20
  purpose : Optional[str]
21
- A description of the event's purpose.
21
+ A brief description of the event's purpose or intent. Can be None if not specified.
22
22
  random_delay : Optional[int]
23
- An optional random delay (in seconds) before the event is triggered.
23
+ An optional random delay (in seconds) to be applied before the event is triggered. Can be None.
24
24
  start_date : Optional[datetime]
25
- The start date and time for the event.
25
+ The start date and time for the event. If None, the event starts immediately or based on the trigger.
26
26
  end_date : Optional[datetime]
27
- The end date and time for the event.
27
+ The end date and time for the event. If None, the event does not have a defined end time.
28
28
  trigger : Optional[Union[CronTrigger, DateTrigger, IntervalTrigger]]
29
- The trigger mechanism for the event.
29
+ The trigger mechanism for the event, which determines when and how the event is executed.
30
+ Can be a CronTrigger, DateTrigger, or IntervalTrigger. Defaults to None.
31
+ details : Optional[str]
32
+ Additional details or metadata about the event. Can be None if not specified.
33
+ listener : Optional[IScheduleEventListener]
34
+ An optional listener object that implements the IScheduleEventListener interface.
35
+ This listener can handle event-specific logic. Defaults to None.
30
36
  """
31
37
 
32
-
33
38
  # Unique identifier for the event
34
39
  signature: str
35
40
 
@@ -52,4 +57,7 @@ class Event(BaseEntity):
52
57
  trigger: Optional[Union[CronTrigger, DateTrigger, IntervalTrigger]] = None
53
58
 
54
59
  # Optional details about the event
55
- details: Optional[str] = None
60
+ details: Optional[str] = None
61
+
62
+ # Optional listener that implements IScheduleEventListener
63
+ listener: Optional[IScheduleEventListener] = None
@@ -2,7 +2,11 @@ from enum import Enum
2
2
 
3
3
  class ListeningEvent(Enum):
4
4
  """
5
- Enum representing global listener events.
5
+ Enumeration of events related to a scheduler and its jobs.
6
+
7
+ This class defines various events that can occur during the lifecycle of a scheduler
8
+ and its associated jobs. These events can be used to monitor and respond to changes
9
+ in the scheduler's state or the execution of jobs.
6
10
 
7
11
  Attributes
8
12
  ----------
@@ -13,63 +17,47 @@ class ListeningEvent(Enum):
13
17
  SCHEDULER_PAUSED : str
14
18
  Event triggered when the scheduler is paused.
15
19
  SCHEDULER_RESUMED : str
16
- Event triggered when the scheduler resumes.
20
+ Event triggered when the scheduler is resumed.
17
21
  SCHEDULER_ERROR : str
18
22
  Event triggered when the scheduler encounters an error.
19
- SCHEDULER_STARTED : str
20
- Event triggered when the scheduler starts.
21
- SCHEDULER_SHUTDOWN : str
22
- Event triggered when the scheduler shuts down.
23
- SCHEDULER_PAUSED : str
24
- Event triggered when the scheduler is paused.
25
- SCHEDULER_RESUMED : str
26
- Event triggered when the scheduler resumes.
27
- EXECUTOR_ADDED : str
28
- Event triggered when an executor is added.
29
- EXECUTOR_REMOVED : str
30
- Event triggered when an executor is removed.
31
- JOBSTORE_ADDED : str
32
- Event triggered when a job store is added.
33
- JOBSTORE_REMOVED : str
34
- Event triggered when a job store is removed.
35
- ALL_JOBS_REMOVED : str
36
- Event triggered when all jobs are removed.
37
- JOB_ADDED : str
38
- Event triggered when a job is added.
39
- JOB_REMOVED : str
40
- Event triggered when a job is removed.
41
- JOB_MODIFIED : str
42
- Event triggered when a job is modified.
43
- JOB_EXECUTED : str
44
- Event triggered when a job is executed.
45
- JOB_ERROR : str
46
- Event triggered when a job encounters an error.
47
- JOB_MISSED : str
23
+ JOB_BEFORE : str
24
+ Event triggered before a job is executed.
25
+ JOB_AFTER : str
26
+ Event triggered after a job is executed.
27
+ JOB_ON_SUCCESS : str
28
+ Event triggered when a job completes successfully.
29
+ JOB_ON_FAILURE : str
30
+ Event triggered when a job fails.
31
+ JOB_ON_MISSED : str
48
32
  Event triggered when a job is missed.
49
- JOB_SUBMITTED : str
50
- Event triggered when a job is submitted.
51
- JOB_MAX_INSTANCES : str
52
- Event triggered when a job exceeds max instances.
53
- ALL : str
54
- Event triggered for all events.
33
+ JOB_ON_MAXINSTANCES : str
34
+ Event triggered when a job exceeds its maximum allowed instances.
35
+ JOB_ON_PAUSED : str
36
+ Event triggered when a job is paused.
37
+ JOB_ON_RESUMED : str
38
+ Event triggered when a paused job is resumed.
39
+ JOB_ON_REMOVED : str
40
+ Event triggered when a job is removed.
41
+
42
+ Returns
43
+ -------
44
+ str
45
+ The string representation of the event name.
55
46
  """
56
47
 
57
- SCHEDULER_STARTED = "X-ORIONIS-EVENT-SCHEDULER-STARTED"
58
- SCHEDULER_SHUTDOWN = "X-ORIONIS-EVENT-SCHEDULER-SHUTDOWN"
59
- SCHEDULER_PAUSED = "X-ORIONIS-EVENT-SCHEDULER-PAUSED"
60
- SCHEDULER_RESUMED = "X-ORIONIS-EVENT-SCHEDULER-RESUMED"
61
- SCHEDULER_ERROR = "X-ORIONIS-SCHEDULER-ERROR"
62
- EXECUTOR_ADDED = "X-ORIONIS-EVENT-EXECUTOR-ADDED"
63
- EXECUTOR_REMOVED = "X-ORIONIS-EVENT-EXECUTOR-REMOVED"
64
- JOBSTORE_ADDED = "X-ORIONIS-EVENT-JOBSTORE-ADDED"
65
- JOBSTORE_REMOVED = "X-ORIONIS-EVENT-JOBSTORE-REMOVED"
66
- ALL_JOBS_REMOVED = "X-ORIONIS-EVENT-ALL-JOBS-REMOVED"
67
- JOB_ADDED = "X-ORIONIS-EVENT-JOB-ADDED"
68
- JOB_REMOVED = "X-ORIONIS-EVENT-JOB-REMOVED"
69
- JOB_MODIFIED = "X-ORIONIS-EVENT-JOB-MODIFIED"
70
- JOB_EXECUTED = "X-ORIONIS-EVENT-JOB-EXECUTED"
71
- JOB_ERROR = "X-ORIONIS-EVENT-JOB-ERROR"
72
- JOB_MISSED = "X-ORIONIS-EVENT-JOB-MISSED"
73
- JOB_SUBMITTED = "X-ORIONIS-EVENT-JOB-SUBMITTED"
74
- JOB_MAX_INSTANCES = "X-ORIONIS-EVENT-JOB-MAX-INSTANCES"
75
- ALL = "X-ORIONIS-EVENT-ALL"
48
+ # Scheduler-related events
49
+ SCHEDULER_STARTED = "schedulerStarted" # Triggered when the scheduler starts
50
+ SCHEDULER_SHUTDOWN = "schedulerShutdown" # Triggered when the scheduler shuts down
51
+ SCHEDULER_PAUSED = "schedulerPaused" # Triggered when the scheduler is paused
52
+ SCHEDULER_RESUMED = "schedulerResumed" # Triggered when the scheduler is resumed
53
+ SCHEDULER_ERROR = "schedulerError" # Triggered when the scheduler encounters an error
54
+
55
+ # Job-related events
56
+ JOB_BEFORE = "before" # Triggered before a job is executed
57
+ JOB_AFTER = "after" # Triggered after a job is executed
58
+ JOB_ON_FAILURE = "onFailure" # Triggered when a job fails
59
+ JOB_ON_MISSED = "onMissed" # Triggered when a job is missed
60
+ JOB_ON_MAXINSTANCES = "onMaxInstances" # Triggered when a job exceeds its max instances
61
+ JOB_ON_PAUSED = "onPaused" # Triggered when a job is paused
62
+ JOB_ON_RESUMED = "onResumed" # Triggered when a paused job is resumed
63
+ JOB_ON_REMOVED = "onRemoved" # Triggered when a job is removed
@@ -20,25 +20,34 @@ class Event(IEvent):
20
20
  """
21
21
  Initialize a new Event instance.
22
22
 
23
+ This constructor sets up the initial state of an Event object, including its
24
+ unique signature, arguments, purpose, and other optional attributes such as
25
+ random delay, start and end dates, trigger, details, listener, and maximum
26
+ instances. These attributes define the behavior and metadata of the event.
27
+
23
28
  Parameters
24
29
  ----------
25
30
  signature : str
26
- The unique identifier or signature for the event. This is typically
27
- used to distinguish between different events in the system.
28
- args : Optional[List[str]]
29
- A list of arguments required by the event. If not provided, an empty
30
- list will be used.
31
- purpose : Optional[str], optional
32
- A human-readable description or purpose for the event, by default None.
31
+ A unique identifier for the event. This is required and must be a non-empty string.
32
+ args : list of str, optional
33
+ A list of arguments associated with the event. Defaults to an empty list if None is provided.
34
+ purpose : str, optional
35
+ A human-readable description or purpose of the event. Defaults to None.
33
36
 
34
37
  Returns
35
38
  -------
36
39
  None
37
- This constructor does not return any value. It initializes the internal
38
- state of the Event object.
40
+ This method does not return any value. It initializes the Event instance.
41
+
42
+ Notes
43
+ -----
44
+ The `__trigger` attribute is initially set to None and can later be configured
45
+ to a Cron, Date, or Interval trigger. Similarly, the `__listener` attribute is
46
+ set to None and can be assigned an instance of `IScheduleEventListener` to handle
47
+ event-specific logic.
39
48
  """
40
49
 
41
- # Store the event's signature
50
+ # Store the event's unique signature
42
51
  self.__signature: str = signature
43
52
 
44
53
  # Store the event's arguments, defaulting to an empty list if None is provided
@@ -88,8 +97,12 @@ class Event(IEvent):
88
97
  # Validate that the signature is set and is a non-empty string
89
98
  if not self.__signature:
90
99
  raise CLIOrionisValueError("Signature is required for the event.")
100
+
101
+ # Validate arguments
91
102
  if not isinstance(self.__args, list):
92
103
  raise CLIOrionisValueError("Args must be a list.")
104
+
105
+ # Validate that purpose is a string if it is set
93
106
  if self.__purpose is not None and not isinstance(self.__purpose, str):
94
107
  raise CLIOrionisValueError("Purpose must be a string or None.")
95
108
 
@@ -111,6 +124,10 @@ class Event(IEvent):
111
124
  if self.__details is not None and not isinstance(self.__details, str):
112
125
  raise CLIOrionisValueError("Details must be a string or None.")
113
126
 
127
+ # Validate that listener is an IScheduleEventListener instance if it is set
128
+ if self.__listener is not None and not isinstance(self.__listener, IScheduleEventListener):
129
+ raise CLIOrionisValueError("Listener must implement IScheduleEventListener interface or be None.")
130
+
114
131
  # Construct and return an EventEntity with the current event's attributes
115
132
  return EventEntity(
116
133
  signature=self.__signature,
@@ -120,7 +137,8 @@ class Event(IEvent):
120
137
  start_date=self.__start_date,
121
138
  end_date=self.__end_date,
122
139
  trigger=self.__trigger,
123
- details=self.__details
140
+ details=self.__details,
141
+ listener=self.__listener
124
142
  )
125
143
 
126
144
  def purpose(
@@ -332,9 +350,9 @@ class Event(IEvent):
332
350
  when the event is executed.
333
351
  """
334
352
 
335
- # Validate that the provided listener implements the IScheduleEventListener interface
353
+ # Validate that the provided listener is an instance of IScheduleEventListener
336
354
  if not isinstance(listener, IScheduleEventListener):
337
- raise CLIOrionisValueError("Listener must implement IScheduleEventListener interface.")
355
+ raise CLIOrionisValueError("Listener must be an instance of IScheduleEventListener.")
338
356
 
339
357
  # Assign the listener to the event's internal listener attribute
340
358
  self.__listener = listener