From 5e0145ec21aaaeb721b797763a722ad156e11e9a Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 5 Mar 2025 23:58:17 +0100 Subject: [PATCH] improved signal handling --- cpu_governor_auto_adjust/config.py | 10 +++++-- .../cpu_governor_auto_adjust.py | 4 +-- cpu_governor_auto_adjust/schedule.py | 29 +++++++++++++------ pyproject.toml | 2 +- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cpu_governor_auto_adjust/config.py b/cpu_governor_auto_adjust/config.py index 33fad5d..84cceb6 100644 --- a/cpu_governor_auto_adjust/config.py +++ b/cpu_governor_auto_adjust/config.py @@ -22,9 +22,15 @@ class TriggerTuple(NamedTuple): class Config: - def __init__(self, basepath: Path) -> None: - self.basepath = basepath + def __init__(self, _basepath: Optional[Path] = None) -> None: + self._basepath = _basepath self.config = ArgumentsParser().parser.config + + @cached_property + def basepath(self) -> Path: + if self._basepath is None: + return Path(__file__).parent + return self._basepath @cached_property def root(self) -> etree._ElementTree: diff --git a/cpu_governor_auto_adjust/cpu_governor_auto_adjust.py b/cpu_governor_auto_adjust/cpu_governor_auto_adjust.py index c321759..397865f 100755 --- a/cpu_governor_auto_adjust/cpu_governor_auto_adjust.py +++ b/cpu_governor_auto_adjust/cpu_governor_auto_adjust.py @@ -15,8 +15,8 @@ def main() -> None: async def _main() -> None: - basepath = Path(__file__).parent.resolve() - config = Config(basepath) + cwd = Path.cwd() + config = Config(cwd) log = getLogger('main', loglevel=config.loglevel.upper()) if config.testmode: log.warning("starting in testmode, cpu adjustments have been disabled") diff --git a/cpu_governor_auto_adjust/schedule.py b/cpu_governor_auto_adjust/schedule.py index d6ad626..2f7971c 100644 --- a/cpu_governor_auto_adjust/schedule.py +++ b/cpu_governor_auto_adjust/schedule.py @@ -3,15 +3,16 @@ from cpu_governor_auto_adjust.config import Config from cpu_governor_auto_adjust.trigger import Trigger from cpu_governor_auto_adjust.governor import GovernorControl, Governor, _governor_list import asyncio +import signal from asyncio import Task -from functools import cached_property +from functools import cached_property, partial class TriggerScheduler(AppClass): def __init__(self, _config: Config) -> None: super().__init__(_config) - self.tasks: list[Task] = [] self.running_triggers: list[Trigger] = [] + self.loop = asyncio.get_event_loop() @cached_property def governor_control(self) -> GovernorControl: @@ -44,24 +45,34 @@ class TriggerScheduler(AppClass): def start_trigger(self, _trigger: Trigger) -> None: """Start a new trigger.""" if _trigger.config.type == "callback": - task = asyncio.create_task(self.callback_trigger(_trigger)) + self.loop.create_task(self.callback_trigger(_trigger)) else: - task = asyncio.create_task(self.run_once_trigger(_trigger)) + self.loop.create_task(self.run_once_trigger(_trigger)) self.running_triggers.append(_trigger) - self.tasks.append(task) async def stop_triggers(self) -> None: """Stop all triggers.""" - for task in self.tasks: + tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] + for task in tasks: + self.log.debug("canceling trigger task: %s", task.get_coro()) task.cancel() - await asyncio.gather(*self.tasks, return_exceptions=True) + await asyncio.gather(*tasks, return_exceptions=True) + self.loop.stop() async def run(self) -> None: """Run the scheduler and keep it alive until stopped.""" + + def signal_handler(sig): + self.log.info("received signal: %s", sig) + self.log.info("Exiting, stopping all triggers") + self.loop.create_task(self.stop_triggers()) + + for sig in [signal.SIGINT, signal.SIGTERM]: + self.loop.add_signal_handler(sig, partial(signal_handler, sig=signal.SIGINT)) + try: while True: self.establish_preferred_governor() await asyncio.sleep(1) # Keep the main function alive except asyncio.exceptions.CancelledError: - self.log.info("Exiting, stopping all triggers") - await self.stop_triggers() + pass diff --git a/pyproject.toml b/pyproject.toml index a095a4e..cc30cb2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cpu_governor_auto_adjust" -version = "0.1.11" +version = "0.1.13" description = "This application has been developed to automatically change cpu governor based on certain triggers." authors = [ { name = "Martin Reurekas", email = "martin@semrks.nl" }