1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Reworked supervisor

This commit is contained in:
Krateng 2020-02-28 16:29:56 +01:00
parent 55c68b21cd
commit 8793b149f5
2 changed files with 45 additions and 14 deletions

View File

@ -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)

View File

@ -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