orionis 0.484.0__py3-none-any.whl → 0.485.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,56 @@
1
+ from datetime import datetime
2
+ from orionis.console.contracts.schedule import ISchedule
3
+
4
+ class BaseScheduler:
5
+
6
+ # Pause Global Scheduler at a specific time
7
+ PAUSE_AT: datetime = None
8
+
9
+ # Resume Global Scheduler at a specific time
10
+ RESUME_AT: datetime = None
11
+
12
+ # Finalize Global Scheduler at a specific time
13
+ FINALIZE_AT: datetime = None
14
+
15
+ def tasks(self, schedule: ISchedule):
16
+ """
17
+ Defines and registers scheduled tasks for the application.
18
+
19
+ Parameters
20
+ ----------
21
+ schedule : ISchedule
22
+ The schedule object used to define and register scheduled commands.
23
+
24
+ Returns
25
+ -------
26
+ None
27
+ This method does not return any value. It is intended to be overridden
28
+ by subclasses to specify scheduled tasks.
29
+
30
+ Notes
31
+ -----
32
+ This method should be implemented in subclasses to add specific tasks
33
+ to the scheduler using the provided `schedule` object.
34
+ """
35
+ # This method should be overridden in subclasses to define scheduled tasks.
36
+ pass
37
+
38
+ def onStarted(self):
39
+ # Called when the scheduler is started.
40
+ pass
41
+
42
+ def onPaused(self):
43
+ # Called when the scheduler is paused.
44
+ pass
45
+
46
+ def onResumed(self):
47
+ # Called when the scheduler is resumed.
48
+ pass
49
+
50
+ def onFinalized(self):
51
+ # Called when the scheduler is finalized.
52
+ pass
53
+
54
+ def onError(self, error: Exception):
55
+ # Called when an error occurs in the scheduler.
56
+ pass
@@ -11,20 +11,23 @@ from orionis.foundation.contracts.application import IApplication
11
11
 
12
12
  class ScheduleListCommand(BaseCommand):
13
13
  """
14
- Command class to display usage information for the Orionis CLI.
14
+ Command class to display a list of scheduled tasks defined in the Orionis application.
15
15
 
16
16
  Methods
17
17
  -------
18
- handle(orionis: IApplication, console: Console) -> bool
19
- Displays a table of scheduled tasks defined in the application.
20
- Imports the scheduler module, retrieves scheduled jobs, and prints them
21
- in a formatted table using the rich library.
18
+ handle(app: IApplication, console: Console) -> bool
19
+ Lists all scheduled jobs configured in the application by retrieving them from the
20
+ ISchedule service and displaying them in a formatted table using the rich library.
21
+
22
+ Attributes
23
+ ----------
24
+ timestamps : bool
25
+ Indicates whether timestamps will be shown in the command output.
26
+ signature : str
27
+ Command signature for invocation.
28
+ description : str
29
+ Description of the command.
22
30
 
23
- Returns
24
- -------
25
- bool
26
- Returns True if the scheduled jobs are listed successfully or if no jobs are found.
27
- Raises CLIOrionisRuntimeError if an error occurs during execution.
28
31
  """
29
32
 
30
33
  # Indicates whether timestamps will be shown in the command output
@@ -36,18 +39,19 @@ class ScheduleListCommand(BaseCommand):
36
39
  # Command description
37
40
  description: str = "Executes the scheduled tasks defined in the application."
38
41
 
39
- def handle(self, orionis: IApplication, console: Console) -> bool:
42
+ def handle(self, app: IApplication, console: Console) -> bool:
40
43
  """
41
44
  Displays a table of scheduled jobs defined in the application.
42
45
 
43
- This method dynamically imports the scheduler module, retrieves the list of
44
- scheduled jobs using the ISchedule service, and prints the jobs in a formatted
45
- table. If no jobs are found, a message is displayed. Handles and reports errors
46
- encountered during the process.
46
+ This method dynamically retrieves the scheduler instance and the ISchedule service
47
+ from the application, registers scheduled tasks, and fetches the list of scheduled
48
+ jobs. It then prints the jobs in a formatted table using the rich library. If no
49
+ jobs are found, a message is displayed. Any errors encountered during the process
50
+ are reported as CLIOrionisRuntimeError.
47
51
 
48
52
  Parameters
49
53
  ----------
50
- orionis : IApplication
54
+ app : IApplication
51
55
  The application instance providing configuration and service resolution.
52
56
  console : Console
53
57
  The rich Console instance used for output.
@@ -58,45 +62,19 @@ class ScheduleListCommand(BaseCommand):
58
62
  Returns True if the scheduled jobs are listed successfully or if no jobs are found.
59
63
  Raises CLIOrionisRuntimeError if an error occurs during execution.
60
64
  """
