|
1 | 1 | """Enables heartbeats.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import time |
4 | 5 | import threading |
5 | 6 |
|
6 | 7 | import requests |
|
12 | 13 | _session.headers.update({"Authorization": f"{os.environ.get('RUNPOD_AI_API_KEY')}"}) |
13 | 14 |
|
14 | 15 |
|
15 | | -def _send_ping(ping_params=None): |
16 | | - if PING_URL not in [None, 'PING_URL_NOT_SET']: |
17 | | - try: |
18 | | - result = _session.get( |
19 | | - PING_URL, |
20 | | - params=ping_params, |
21 | | - timeout=int(PING_INTERVAL / 1000) |
22 | | - ) |
23 | | - |
24 | | - log.info(f"Heartbeat Sent URL: {PING_URL} Status: {result.status_code}") |
25 | | - log.info(f"Heartbeat Sent Interval: {PING_INTERVAL}ms Params: {ping_params}") |
26 | | - |
27 | | - except Exception as err: # pylint: disable=broad-except |
28 | | - log.error(f"Heartbeat Failed URL: {PING_URL} Params: {ping_params}") |
29 | | - log.error(f"Heartbeat Fail Error: {err}") |
30 | | - |
31 | | - |
32 | | -def start_ping(): |
33 | | - """ |
34 | | - Pings the heartbeat endpoint at the specified interval. |
35 | | - """ |
36 | | - job_id = get_current_job_id() |
37 | | - |
38 | | - ping_params = { |
39 | | - 'job_id': job_id, |
40 | | - } if job_id is not None else None |
41 | | - |
42 | | - _send_ping(ping_params) |
43 | | - |
44 | | - log.debug(f"Scheduling next heartbeat in {PING_INTERVAL}ms") |
45 | | - heartbeat_thread = threading.Timer(int(PING_INTERVAL / 1000), start_ping) |
46 | | - heartbeat_thread.daemon = True |
47 | | - heartbeat_thread.start() |
| 16 | +class HeartbeatSender: |
| 17 | + ''' Sends heartbeats to the Runpod server. ''' |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + self._thread = threading.Thread(target=self._run, daemon=True) |
| 21 | + |
| 22 | + def start_ping(self): |
| 23 | + ''' |
| 24 | + Starts the heartbeat thread. |
| 25 | + ''' |
| 26 | + self._thread.start() |
| 27 | + |
| 28 | + def _run(self): |
| 29 | + ''' |
| 30 | + Sends heartbeats to the Runpod server. |
| 31 | + ''' |
| 32 | + while True: |
| 33 | + self._send_ping() |
| 34 | + time.sleep(int(PING_INTERVAL / 1000)) |
| 35 | + |
| 36 | + def _send_ping(self): |
| 37 | + ''' |
| 38 | + Sends a heartbeat to the Runpod server. |
| 39 | + ''' |
| 40 | + job_id = get_current_job_id() |
| 41 | + |
| 42 | + ping_params = { |
| 43 | + 'job_id': job_id, |
| 44 | + } if job_id is not None else None |
| 45 | + |
| 46 | + if PING_URL not in [None, 'PING_URL_NOT_SET']: |
| 47 | + try: |
| 48 | + result = _session.get( |
| 49 | + PING_URL, |
| 50 | + params=ping_params, |
| 51 | + timeout=int(PING_INTERVAL / 1000) |
| 52 | + ) |
| 53 | + |
| 54 | + log.info(f"Heartbeat Sent URL: {PING_URL} Status: {result.status_code}") |
| 55 | + log.info(f"Heartbeat Sent Interval: {PING_INTERVAL}ms Params: {ping_params}") |
| 56 | + |
| 57 | + except Exception as err: # pylint: disable=broad-except |
| 58 | + log.error(f"Heartbeat Failed URL: {PING_URL} Params: {ping_params}") |
| 59 | + log.error(f"Heartbeat Fail Error: {err}") |
0 commit comments