fix: seed initial state from last 8KB of log on startup
This commit is contained in:
@@ -7,6 +7,7 @@ from typing import Callable, Awaitable, Optional
|
|||||||
class LogWatcher:
|
class LogWatcher:
|
||||||
POLL_INTERVAL = 0.1
|
POLL_INTERVAL = 0.1
|
||||||
FILE_RETRY_INTERVAL = 1.0
|
FILE_RETRY_INTERVAL = 1.0
|
||||||
|
SEED_BYTES = 8192 # read last ~8 KB on open to seed initial state
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -35,7 +36,11 @@ class LogWatcher:
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
with open(self._path, 'r', errors='replace') as f:
|
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
|
offline_since = None
|
||||||
while True:
|
while True:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
|
|||||||
Reference in New Issue
Block a user