detect file rollover in LogTrigger
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
+1
-1
@@ -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" }
|
||||
|
||||
Reference in New Issue
Block a user