-
-
Notifications
You must be signed in to change notification settings - Fork 761
Refactor FileWatchSensor to remove logshipper #5096
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
base: master
Are you sure you want to change the base?
Conversation
3b77378
to
0a2f6ae
Compare
285c6ba
to
d175507
Compare
Overall, I'm fine with the change, but some tests would be good :) |
Just running these tests gives us excellent coverage:
Almost all of the untested code was in the sensor itself. |
I tested this manually, both on macOS bare metal and in a Docker container (running on macOS).
FROM ubuntu:18.04
RUN apt update && apt install --yes git make python3-dev python3-pip
RUN python3 -m pip install coverage eventlet mock nose watchdog
VOLUME /code
WORKDIR /code
ENV PYTHONPATH=actions:sensors
ENV NOSE_COVER_CONFIG_FILE=.coveragerc
CMD nosetests --with-coverage --cover-branches --cover-html tests/test_file_watch_sensor.py And the coverage configuration [run]
include = sensors/file_watch_sensor.py
[report]
include = sensors/file_watch_sensor.py Commands (on bare metal): cd contrib/linux
docker build --tag contrib-linux-pack-test .
docker run --rm --volume $(pwd):/code --workdir /code contrib-linux-pack-test If you want to debug anything, run: docker run -it --rm --volume $(pwd):/code --workdir /code contrib-linux-pack-test /bin/bash Commands (within running Docker container): # run a single test (specified at the bottom of test_file_watch_sensor.py)
python3 tests/test_file_watch_sensor.py
# run all tests and report coverage results in HTML
nosetests --with-coverage --cover-branches --cover-html tests/test_file_watch_sensor.py then you can view results in your web browser. I'm having an issue with the Travis tests where inotify events don't seem to be delivered. I'll have to debug that. But these testing instructions should suffice for anybody else who is interested in testing this out. |
The requirements compile issues have been resolved. Now there's one consistent error left in the unit tests, though I don't know why this PR would cause that:
|
Well. I can't reproduce that test failure in CI. It might be related to #5358 which was just merged and touched the test that was failing. But, it's not failing anymore. If it shows up again, we can look into it. Anyway, all tests are green now. |
I'm currently testing this.
|
|
OK. With debug logging (using the sensor from this PR), I can see that:
We might also need to modify:
|
Since this doesn't work with eventlet yet, I marked it as draft. |
Using
When I modify the file, this forces it to continue on to the next line (because it was able to read the fd). Then it seems to get lost in the logger: Subsequent file edits never spew anything else from watchdog. Instead there are some lines from |
ced1299
to
83fa1ec
Compare
35fa884
to
bab2952
Compare
Lockfile diff: lockfiles/st2.lock [st2] == Added dependencies == watchdog 3.0.0 == Removed dependencies == logshipper 0.0.0 pyinotify 0.9.6
History of PRs touching the sensor in this pack (my git spelunking notes):
|
78aa157
to
36bacae
Compare
Watchdog uses threading. But the Actually, it looks like For reference, I used this to determine the state of monkey patching, just before the observer "thread" is supposed to start. In every scenario I tried, I got def start(self):
if self.tails and not self.started:
self.logger.debug("Starting TailManager")
import threading
self.logger.debug(f"threading monkey_patch: {threading.current_thread.__module__}")
import eventlet
self.logger.debug(f"threading monkey_patch: {eventlet.patcher.is_monkey_patched(threading)}")
self.observer.start()
I tested using eventlet.sleep, but that didn't make a difference because the context never goes back to that loop. |
This PR refactors the
FileWatchSensor
from thelinux
pack, to remove it's dependency onlogshipper
, and usewatchdog
instead.Upstream
logshipper
hasn't been touched in years, so we switched to our own fork. That hasn't been well maintained either, and theTail
implementation, which is all we use from it, is a bit of a mess on top of depending onpyinotify
has been itself unmaintained for even longer.This PR borrows some code from the previous sensor, but also breaks up the code into well structured classes that all have a single focus, possibly allowing this code to be reused elsewhere. Additionally, you can run the sensor Python script itself, which should help serve as an example of how to write a "good" sensor.
The
watchdog
project is now being actively maintained, has wide support for Linux, macOS, BSD, Windows, and a polling fallback implementation. Thepyinotify
package refuses to even install on macOS (not being able to run is understandable, but preventing installation is too limiting IMO).I realize that it's a bit odd to make the
linux
depend less on Linux-specific features, but sincepyinotify
refused to even install on macOS, I couldn't do simple things like runmake requirements
without Linux running in a full Docker or VM environment.This PR also fixes a bug in
logshipper
(has to do with lines that do not end in newlines - previous code only took the last character, new code takes the whole line), and also presents a workaround for another possible bug in the comments.Currently being held up by #5095.