61
-
62
65
  try:
63
66
 
64
- # Get the absolute path of the scheduler from the application configuration
65
- scheduler_path = orionis.path('console_scheduler')
66
-
67
- # Get the base path from the current working directory
68
- base_path = Path(os.getcwd()).resolve()
69
- scheduler_path = Path(scheduler_path).resolve()
70
- rel_path = scheduler_path.relative_to(base_path)
71
-
72
- # Convert the path to a module name (replace separators with dots, remove .py)
73
- module_name = ".".join(rel_path.with_suffix('').parts)
74
-
75
- # Dynamically import the scheduler module
76
- scheduler_module = importlib.import_module(module_name)
67
+ # Retrieve the Scheduler instance from the application
68
+ Scheduler = app.getScheduler()
77
69
 
78
- # Retrieve the Scheduler class from the imported module
79
- Scheduler = getattr(scheduler_module, "Scheduler", None)
70
+ # Create an instance of the ISchedule service
71
+ schedule_service: ISchedule = app.make(ISchedule)
80
72
 
81
- # Raise an error if the Scheduler class is not found
82
- if Scheduler is None:
83
- raise CLIOrionisRuntimeError(f"Scheduler class not found in module {module_name}")
84
-
85
- # Retrieve the 'tasks' method from the Scheduler class
86
- task_method = getattr(Scheduler, "tasks", None)
87
-
88
- # Raise an error if the 'tasks' method is not found
89
- if task_method is None:
90
- raise CLIOrionisRuntimeError(f"Method 'tasks' not found in Scheduler class in module {module_name}")
91
-
92
- # Create an instance of ISchedule using the application container
93
- schedule_serice: ISchedule = orionis.make(ISchedule)
94
-
95
- # Initialize the scheduled tasks by calling the 'tasks' method
96
- task_method(schedule_serice)
73
+ # Register scheduled tasks using the Scheduler's tasks method
74
+ Scheduler.tasks(schedule_service)
97
75
 
98
76
  # Retrieve the list of scheduled jobs/events
99
- list_tasks = schedule_serice.events()
77
+ list_tasks = schedule_service.events()
100
78
 
101
79
  # Display a message if no scheduled jobs are found
102
80
  if not list_tasks:
@@ -134,6 +112,7 @@ class ScheduleListCommand(BaseCommand):
134
112
  return True
135
113
 
136
114
  except Exception as exc:
137
-
138
115
  # Catch any unexpected exceptions and raise as a CLIOrionisRuntimeError
139
- raise CLIOrionisRuntimeError(f"An unexpected error occurred while clearing the cache: {exc}")
116
+ raise CLIOrionisRuntimeError(
117
+ f"An unexpected error occurred while listing the scheduled jobs: {exc}"
118
+ )
@@ -1,7 +1,4 @@
1
- import importlib
2
- import os
3
1
  from datetime import datetime
4
- from pathlib import Path
5
2
  from rich.console import Console
6
3
  from rich.panel import Panel
7
4
  from rich.text import Text
@@ -47,18 +44,17 @@ class ScheduleWorkCommand(BaseCommand):
47
44
  # Command description
48
45
  description: str = "Executes the scheduled tasks defined in the application."
49
46
 
