diff --git a/cpu_governor_auto_adjust/schedule.py b/cpu_governor_auto_adjust/schedule.py index c3607f3..6f44275 100644 --- a/cpu_governor_auto_adjust/schedule.py +++ b/cpu_governor_auto_adjust/schedule.py @@ -62,8 +62,10 @@ class TriggerScheduler(AppClass): """Start a new trigger.""" if _trigger.config.type == "async": self.loop.create_task(self.async_trigger(_trigger)) - else: + elif _trigger.config.type == "sync": self.loop.create_task(self.sync_trigger(_trigger)) + else: + raise ValueError(f"unknown trigger type: {_trigger.config.type}") self.running_triggers.append(_trigger) async def stop_triggers(self) -> None: diff --git a/cpu_governor_auto_adjust/triggers/log.py b/cpu_governor_auto_adjust/triggers/log.py index bbc429d..fc31606 100644 --- a/cpu_governor_auto_adjust/triggers/log.py +++ b/cpu_governor_auto_adjust/triggers/log.py @@ -1,8 +1,9 @@ from cpu_governor_auto_adjust.trigger import Trigger from cpu_governor_auto_adjust.config import Config -import asyncio from datetime import datetime, timedelta from functools import cached_property +import asyncio +import os class LogTrigger(Trigger): @@ -50,16 +51,29 @@ class LogTrigger(Trigger): return async def read_log(self) -> None: - with open(self.config.custom_config['logFile'], 'r') as _file: - _file.seek(0, 2) # Move to the end of the file - while True: - line = _file.readline() - if line: - self._process_line(line) - else: - await asyncio.sleep(0.1) - if (datetime.now() - self.timestamp_last_active_change) > timedelta(minutes=self.timeout_in_minutes): - self.set_inactive() + while True: + try: + current_inode = os.stat(self.file).st_ino # Get initial inode + + with open(self.file, 'r') as _file: + _file.seek(0, 2) # Move to the end of the file + while True: + line = _file.readline() + if line: + self._process_line(line) + else: + await asyncio.sleep(0.1) + # Check if the trigger has not been triggered for the timeout period + if (datetime.now() - self.timestamp_last_active_change) > timedelta(minutes=self.timeout_in_minutes): + self.set_inactive() + + # Detect if file has been rotated + if os.stat(self.file).st_ino != current_inode: + self.log.debug("File %s rolled over. Reopening...", self.file) + break # Exit inner loop to reopen file + except FileNotFoundError: + self.log.debug("file not found: %s", self.file) + await asyncio.sleep(1) async def async_run(self) -> None: await self.read_log() diff --git a/pyproject.toml b/pyproject.toml index 5278547..ce8356b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cpu_governor_auto_adjust" -version = "0.1.44" +version = "0.2.2" description = "This application has been developed to automatically change cpu governor based on certain triggers." authors = [ { name = "Martin Reurekas", email = "martin@semrks.nl" }