Skip to content
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

Losing offset #56

Open
jmrichardson opened this issue May 4, 2020 · 2 comments
Open

Losing offset #56

jmrichardson opened this issue May 4, 2020 · 2 comments

Comments

@jmrichardson
Copy link

Hi, I have this code to write to the offset every line. However, if I ctrl-c the script, the offset file occasionally (about 1 out of 4) is empty and it starts from beginning. Shouldn't this write be atomic or have some test to verify:


from pygtail import Pygtail
import time

sleep = 1
symbol = 'data.csv'
symbol = 'data/kibot/IBM.txt'

pt = Pygtail(symbol, every_n=1, paranoid=True)
while True:
    try:
        line = pt.next().strip()
        print(line)
    except StopIteration:
        print("sleep")
        time.sleep(sleep)
    except Exception as e:
        print(str(e))
@jmrichardson
Copy link
Author

I ended up extending the _update_offset_file method:

from atomicwrites import atomic_write
class File(Pygtail):
    def _update_offset_file(self):
        if self.on_update:
            self.on_update()
        offset = self._filehandle().tell()
        inode = os.stat(self.filename).st_ino
        with atomic_write(self._offset_file, overwrite=True) as fh:
            fh.write("%s\n%s\n" % (inode, offset))
        self._since_update = 0

JuergenBS added a commit to JuergenBS/pygtail that referenced this issue Dec 27, 2020
@omersiar
Copy link

I can reproduce this. read_from_end=True seems to not work as expected as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants