This repository has no description
1#!/usr/bin/env python3
2import time
3import re
4import urllib.request
5import urllib.error
6
7CONF = "/home/regent/Developer/niri/home/heartbeat.conf"
8TRIGGER = "http://localhost:3000/trigger/cron"
9DEFAULT_INTERVAL = 100 # minutes
10
11
12def read_interval() -> int:
13 try:
14 with open(CONF) as f:
15 content = f.read()
16 match = re.search(r"^\d+$", content, re.MULTILINE)
17 if match:
18 return int(match.group())
19 except OSError:
20 pass
21 return DEFAULT_INTERVAL
22
23
24def trigger() -> bool:
25 try:
26 req = urllib.request.Request(TRIGGER, method="POST")
27 with urllib.request.urlopen(req) as resp:
28 return resp.status < 400
29 except urllib.error.URLError:
30 return False
31
32
33if __name__ == "__main__":
34 print("Heartbeat started. Press Ctrl+C to stop.")
35 while True:
36 interval = read_interval()
37 success = trigger()
38 status = "OK" if success else "FAILED"
39 print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Trigger {status} — next in {interval}m")
40 time.sleep(interval * 60)