From 0b3db0ae9e3da0174e7f9c6f65d749434e78657e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 14 Feb 2018 16:09:09 +0100 Subject: [PATCH 1/2] Implement pausing watchdog while webide files are being written --- lib/tfw/components/directory_monitor.py | 12 +++++++++++- lib/tfw/components/source_code_event_handler.py | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 6dbf78f..7e7a11e 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -12,9 +12,17 @@ class WebideReloadEventHandler(FileSystemEventHandler): def __init__(self): super().__init__() self.uplink = ServerUplinkConnector() + self._paused = False + + def pause(self): + self._paused = True + + def resume(self): + self._paused = False @RateLimiter(rate_per_second=5) def on_modified(self, event): + if self._paused: return log.debug(event) anchor = 'anchor_webide' self.uplink.send(anchor, {'anchor': anchor, @@ -24,7 +32,9 @@ class WebideReloadEventHandler(FileSystemEventHandler): class DirectoryMonitor: def __init__(self, directory): self.observer = Observer() - self.observer.schedule(WebideReloadEventHandler(), directory, recursive=True) + self.eventhandler = WebideReloadEventHandler() + self.observer.schedule(self.eventhandler, directory, recursive=True) + self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume def watch(self): self.observer.start() diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index a0964a8..d838a27 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -68,9 +68,11 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): return data def write(self, data): + self.monitor.pause() try: self.filemanager.file_contents = data['content'] except Exception: log.exception('Error writing file!') self.restart_process() + self.monitor.resume() return data def select(self, data): From c2e161dad3b1edb263ee9df987cdae3da5e2f6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 14 Feb 2018 17:44:05 +0100 Subject: [PATCH 2/2] Make DirectoryMonitor pausable using the with statement --- lib/tfw/components/directory_monitor.py | 12 ++++++++++++ lib/tfw/components/source_code_event_handler.py | 9 ++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 7e7a11e..5c7da14 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -42,3 +42,15 @@ class DirectoryMonitor: def stop(self): self.observer.stop() self.observer.join() + + @property + def pauser(self): + return DirectoryMonitor.Pauser(self) + + class Pauser: + def __init__(self, directory_monitor): + self.directorymonitor = directory_monitor + def __enter__(self): + self.directorymonitor.pause() + def __exit__(self, exc_type, exc_val, exc_tb): + self.directorymonitor.resume() diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index d838a27..b0d079a 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -68,11 +68,10 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): return data def write(self, data): - self.monitor.pause() - try: self.filemanager.file_contents = data['content'] - except Exception: log.exception('Error writing file!') - self.restart_process() - self.monitor.resume() + with self.monitor.pauser: + try: self.filemanager.file_contents = data['content'] + except Exception: log.exception('Error writing file!') + self.restart_process() return data def select(self, data):