Skip to content

Commit

Permalink
fix multiple events
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowMB committed Aug 4, 2018
1 parent 5c79fe0 commit 96e407a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import json
import docker
import threading
import argparse
from argparse import ArgumentTypeError as err
from base64 import b64decode
Expand Down Expand Up @@ -168,6 +169,9 @@ class Handler(FileSystemEventHandler):

def __init__(self, args):
self.args = args
self.isWaiting = False
self.timer = threading.Timer(0.5, self.doTheWork)
self.lock = threading.Lock()

def on_created(self, event):
self.handle(event)
Expand All @@ -181,10 +185,19 @@ def handle(self, event):
if not event.is_directory and event.src_path.endswith(str(self.args.certificate)):
print('Certificates changed')

domains = createCerts(
event.src_path, self.args.directory, self.args.flat)
if (self.args.restart_container):
restartContainerWithDomains(domains)
with self.lock:
if not self.isWaiting:
self.isWaiting = True #trigger the work just once (multiple events get fired)
self.timer = threading.Timer(0.5, self.doTheWork)
self.timer.start()

def doTheWork(self):
domains = createCerts(self.args.certificate, self.args.directory, self.args.flat)
if (self.args.restart_container):
restartContainerWithDomains(domains)

with self.lock:
self.isWaiting = False


if __name__ == "__main__":
Expand Down

0 comments on commit 96e407a

Please sign in to comment.