-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed an issue where the tail would fail in case of file rename and replace as in log rotation scenario. #22
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import os | ||
import re | ||
import sys | ||
import time | ||
|
@@ -160,7 +161,17 @@ def follow(self, delay=1.0): | |
""" | ||
trailing = True | ||
|
||
initial_inode = os.stat(self.file.name).st_ino | ||
file_path = self.file.name | ||
|
||
while 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
try: | ||
if os.stat(self.file.name).st_ino != initial_inode: | ||
self.file = open(file_path) | ||
initial_inode = os.stat(self.file.name).st_ino | ||
except FileNotFoundError: | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will get stuck in a tight infinite loop if the file does not exist. We need a non-blocking wait. The easiest way to do this is to introduce a See comments on https://github.com/six8/pytailer/pull/14/files#r1167325214 We want to mimic what GNU tail is doing. With GNU tail, the GNU tail adds a Note: In See the GNU tail docs for more info https://man7.org/linux/man-pages/man1/tail.1.html The following tests needed to be added. Start with the tests, then the implementation will be more obvious:
This means adding a new
You can see how GNU tail is implemented at https://github.com/coreutils/coreutils/blob/master/src/tail.c. It uses |
||
|
||
where = self.file.tell() | ||
line = self.file.readline() | ||
if line: | ||
|
@@ -234,9 +245,18 @@ def follow(file, delay=1.0): | |
>>> f.flush() | ||
>>> next(generator) | ||
'Line 2' | ||
>>> os.rename("test_follow.txt", "test_follow_1.txt") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a separate test case. The doctests are meant to act as documentation as well as tests. When we start adding new test cases, they should be added as separate pytests. |
||
>>> f = open('test_follow.txt', 'w') | ||
>>> fo = open('test_follow.txt', 'r') | ||
>>> generator = follow(fo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be testing the case of "when a file currently being opened is renamed" while following. This is starting a new |
||
>>> _ = f.write('Line 1\\n') | ||
>>> f.flush() | ||
>>> next(generator) | ||
'Line 1' | ||
>>> f.close() | ||
>>> fo.close() | ||
>>> os.remove('test_follow.txt') | ||
>>> os.remove('test_follow_1.txt') | ||
""" | ||
return Tailer(file, end=True).follow(delay) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tailer
is meant to work on streams as well. So anio.StringIO
should work too.io.StringIO
does not have a.name
parameter so this would fail.