fix multiple events

This commit is contained in:
Snowyo 2018-08-05 00:27:22 +02:00
parent 5c79fe024a
commit 96e407a843

View File

@ -4,6 +4,7 @@ import errno
import time import time
import json import json
import docker import docker
import threading
import argparse import argparse
from argparse import ArgumentTypeError as err from argparse import ArgumentTypeError as err
from base64 import b64decode from base64 import b64decode
@ -168,6 +169,9 @@ class Handler(FileSystemEventHandler):
def __init__(self, args): def __init__(self, args):
self.args = args self.args = args
self.isWaiting = False
self.timer = threading.Timer(0.5, self.doTheWork)
self.lock = threading.Lock()
def on_created(self, event): def on_created(self, event):
self.handle(event) self.handle(event)
@ -181,10 +185,19 @@ class Handler(FileSystemEventHandler):
if not event.is_directory and event.src_path.endswith(str(self.args.certificate)): if not event.is_directory and event.src_path.endswith(str(self.args.certificate)):
print('Certificates changed') print('Certificates changed')
domains = createCerts( with self.lock:
event.src_path, self.args.directory, self.args.flat) if not self.isWaiting:
if (self.args.restart_container): self.isWaiting = True #trigger the work just once (multiple events get fired)
restartContainerWithDomains(domains) 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__": if __name__ == "__main__":