Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debounce multiple modified events that occur at a same moment #14

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docker_volume_watcher/container_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,43 @@
import logging
from os.path import relpath
import posixpath
import time

import docker
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

def debounce(bounce_delay):
"""
define debounce method annotation

Args:
bounce_delay (int): the minimum delay in seconds between two function calls
"""

def decorate(my_function):
"""
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):
"""
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)
old_time['value'] = time.time()
return result
return None
return wrapped
return decorate

class NonZeroExitError(RuntimeError):
"""
A non-zero exit code error from the command execution in docker.
Expand Down Expand Up @@ -54,6 +86,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('\\', '/')
Expand Down