diff --git a/src/functions/__init__.py b/src/functions/__init__.py index b80e181..b3d6455 100644 --- a/src/functions/__init__.py +++ b/src/functions/__init__.py @@ -4,6 +4,8 @@ import subprocess import shlex import time import os +import re +import time class Functions: HAPROXY_LOG="HAPROXY" @@ -105,6 +107,7 @@ class DaemonizeHAProxy: def __init__(self): self.process = None self.thread = None + self.sleep_secs = None def haproxy(self, action): if action == "start": @@ -160,6 +163,15 @@ class DaemonizeHAProxy: self.process.terminate() self.thread.terminate() + def sleep(self): + if self.sleep_secs is None: + try: + self.sleep_secs = int(os.getenv("EASYHAPROXY_REFRESH_CONF", "10")) + except ValueError: + self.sleep_secs = 10 + + time.sleep(self.sleep_secs) + class Certbot: def __init__(self, certs, email): diff --git a/src/main.py b/src/main.py index bb5305f..fe733fe 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,6 @@ from functions import Functions, DaemonizeHAProxy, Certbot, Consts from processor import ProcessorInterface import os -import time from deepdiff import DeepDiff def start(): @@ -21,18 +20,18 @@ def start(): old_haproxy = None haproxy = DaemonizeHAProxy() haproxy.haproxy("start") + haproxy.sleep() certbot = Certbot(Consts.certs_letsencrypt, os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL")) while True: - time.sleep(10) if old_haproxy is not None: old_haproxy.kill() old_haproxy = None try: old_parsed = processor_obj.get_parsed_object() processor_obj.refresh() - if DeepDiff(old_parsed, processor_obj.get_parsed_object()) != {} or not haproxy.is_alive(): + if certbot.check_certificates(letsencrypt_certs_found) or DeepDiff(old_parsed, processor_obj.get_parsed_object()) != {} or not haproxy.is_alive(): Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'New configuration found. Reloading...') Functions.log(Functions.EASYHAPROXY_LOG, Functions.TRACE, 'Object Found: %s' % (processor_obj.get_parsed_object())) processor_obj.save_config(Consts.haproxy_config) @@ -44,10 +43,11 @@ def start(): haproxy.haproxy("reload") old_haproxy.terminate() - certbot.check_certificates(letsencrypt_certs_found) except Exception as e: Functions.log(Functions.EASYHAPROXY_LOG, Functions.FATAL, "Err: %s" % (e)) + Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'Heartbeat') + haproxy.sleep()