-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.py
33 lines (26 loc) · 1012 Bytes
/
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
# This module provides a hreatbeat mechanism to help investigate when
# the controller went down, perhaps due to power failure. It does
# this by periodically touching a file named "HEARTBEAT".
import os
import datetime
import logging
import actions
import config
from schedule import Every, Event, Scheduler
logging.getLogger(__name__).propagate = True
HEARTBEAT_FILE = "HEARTBEAT"
HEARTBEAT_INTERVAL = datetime.timedelta(seconds=5*60)
def still_alive():
'''Touch HEARTBEAT_FILE, updating its accessed and modified times to now.'''
os.utime(HEARTBEAT_FILE)
def _do_onLoggingStarted_first_heartbeat():
# Check time of heartbeat file before the Event is scheduled.
try:
stat = os.stat(HEARTBEAT_FILE)
logging.getLogger(__name__).info(
'Last heartbeat before startup: %s' %
datetime.datetime.fromtimestamp(stat.st_mtime).
strftime(config.TIME_FORMAT))
except FileNotFoundError:
pass
Event(still_alive, Every(HEARTBEAT_INTERVAL), "Still alive?").schedule()