Skip to content

Commit

Permalink
timeout heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
MateoLostanlen committed Jul 30, 2024
1 parent 4742146 commit ca8952b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pyroengine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import shutil
import signal
import time
from collections import deque
from datetime import datetime, timedelta, timezone
Expand All @@ -30,6 +31,23 @@
logging.basicConfig(format="%(asctime)s | %(levelname)s: %(message)s", level=logging.INFO, force=True)


def handler(signum, frame):
raise TimeoutError("Heartbeat check timed out")

Check warning on line 35 in pyroengine/engine.py

View check run for this annotation

Codecov / codecov/patch

pyroengine/engine.py#L35

Added line #L35 was not covered by tests


def heartbeat_with_timeout(api_instance, cam_id, timeout=1):
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
try:
api_instance.heartbeat(cam_id)
except TimeoutError:
logging.warning(f"Heartbeat check timed out for {cam_id}")
except ConnectionError:
logging.warning(f"Unable to reach the pyro-api with {cam_id}")

Check warning on line 46 in pyroengine/engine.py

View check run for this annotation

Codecov / codecov/patch

pyroengine/engine.py#L43-L46

Added lines #L43 - L46 were not covered by tests
finally:
signal.alarm(0)


class Engine:
"""This implements an object to manage predictions and API interactions for wildfire alerts.
Expand Down Expand Up @@ -253,10 +271,7 @@ def predict(self, frame: Image.Image, cam_id: Optional[str] = None) -> float:

# Heartbeat
if len(self.api_client) > 0 and isinstance(cam_id, str):
try:
self.heartbeat(cam_id)
except ConnectionError:
logging.warning(f"Unable to reach the pyro-api with {cam_id}")
heartbeat_with_timeout(self, cam_id, timeout=1)

cam_key = cam_id or "-1"
# Reduce image size to save bandwidth
Expand Down

0 comments on commit ca8952b

Please sign in to comment.