-
Notifications
You must be signed in to change notification settings - Fork 1
/
healthchecks.py
43 lines (30 loc) · 934 Bytes
/
healthchecks.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
import logging
import socket
import urllib.request
import threading
class HealthChecks:
def __init__(self, hc_uuid: str):
self.hc_uuid = hc_uuid
self.lock = threading.Lock()
def ping(self, start: bool = False) -> bool:
if self.hc_uuid is None:
return False
ping_url = f"https://hc-ping.com/{self.hc_uuid}"
if start:
ping_url += "/start"
try:
urllib.request.urlopen(ping_url, timeout=10)
return True
except socket.error as e:
logging.error("Healthchecks returned error: %s", e)
return False
def start(self) -> bool:
ping_result = self.ping(True)
if ping_result:
self.lock.acquire()
return ping_result
def stop(self) -> bool:
ping_result = self.ping()
if ping_result:
self.lock.release()
return ping_result