diff --git a/maloja/__init__.py b/maloja/__init__.py index 63658f7..e87142e 100644 --- a/maloja/__init__.py +++ b/maloja/__init__.py @@ -7,7 +7,7 @@ author = { "email":"maloja@krateng.dev", "github": "krateng" } -version = 2,2,4 +version = 2,3,0 versionstr = ".".join(str(n) for n in version) diff --git a/maloja/supervisor.py b/maloja/supervisor.py index a1e5cfe..ae8d4f6 100644 --- a/maloja/supervisor.py +++ b/maloja/supervisor.py @@ -5,28 +5,59 @@ import subprocess import time import setproctitle import signal +from datetime import datetime from doreah.logging import log from doreah.settings import get_settings setproctitle.setproctitle("maloja_supervisor") +lastrestart = () + +def get_pid(): + try: + output = subprocess.check_output(["pidof","Maloja"]) + return int(output) + except: + return None + +def update(): + log("Updating...",module="supervisor") + try: + os.system("pip3 install maloja --upgrade --no-cache-dir") + except: + log("Could not update.",module="supervisor") + +def start(): + try: + p = subprocess.Popen(["python3","-m","maloja.server"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + + except e: + log("Error starting Maloja: " + str(e),module="supervisor") + + while True: time.sleep(60) + + now = datetime.now() + today = now.year, now.month, now.day + + pid = get_pid() - try: - output = subprocess.check_output(["pidof","Maloja"]) - pid = int(output) - except: + if pid: + + restart = get_settings("DAILY_RESTART") + if restart: + if today != lastrestart: + if now.hour == restart: + os.kill(pid,signal.SIGTERM) + start() + lastrestart = today + + else: log("Maloja is not running, restarting...",module="supervisor") if get_settings("UPDATE_AFTER_CRASH"): - log("Updating first...",module="supervisor") - try: - os.system("pip3 install maloja --upgrade --no-cache-dir") - except: - log("Could not update.",module="supervisor") - try: - p = subprocess.Popen(["python3","-m","maloja.server"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) - except e: - log("Error starting Maloja: " + str(e),module="supervisor") + update() + start() + lastrestart = today