From 22470ee76aabc2f59cb04a1d07c2ab531dae01b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Thu, 13 Dec 2018 21:30:27 +0100 Subject: [PATCH 1/4] debounce multiple modified events that occur at a same moment https://github.com/merofeev/docker-windows-volume-watcher/issues/13 --- docker_volume_watcher/container_notifier.py | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docker_volume_watcher/container_notifier.py b/docker_volume_watcher/container_notifier.py index c335560..4beffae 100644 --- a/docker_volume_watcher/container_notifier.py +++ b/docker_volume_watcher/container_notifier.py @@ -10,6 +10,25 @@ from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler +import time + + +def debounce(s): + """Decorator ensures function that can only be called once every `s` seconds. + """ + def decorate(f): + t = None + + def wrapped(*args, **kwargs): + nonlocal t + t_ = time.time() + if t is None or t_ - t >= s: + result = f(*args, **kwargs) + t = time.time() + return result + return wrapped + return decorate + class NonZeroExitError(RuntimeError): """ A non-zero exit code error from the command execution in docker. @@ -54,6 +73,10 @@ def __init__(self, container, host_dir, container_dir, exclude_patterns=None): def __str__(self): return '%s -> %s:%s' % (self.host_dir, self.container.name, self.container_dir) + # debounce avoid two subsequents save to ooccur + # limitation, if several files are saved the same time + # some files could be skipped + @debounce(2) def __change_handler(self, event): host_path = event.dest_path if hasattr(event, 'dest_path') else event.src_path relative_host_path = relpath(host_path, self.host_dir).replace('\\', '/') From d7b94735efb7e3a89e5ffc0d115550c79306d76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Thu, 13 Dec 2018 23:27:51 +0100 Subject: [PATCH 2/4] fixed travis errors + fixed nonlocal not compatible with python 2.7 --- docker_volume_watcher/container_notifier.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docker_volume_watcher/container_notifier.py b/docker_volume_watcher/container_notifier.py index 4beffae..7090ee5 100644 --- a/docker_volume_watcher/container_notifier.py +++ b/docker_volume_watcher/container_notifier.py @@ -13,19 +13,19 @@ import time -def debounce(s): +def debounce(bounce_delay): """Decorator ensures function that can only be called once every `s` seconds. """ - def decorate(f): - t = None + def decorate(my_function): + old_time = {'value' : None } def wrapped(*args, **kwargs): - nonlocal t - t_ = time.time() - if t is None or t_ - t >= s: - result = f(*args, **kwargs) - t = time.time() + new_time = time.time() + if old_time['value'] is None or new_time - old_time['value'] >= bounce_delay: + result = my_function(*args, **kwargs) + old_time['value'] = time.time() return result + return None return wrapped return decorate From 09756b3d892da3476b758765bdb9b5199af1c2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Thu, 13 Dec 2018 23:37:27 +0100 Subject: [PATCH 3/4] added some comments --- docker_volume_watcher/container_notifier.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/docker_volume_watcher/container_notifier.py b/docker_volume_watcher/container_notifier.py index 7090ee5..eedc56e 100644 --- a/docker_volume_watcher/container_notifier.py +++ b/docker_volume_watcher/container_notifier.py @@ -5,19 +5,29 @@ import logging from os.path import relpath import posixpath +import time import docker from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler -import time - - def debounce(bounce_delay): - """Decorator ensures function that can only be called once every `s` seconds. """ + define debounce method annotation + + Args: + bounce_delay (int): the minimum delay in seconds between two function calls + """ + def decorate(my_function): - old_time = {'value' : None } + """ + Decorator ensures function that can only be called once every `bounce_delay` seconds. + + Args: + my_function (function): the function to debounce + """ + + old_time = {'value' : None} def wrapped(*args, **kwargs): new_time = time.time() From a0e202ab3c7d2779f6fbd769f2f30a2bd78ade45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Thu, 13 Dec 2018 23:42:36 +0100 Subject: [PATCH 4/4] added some comments --- docker_volume_watcher/container_notifier.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker_volume_watcher/container_notifier.py b/docker_volume_watcher/container_notifier.py index eedc56e..00c8376 100644 --- a/docker_volume_watcher/container_notifier.py +++ b/docker_volume_watcher/container_notifier.py @@ -30,6 +30,9 @@ def decorate(my_function): old_time = {'value' : None} def wrapped(*args, **kwargs): + """ + internal function + """ new_time = time.time() if old_time['value'] is None or new_time - old_time['value'] >= bounce_delay: result = my_function(*args, **kwargs)