Skip to content

Commit

Permalink
Add check running to Sanic
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamalama committed Aug 7, 2023
1 parent a60f704 commit 2b46490
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions src/dockerflow/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from sanic import response

from dockerflow.checks import iscoroutinefunction_or_partial, run_checks_async

from .. import version
from . import checks

Expand Down Expand Up @@ -208,19 +210,6 @@ async def _lbheartbeat_view(self, request):
"""
return response.raw(b"", 200)

async def _heartbeat_check_detail(self, check):
result = check()
if isawaitable(result):
result = await result
errors = [e for e in result if e.id not in self.silenced_checks]
level = max([0] + [e.level for e in errors])

return {
"status": checks.level_to_text(level),
"level": level,
"messages": {e.id: e.msg for e in errors},
}

async def _heartbeat_view(self, request):
"""
Runs all the registered checks and returns a JSON response with either
Expand All @@ -229,24 +218,16 @@ async def _heartbeat_view(self, request):
Any check that returns a warning or worse (error, critical) will
return a 500 response.
"""
details = {}
statuses = {}
level = 0

for name, check in self.checks.items():
detail = await self._heartbeat_check_detail(check)
statuses[name] = detail["status"]
level = max(level, detail["level"])
if detail["level"] > 0:
details[name] = detail
check_results = await run_checks_async(self.checks.items())

payload = {
"status": checks.level_to_text(level),
"checks": statuses,
"details": details,
"status": checks.level_to_text(check_results.level),
"checks": check_results.statuses,
"details": check_results.details,
}

if level < checks.ERROR:
if check_results.level < checks.ERROR:
status_code = 200
else:
status_code = 500
Expand Down Expand Up @@ -321,10 +302,21 @@ async def storage_reachable():

self.logger.info("Registered Dockerflow check %s", name)

@functools.wraps(func)
def decorated_function(*args, **kwargs):
self.logger.info("Called Dockerflow check %s", name)
return func(*args, **kwargs)
if iscoroutinefunction_or_partial(func):

@functools.wraps(func)
async def decorated_function_asyc(*args, **kwargs):
self.logger.info("Called Dockerflow check %s", name)
return await func(*args, **kwargs)

self.checks[name] = decorated_function_asyc
return decorated_function_asyc
else:

@functools.wraps(func)
def decorated_function(*args, **kwargs):
self.logger.info("Called Dockerflow check %s", name)
return func(*args, **kwargs)

self.checks[name] = decorated_function
return decorated_function
self.checks[name] = decorated_function
return decorated_function

0 comments on commit 2b46490

Please sign in to comment.