-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.py
51 lines (39 loc) · 1.5 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import threading
import time
from os import kill
from signal import SIGKILL
from refill_log import logger
class WatchDog(threading.Thread):
def __init__(self, leds):
super().__init__()
self.Leds = leds
self.MyThreads = {}
self.Lock = threading.Lock()
self.Interval = 0
self.Active = True
def calm(self, thread_to_watch, duration):
with self.Lock:
self.MyThreads.update({thread_to_watch: duration})
def shutdown(self):
for i in range(10):
activethreads = threading.enumerate()
for t in activethreads:
try:
t.stop()
except AttributeError:
pass
time.sleep(0.3)
def run(self, interval=3):
self.Interval = interval
while self.Active:
# break # fixme watchdog deactivated
time.sleep(self.Interval)
with self.Lock:
for thread_to_watch in self.MyThreads:
timeLeft = self.MyThreads[thread_to_watch] - self.Interval
self.MyThreads.update({thread_to_watch: timeLeft})
logger.debug("Watchdog {} : {}".format(thread_to_watch, str(timeLeft)))
if self.MyThreads[thread_to_watch] < 0:
logger.critical("Watch dog is missing {}. Restarting refiller service.".format(thread_to_watch))
self.Active = False
kill(0, SIGKILL)