diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index ec20fce..6fcc248 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -1,3 +1,5 @@ +from functools import wraps + from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.mixins import SupervisorMixin from tfw.networking.event_handlers.server_connector import ServerUplinkConnector @@ -16,12 +18,23 @@ class ProcessManager(SupervisorMixin): class ProcessManagingEventHandler(TriggerlessEventHandler): - def __init__(self, key): + def __init__(self, key, dirmonitor=None): super().__init__(key) self.key = key + self.monitor = dirmonitor self.processmanager = ProcessManager() self.uplink = ServerUplinkConnector() + def _with_monitor_paused(fun): + @wraps(fun) + def wrapper(self, *args, **kwargs): + if self.monitor: + with self.monitor.pauser: + return fun(self, *args, **kwargs) + return fun(self, *args, **kwargs) + return wrapper + + @_with_monitor_paused def handle_event(self, key, data_json): data = data_json['data'] self.processmanager(data['command'], data['process_name']) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 33d6e4d..19896d4 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -53,8 +53,12 @@ class SourceCodeEventHandler(TriggerlessEventHandler): 'select': self.select } - self.monitor = DirectoryMonitor(directory) - self.monitor.watch() # This runs on a separate thread + self._monitor = DirectoryMonitor(directory) + self._monitor.watch() # This runs on a separate thread + + @property + def monitor(self): + return self._monitor def read(self, data): try: data['content'] = self.filemanager.file_contents @@ -67,6 +71,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler): with self.monitor.pauser: try: self.filemanager.file_contents = data['content'] except Exception: log.exception('Error writing file!') + del data['content'] return data def select(self, data): diff --git a/src/demo/event_handler_main.py b/src/demo/event_handler_main.py index 35a8f09..dfbd5bb 100644 --- a/src/demo/event_handler_main.py +++ b/src/demo/event_handler_main.py @@ -7,9 +7,11 @@ from tfw.config import tfwenv if __name__ == '__main__': - eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD), - TerminadoEventHandler('terminado', 'terminado'), - ProcessManagingEventHandler('processmanager')} + ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD) + terminado = TerminadoEventHandler('terminado', 'terminado') + processmanager = ProcessManagingEventHandler('processmanager', ide.monitor) + + eventhandlers = {ide, terminado, processmanager} try: IOLoop.instance().start() finally: