From e451de2179a92008802e0cdc538d364e9ebbcc13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 2 Mar 2018 10:21:56 +0100 Subject: [PATCH 1/5] Implement DirectoryMonitor support in ProcessManagingEventHandler --- .../components/process_managing_event_handler.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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']) From 715e39af0df3b23a7bd5e4e3652a4cafe5aff549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 2 Mar 2018 10:23:10 +0100 Subject: [PATCH 2/5] Expose DirectoryMonitor instance from SourceCodeEventHandler --- lib/tfw/components/source_code_event_handler.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 33d6e4d..c8a8ad9 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 From cb88acb54fa283556b7a1e2607d9bd2da4750cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 2 Mar 2018 10:43:09 +0100 Subject: [PATCH 3/5] Fix ProcessMonitor pausing being useless due to pmgeh refactor --- src/demo/event_handler_main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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: From 3e06f5c196fa23a195a03ebe23dd73272c05262c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 2 Mar 2018 11:04:58 +0100 Subject: [PATCH 4/5] Optimize write event responses by removing unused file content --- lib/tfw/components/source_code_event_handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index c8a8ad9..91b4fb3 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -71,6 +71,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler): with self.monitor.pauser: try: self.filemanager.file_contents = data['content'] except Exception: log.exception('Error writing file!') + data['content'] = '' return data def select(self, data): From 207f984db1b75ad129bef99452fbbf2a5b45b138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 2 Mar 2018 14:02:05 +0100 Subject: [PATCH 5/5] Delete key instead of setting it to empty string value --- lib/tfw/components/source_code_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 91b4fb3..19896d4 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -71,7 +71,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler): with self.monitor.pauser: try: self.filemanager.file_contents = data['content'] except Exception: log.exception('Error writing file!') - data['content'] = '' + del data['content'] return data def select(self, data):