detect file rollover in LogTrigger
This commit is contained in:
@@ -62,8 +62,10 @@ class TriggerScheduler(AppClass):
|
|||||||
"""Start a new trigger."""
|
"""Start a new trigger."""
|
||||||
if _trigger.config.type == "async":
|
if _trigger.config.type == "async":
|
||||||
self.loop.create_task(self.async_trigger(_trigger))
|
self.loop.create_task(self.async_trigger(_trigger))
|
||||||
else:
|
elif _trigger.config.type == "sync":
|
||||||
self.loop.create_task(self.sync_trigger(_trigger))
|
self.loop.create_task(self.sync_trigger(_trigger))
|
||||||
|
else:
|
||||||
|
raise ValueError(f"unknown trigger type: {_trigger.config.type}")
|
||||||
self.running_triggers.append(_trigger)
|
self.running_triggers.append(_trigger)
|
||||||
|
|
||||||
async def stop_triggers(self) -> None:
|
async def stop_triggers(self) -> None:
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from cpu_governor_auto_adjust.trigger import Trigger
|
from cpu_governor_auto_adjust.trigger import Trigger
|
||||||
from cpu_governor_auto_adjust.config import Config
|
from cpu_governor_auto_adjust.config import Config
|
||||||
import asyncio
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class LogTrigger(Trigger):
|
class LogTrigger(Trigger):
|
||||||
@@ -50,16 +51,29 @@ class LogTrigger(Trigger):
|
|||||||
return
|
return
|
||||||
|
|
||||||
async def read_log(self) -> None:
|
async def read_log(self) -> None:
|
||||||
with open(self.config.custom_config['logFile'], 'r') as _file:
|
while True:
|
||||||
_file.seek(0, 2) # Move to the end of the file
|
try:
|
||||||
while True:
|
current_inode = os.stat(self.file).st_ino # Get initial inode
|
||||||
line = _file.readline()
|
|
||||||
if line:
|
with open(self.file, 'r') as _file:
|
||||||
self._process_line(line)
|
_file.seek(0, 2) # Move to the end of the file
|
||||||
else:
|
while True:
|
||||||
await asyncio.sleep(0.1)
|
line = _file.readline()
|
||||||
if (datetime.now() - self.timestamp_last_active_change) > timedelta(minutes=self.timeout_in_minutes):
|
if line:
|
||||||
self.set_inactive()
|
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:
|
async def async_run(self) -> None:
|
||||||
await self.read_log()
|
await self.read_log()
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "cpu_governor_auto_adjust"
|
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."
|
description = "This application has been developed to automatically change cpu governor based on certain triggers."
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "Martin Reurekas", email = "martin@semrks.nl" }
|
{ name = "Martin Reurekas", email = "martin@semrks.nl" }
|
||||||
|
|||||||
Reference in New Issue
Block a user