fix: seed initial state from last 8KB of log on startup

This commit is contained in:
Vlad Doloman
2026-06-22 20:11:00 +03:00
parent c3b06d4e99
commit fddbd8f4d4

View File

@@ -7,6 +7,7 @@ from typing import Callable, Awaitable, Optional
class LogWatcher:
POLL_INTERVAL = 0.1
FILE_RETRY_INTERVAL = 1.0
SEED_BYTES = 8192 # read last ~8 KB on open to seed initial state
def __init__(
self,
@@ -35,7 +36,11 @@ class LogWatcher:
while True:
try:
with open(self._path, 'r', errors='replace') as f:
f.seek(0, 2) # start at end of file (tail -f behaviour)
f.seek(0, 2)
end = f.tell()
f.seek(max(0, end - self.SEED_BYTES))
if f.tell() > 0:
f.readline() # discard partial first line
offline_since = None
while True:
line = f.readline()