Commit 3d1c1ba6 authored by Antoine Catton's avatar Antoine Catton

Add the ability to specify a timeout to logfollower function.

This will be used in wordpress logfollower version.
parent 424c77ad
...@@ -51,7 +51,7 @@ def wait_for_creation(filepath): ...@@ -51,7 +51,7 @@ def wait_for_creation(filepath):
finally: finally:
os.close(creation_fd) os.close(creation_fd)
def follow_each_line(filepath, rotated): def follow_each_line(filepath, rotated, timeout=-1):
mask = inotifyx.IN_MODIFY mask = inotifyx.IN_MODIFY
if rotated: if rotated:
mask |= inotifyx.IN_MOVE_SELF | inotifyx.IN_DELETE_SELF mask |= inotifyx.IN_MOVE_SELF | inotifyx.IN_DELETE_SELF
...@@ -62,8 +62,10 @@ def follow_each_line(filepath, rotated): ...@@ -62,8 +62,10 @@ def follow_each_line(filepath, rotated):
try: try:
inotifyx.add_watch(follow_fd, filepath, mask) inotifyx.add_watch(follow_fd, filepath, mask)
while True: while True:
events = inotifyx.get_events(follow_fd) events = inotifyx.get_events(follow_fd, timeout)
empty = True
for e in events: for e in events:
empty = False
# File has been moved # File has been moved
if e.mask & inotifyx.IN_MOVE_SELF or e.mask & inotifyx.IN_DELETE_SELF: if e.mask & inotifyx.IN_MOVE_SELF or e.mask & inotifyx.IN_DELETE_SELF:
# Watch the new file when created. # Watch the new file when created.
...@@ -86,6 +88,8 @@ def follow_each_line(filepath, rotated): ...@@ -86,6 +88,8 @@ def follow_each_line(filepath, rotated):
queued_data.write(temp.pop(-1)) queued_data.write(temp.pop(-1))
for line in temp: for line in temp:
yield line yield line
if empty:
yield None
finally: finally:
os.close(follow_fd) os.close(follow_fd)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment