Skip to content

Commit

Permalink
refactor: health check code to improve readability and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyvan committed Mar 13, 2024
1 parent d9ebff9 commit f51c46e
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions example_publisher/api/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,22 @@


class HTTPRequestHandler(BaseHTTPRequestHandler):
publisher: Publisher = None
config: Config = None

def __init__(self, *args, **kwargs):
self.test_priod_secs: int = (
HTTPRequestHandler.config.health_check_test_period_secs
)
self.last_successful_update: float = (
HTTPRequestHandler.publisher.last_successful_update
)
super().__init__(*args, **kwargs)
publisher: Publisher
config: Config

def is_healthy(self):
last_successful_update = HTTPRequestHandler.publisher.last_successful_update
return (
self.last_successful_update is not None
and time.time() - self.last_successful_update < self.test_priod_secs
last_successful_update is not None
and time.time() - last_successful_update
< HTTPRequestHandler.config.health_check_test_period_secs
)

def do_GET(self):
healthy = self.is_healthy()
data = {
"status": "ok" if healthy else "error",
"last_successful_update": self.last_successful_update,
"last_successful_update": HTTPRequestHandler.publisher.last_successful_update,
}

self.send_response(200 if healthy else 503)
Expand Down

0 comments on commit f51c46e

Please sign in to comment.