| 10 | |
| 11 | |
| 12 | class AsyncCLICommandBase(ABC): |
| 13 | def __init__(self) -> None: |
| 14 | self._container: Container | None = None |
| 15 | self._log: Log | None = None |
| 16 | |
| 17 | def initialize_container(self) -> None: |
| 18 | from src.core.di.container import Container |
| 19 | |
| 20 | self._container = Container() |
| 21 | self._log = self._container.log() |
| 22 | |
| 23 | @property |
| 24 | def container(self) -> "Container": |
| 25 | if self._container is None: |
| 26 | raise RuntimeError("Container not initialized. Call initialize_container() first.") |
| 27 | return self._container |
| 28 | |
| 29 | @property |
| 30 | def log(self) -> "Log": |
| 31 | if self._log is None: |
| 32 | raise RuntimeError("Logger not initialized. Call initialize_container() first.") |
| 33 | return self._log |
| 34 | |
| 35 | @abstractmethod |
| 36 | async def execute(self, loop: asyncio.AbstractEventLoop) -> int: |
| 37 | pass |
| 38 | |
| 39 | async def run(self, loop: asyncio.AbstractEventLoop) -> int: |
| 40 | try: |
| 41 | self.initialize_container() |
| 42 | return await self.execute(loop) |
| 43 | except ImportError as e: |
| 44 | print(f"Import error: {e}") |
| 45 | print("Make sure you're running this from the project root directory.") |
| 46 | return 1 |
| 47 | except Exception as e: |
| 48 | if self._log: |
| 49 | self._log.error(f"CLI command failed: {e}", error=e.__dict__) |
| 50 | else: |
| 51 | print(f"Error: {e}, type: {type(e)}, traceback: {e.__traceback__}") |
| 52 | return 1 |
| 53 | |
| 54 | @classmethod |
| 55 | def start(cls) -> None: |
| 56 | command = cls() |
| 57 | loop = asyncio.new_event_loop() |
| 58 | asyncio.set_event_loop(loop) |
| 59 | |
| 60 | shutdown_event = asyncio.Event() |
| 61 | main_task = None |
| 62 | |
| 63 | def signal_handler(signum: int, frame: Any) -> None: |
| 64 | print(f"\n🛑 Received signal {signum}. Initiating graceful shutdown...") |
| 65 | if main_task and not main_task.done(): |
| 66 | main_task.cancel() |
| 67 | shutdown_event.set() |
| 68 | |
| 69 | signal.signal(signal.SIGINT, signal_handler) |
nothing calls this directly
no outgoing calls
no test coverage detected