Skip to content

Commit

Permalink
HOTFIX:
Browse files Browse the repository at this point in the history
- LogAnalyser: performance issue fixed
  • Loading branch information
karel26 committed Jan 20, 2025
1 parent 04b4089 commit 5e9dcad
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions extensions/loganalyser/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def do_startup(self):
asyncio.create_task(self.check_log())

async def prepare(self) -> bool:
os.remove(self.logfile)
await self.do_startup()
self.running = True
return await super().startup()
Expand All @@ -71,6 +72,12 @@ def shutdown(self) -> bool:
self.stop_event.set()
return super().shutdown()

@property
def logfile(self) -> str:
return os.path.expandvars(
self.config.get('log', os.path.join(self.server.instance.home, 'Logs', 'dcs.log'))
)

async def check_log(self):
try:
logfile = os.path.expandvars(
Expand All @@ -83,28 +90,40 @@ async def check_log(self):

self.log_pos = 0
while not self.stop_event.is_set():
if not os.path.exists(logfile):
self.log_pos = 0
await asyncio.sleep(1)
continue

async with aiofiles.open(logfile, mode='r', encoding='utf-8', errors='ignore') as file:
await file.seek(self.log_pos, 0)
async for line in file:
if '=== Log closed.' in line:
self.log_pos = -1
return
match = combined_pattern.search(line)
if match:
for key, value in match.groupdict().items():
if value:
callback = callback_map[key]
if asyncio.iscoroutinefunction(callback):
asyncio.create_task(callback(self.log_pos, line, match))
else:
self.loop.run_in_executor(None, callback, self.log_pos, line, match)
try:
if not os.path.exists(logfile):
self.log_pos = 0
continue

async with aiofiles.open(logfile, mode='r', encoding='utf-8', errors='ignore') as file:
max_pos = os.fstat(file.fileno()).st_size
if self.log_pos == -1 or max_pos == self.log_pos:
self.log_pos = max_pos
continue
# if the logfile was rotated, seek to the beginning of the file
elif max_pos < self.log_pos:
self.log_pos = 0

self.log_pos = await file.seek(self.log_pos, 0)
await file.seek(self.log_pos, 0)
async for line in file:
if '=== Log closed.' in line:
self.log_pos = -1
return
match = combined_pattern.search(line)
if match:
for key, value in match.groupdict().items():
if value:
callback = callback_map[key]
if asyncio.iscoroutinefunction(callback):
asyncio.create_task(callback(self.log_pos, line, match))
else:
self.loop.run_in_executor(None, callback, self.log_pos, line, match)
self.log_pos = await file.tell()

except FileNotFoundError as ex:
pass
finally:
await asyncio.sleep(1)
except Exception as ex:
self.log.exception(ex)
finally:
Expand Down

0 comments on commit 5e9dcad

Please sign in to comment.