Fix two issues in FileWatchSensor:
1. Mitigate / avoid race when calling add_file() before tailer has been
initialized
2. Fix a bug with failing to dispatch a trigger due to code using
invalid trigger type reference (it uses trigger ref instead of trigger
type ref)
import os
+import eventlet
+
from logshipper.tail import Tail
from st2reactor.sensor.base import Sensor
@@ -10,6 +12,7 @@ class FileWatchSensor(Sensor):
super(FileWatchSensor, self).__init__(sensor_service=sensor_service,
config=config)
self._trigger = None
+ self._trigger_type_ref = 'linux.file_watch.line'
self._logger = self._sensor_service.get_logger(__name__)
self._tail = None
@@ -42,6 +45,9 @@ class FileWatchSensor(Sensor):
if not self._trigger:
raise Exception('Trigger %s did not contain a ref.' % trigger)
+ # Wait a bit to avoid initialization race in logshipper library
+ eventlet.sleep(1.0)
+
self._tail.add_file(filename=file_path)
self._logger.info('Added file "%s"' % (file_path))
@@ -61,7 +67,7 @@ class FileWatchSensor(Sensor):
self._logger.info('Removed file "%s"' % (file_path))
def _handle_line(self, file_path, line):
- trigger = self._trigger
+ trigger = self._trigger_type_ref
payload = {
'file_path': file_path,
'file_name': os.path.basename(file_path),