50
- async def handle(self, orionis: IApplication, console: Console) -> bool:
47
+ async def handle(self, app: IApplication, console: Console) -> bool:
51
48
  """
52
49
  Executes the scheduled tasks defined in the application's scheduler.
53
50
 
54
- This method dynamically loads the scheduler module specified in the application's configuration,
55
- retrieves the `Scheduler` class and its `tasks` method, registers the scheduled tasks with the
56
- ISchedule service, and starts the scheduler worker. It provides user feedback via the console and
57
- handles errors by raising CLIOrionisRuntimeError exceptions.
51
+ This method retrieves the Scheduler instance from the application, registers scheduled tasks
52
+ with the ISchedule service, and starts the scheduler worker asynchronously. It provides user
53
+ feedback via the console and handles errors by raising CLIOrionisRuntimeError exceptions.
58
54
 
59
55
  Parameters
60
56
  ----------
61
- orionis : IApplication
57
+ app : IApplication
62
58
  The application instance providing configuration and service resolution.
63
59
  console : Console
64
60
  The Rich console instance used for displaying output to the user.
@@ -66,8 +62,7 @@ class ScheduleWorkCommand(BaseCommand):
66
62
  Returns
67
63
  -------
68
64
  bool
69
- Returns True if the scheduler worker starts successfully. If an error occurs during the process,
70
- a CLIOrionisRuntimeError is raised.
65
+ True if the scheduler worker starts successfully.
71
66
 
72
67
  Raises
73
68
  ------
@@ -75,45 +70,15 @@ class ScheduleWorkCommand(BaseCommand):
75
70
  If the scheduler module, class, or tasks method cannot be found, or if any unexpected error occurs.
76
71
  """
77
72
  try:
78
- # Get the absolute path to the scheduler module from the application configuration
79
- scheduler_path = orionis.path('console_scheduler')
80
73
 
81
- # Resolve the base path (current working directory)
82
- base_path = Path(os.getcwd()).resolve()
83
- scheduler_path = Path(scheduler_path).resolve()
84
-
85
- # Compute the relative path from the base path to the scheduler module
86
- rel_path = scheduler_path.relative_to(base_path)
87
-
88
- # Convert the relative path to a Python module name (dot notation, no .py extension)
89
- module_name = ".".join(rel_path.with_suffix('').parts)
90
-
91
- # Dynamically import the scheduler module
92
- scheduler_module = importlib.import_module(module_name)
93
-
94
- # Retrieve the Scheduler class from the imported module
95
- Scheduler = getattr(scheduler_module, "Scheduler", None)
96
- if Scheduler is None:
97
- raise CLIOrionisRuntimeError(f"Scheduler class not found in module {module_name}")
98
-
99
- # Create an instance of the Scheduler class
100
- Scheduler = Scheduler()
101
-
102
- # Retrieve the 'tasks' method from the Scheduler class
103
- task_method = getattr(Scheduler, "tasks", None)
104
- if task_method is None:
105
- raise CLIOrionisRuntimeError(f"Method 'tasks' not found in Scheduler class in module {module_name}")
74
+ # Retrieve the Scheduler instance from the application
75
+ Scheduler = app.getScheduler()
106
76
 
107
77
  # Create an instance of the ISchedule service
108
- schedule_serice: ISchedule = orionis.make(ISchedule)
78
+ schedule_serice: ISchedule = app.make(ISchedule)
109
79
 
110
80
  # Register scheduled tasks using the Scheduler's tasks method
111
- task_method(schedule_serice)
112
-
113
- # Register event listeners for the scheduler
114
- onSchedulerStarted = getattr(Scheduler, "onSchedulerStarted", None)
115
- if onSchedulerStarted:
116
- schedule_serice.addListenerOnSchedulerStarted(onSchedulerStarted)
81
+ Scheduler.tasks(schedule_serice)
117
82
 
118
83
  # Display a start message for the scheduler worker
119
84
  console.line()
@@ -138,4 +103,6 @@ class ScheduleWorkCommand(BaseCommand):
138
103
 
139
104
  except Exception as exc:
140
105
  # Raise any unexpected exceptions as CLIOrionisRuntimeError
