-
-
Notifications
You must be signed in to change notification settings - Fork 782
File watch sensor to utilize trigger with parameters #3361
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
File watch sensor to utilize trigger with parameters #3361
Conversation
|
|
||
| def add_trigger(self, trigger): | ||
| pass | ||
| if trigger['type'] not in ['linux.file_watch.file_path']: |
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.
As discussed on Slack - I prototyped it this way for easiness.
If we decide to go with this approach I would propose to move this filtering to the base sensor class so those trigger handler methods only get called with triggers which belong to that sensor (so no additional filtering needed in sensor class trigger handler methods).
Another, even better future optimization (better than this suggested later filtering in the base class) would be to use a more advanced message routing so each sensor class would only receive triggers it should receive. Problem with that approach is that it's not fully backward compatible and would need to research how expensive it is to use queue per sensor (probably not too expensive).
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.
@Kami I think the trigger check is not strictly required. We already filter in TriggerWatcher.
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯ ps auxww | grep file_watch | grep -v grep tt_with_params ✭ ◼
vagrant 18758 1.1 2.1 115520 45008 pts/4 S+ 14:25 0:02 /mnt/src/storm/st2/virtualenv/bin/python /mnt/src/storm/st2/st2reactor/st2reactor/container/sensor_wrapper.py --pack=linux --file-path=/opt/stackstorm/packs/linux/sensors/file_watch_sensor.py --class-name=FileWatchSensor --trigger-type-refs=linux.file_watch.file_path,linux.file_watch.line --parent-args=["--config-file", "/mnt/src/storm/st2/tools/../conf/st2.dev.conf"]
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯
Note we pass --trigger-type-refs to the sensor wrapper. This is then passed to TriggerWatcher
https://github.com/StackStorm/st2/blob/master/st2reactor/st2reactor/container/sensor_wrapper.py#L354
So I thought you originally did it for safety measure.
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.
Good call - this was probably added after my original trigger with parameters change was implemented so it looks like my comment is obsolete :)
In any case, would be good to double check and verify that is indeed the case.
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.
I verified and we are filtering right in TriggerWatcher and sensors don't see triggers that they don't register.
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.
Cool, in this case we should get rid of checks in the sensor code :)
|
This looks right. To complete this PR, let's modify the line-watching sensor to take path as a trigger parameter, not a global parameter, too.
|
Dude, that's what this PR does. I am confused.
I do see a drawback which is the sensor code is now responsible for some kind of scheduling handling the trigger types. In this particular case, this logic is hidden luckily https://github.com/StackStorm/st2/pull/3361/files#diff-414b51e67b8a124ee29083fc2bbce3a8R19. But it also is a poor example for people. We might want to add an example where we loop over the trigger types (may come from different rules) and do something. |
dzimine
left a comment
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.
+1
I think you're adding file_watcher which takes parameters, but the line_watcher still relies on the global, no? |
|
Let's please also update the changelog. |
Not exactly sure what you mean with line watcher? There is no such thing as line watcher (or am I missing something) - file watcher emits a trigger each time a change (new line) is detected on monitored file. |
293afee to
e9c7f52
Compare
dzimine
left a comment
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.
@lakshmi-kannan there seem to be a mess between trigger references.
I don't quite get how you handle both .line and .file_pathtriggers.
Also, I am having troubles to make it work but likely because I'm in a rush and did something wrong.
| self._logger.info('Removed file "%s"' % (file_path)) | ||
|
|
||
| def _handle_line(self, file_path, line): | ||
| trigger = self._trigger_ref |
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.
Will it be always emitting 'linux.file_watch.line, see line 12?
| trigger: | ||
| parameters: | ||
| file_path: /var/log/dmesg | ||
| type: linux.file_watch.file_path |
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.
The emitted trigger is linux.file_watch.line (I checked the logs, too) and here it is linux.file_watch.file_path. Funniest is that it works.
| - | ||
| name: "file_watch.line" | ||
| pack: "linux" | ||
| description: "Trigger which indicates a new line has been detected" |
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.
need to add parameters_schema here, too
Cherry-picked #1291
Then, I made the following changes:
To test, do the following: