forked from cpfair/tapiriik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_watchdog.py
30 lines (24 loc) · 870 Bytes
/
sync_watchdog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from tapiriik.database import db
from tapiriik.sync import SyncStep
import os
import signal
import socket
from datetime import timedelta, datetime
for worker in db.sync_workers.find({"Host": socket.gethostname()}):
# Does the process still exist?
alive = True
try:
os.kill(worker["Process"], 0)
except os.error:
alive = False
# Has it been stalled for too long?
if worker["State"] == SyncStep.List:
timeout = timedelta(minutes=45) # This can take a loooooooong time
else:
timeout = timedelta(minutes=10) # But everything else shouldn't
if alive and worker["Heartbeat"] < datetime.utcnow() - timeout:
os.kill(worker["Process"], signal.SIGKILL)
alive = False
# Clear it from the database if it's not alive.
if not alive:
db.sync_workers.remove({"_id": worker["_id"]})