141
- raise CLIOrionisRuntimeError(f"An unexpected error occurred while clearing the cache: {exc}")
106
+ raise CLIOrionisRuntimeError(
107
+ f"An unexpected error occurred while starting the scheduler worker: {exc}"
108
+ )
@@ -1,4 +1,3 @@
1
- from orionis.app import Orionis
2
1
  from orionis.console.base.command import BaseCommand
3
2
  from orionis.console.exceptions import CLIOrionisRuntimeError
4
3
  from orionis.foundation.contracts.application import IApplication
@@ -0,0 +1,27 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ class IEventListener(ABC):
4
+
5
+ @abstractmethod
6
+ def before(self, event):
7
+ pass
8
+
9
+ @abstractmethod
10
+ def after(self, event):
11
+ pass
12
+
13
+ @abstractmethod
14
+ def onSuccess(self, event):
15
+ pass
16
+
17
+ @abstractmethod
18
+ def onFailure(self, event):
19
+ pass
20
+
21
+ @abstractmethod
22
+ def onMissed(self, event):
23
+ pass
24
+
25
+ @abstractmethod
26
+ def onMaxInstances(self, event):
27
+ pass
@@ -3,7 +3,6 @@ import os
3
3
  import re
4
4
  from pathlib import Path
5
5
  from typing import Any, List, Optional
6
- from orionis.app import Orionis
7
6
  from orionis.console.args.argument import CLIArgument
8
7
  from orionis.console.base.command import BaseCommand
9
8
  from orionis.console.base.contracts.command import IBaseCommand
@@ -51,7 +50,7 @@ class Reactor(IReactor):
51
50
  -----
52
51
  - Command discovery is performed automatically during initialization
53
52
  - The current working directory is used as the project root for module resolution
54
- - Commands are loaded from the path specified by app.path('console_commands')
53
+ - Commands are loaded from the path specified by app.path('commands')
55
54
  - The internal command registry is initialized as an empty dictionary before loading
56
55
  """
57
56
 
@@ -59,13 +58,13 @@ class Reactor(IReactor):
59
58
  self.__app = app
60
59
 
61
60
  # Set the project root directory to current working directory for module path resolution
62
- self.__root: str = str(Path.cwd())
61
+ self.__root = self.__app.path('root') or Path().cwd()
63
62
 
64
63
  # Initialize the internal command registry as an empty dictionary
65
64
  self.__commands: dict[str, Command] = {}
66
65
 
67
66
  # Automatically discover and load command classes from the console commands directory
68
- self.__loadCommands(str(self.__app.path('console_commands')), self.__root)
67
+ self.__loadCommands(str(self.__app.path('commands')), self.__root)
69
68
 
70
69
  # Load core command classes provided by the Orionis framework
71
70
  self.__loadCoreCommands()
@@ -5,6 +5,7 @@ from apscheduler.triggers.cron import CronTrigger
5
5
  from apscheduler.triggers.date import DateTrigger
6
6
  from apscheduler.triggers.interval import IntervalTrigger
7
7
  from orionis.console.contracts.event import IEvent
8
+ from orionis.console.contracts.event_listener import IEventListener
8
9
  from orionis.console.enums.event import Event as EventEntity
9
10
  from orionis.console.exceptions import CLIOrionisValueError
10
11
 
@@ -249,6 +250,12 @@ class Event(IEvent):
249
250
  # Return self to support method chaining
250
251
  return self
251
252
 
253
+ def listener(
254
+ self,
255
+ listener: 'IEventListener'
256
+ ) -> 'Event':
257
+ return self
258
+
252
259
  def onceAt(
253
260
  self,
254
261
  date: datetime
File without changes
@@ -80,7 +80,8 @@ class Scheduler(ISchedule):
80
80
  self.__jobs: List[dict] = []
81
81
 
82
82
  # Add a listener to the scheduler to capture job events such as missed jobs or errors.
83
- self.__scheduler.add_listener(self.__listener, EVENT_ALL)
83
+ if self.__app.config('app.debug', False):
84
+ self.__scheduler.add_listener(self.__listener, EVENT_ALL)
84
85
 
85
86
  # Log the initialization of the Scheduler.
86
87
  self.__logger.info("Orionis scheduler initialized.")