-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_heartbeat.py
100 lines (79 loc) · 2.84 KB
/
check_heartbeat.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import configparser
import datetime
import os
from subprocess import Popen, PIPE
import sys
import time
HOMEDIR = os.path.expanduser("~")
APPDIR = os.path.join(HOMEDIR, "projects/photoviewer")
CONFIG_FILE = os.path.join(APPDIR, "photo.cfg")
HEARTBEAT_FLAG_FILE = os.path.join(APPDIR, "HEARTBEAT")
NO_FILE_FLAG = -1
PARSER = configparser.ConfigParser()
if sys.platform == "darwin":
RESTART_COMMAND = (
f"cd {APPDIR}; kill -9 `cat photo.pid`; source {HOMEDIR}/venvs/viewer/bin/activate; "
f"python photo.py & ; rm -f {HEARTBEAT_FLAG_FILE}"
)
else:
RESTART_COMMAND = "sudo systemctl restart photoviewer.service"
def runproc(cmd, wait=True):
if wait:
kwargs = dict(stdin=PIPE, stdout=PIPE, stderr=PIPE)
else:
kwargs = dict(stdin=None, stdout=None, stderr=None)
proc = Popen([cmd], shell=True, close_fds=True, **kwargs)
if wait:
stdout_text, stderr_text = proc.communicate()
return stdout_text.decode("utf-8"), stderr_text.decode("utf-8")
def safe_get(section, option, default=None):
try:
return PARSER.get(section, option)
except (configparser.NoSectionError, configparser.NoOptionError):
return default
def normalize_interval(time_, units):
unit_key = units[0].lower()
factor = {"s": 1, "m": 60, "h": 3600, "d": 86400}.get(unit_key, 1)
return time_ * factor
def get_interval():
try:
PARSER.read(CONFIG_FILE)
except configparser.MissingSectionHeaderError as e:
# The file exists, but doesn't have the correct format.
raise Exception("Invalid Configuration File")
# How often the image is changed
interval_time = int(safe_get("frame", "interval_time", 60))
# Units of time for the image change interval
interval_units = safe_get("frame", "interval_units", "minutes")
interval = normalize_interval(interval_time, interval_units)
return interval
def since_heartbeat():
if not os.path.exists(HEARTBEAT_FLAG_FILE):
return NO_FILE_FLAG
info = os.stat(HEARTBEAT_FLAG_FILE)
local_time = time.localtime(info.st_atime)
last_access = time.mktime(local_time)
return time.time() - last_access
def clear_heartbeat_flag():
if os.path.exists(HEARTBEAT_FLAG_FILE):
os.unlink(HEARTBEAT_FLAG_FILE)
def restart():
runproc(RESTART_COMMAND)
clear_heartbeat_flag()
def printit(txt):
tm = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("/home/pi/projects/photoviewer/log/heartbeat.log", "a") as ff:
ff.write(f"{tm} {txt}\n")
def main():
elapsed = since_heartbeat()
if elapsed == NO_FILE_FLAG:
printit("Cool")
return
interval = get_interval()
if elapsed > (1.5 * interval):
printit("RESTARTING!")
restart()
else:
printit(f"Elapsed = {elapsed}, Interval = {interval}")
if __name__ == "__main__":
main()