Skip to content

Commit 6b73791

Browse files
committed
add health check to api-server celery worker
1 parent bc263b0 commit 6b73791

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

services/api-server/docker/healthcheck.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,49 @@
1818
"""
1919

2020
import os
21+
import subprocess
2122
import sys
2223
from urllib.request import urlopen
2324

25+
from simcore_service_api_server.core.settings import ApplicationSettings
26+
2427
SUCCESS, UNHEALTHY = 0, 1
2528

2629
# Disabled if boots with debugger
2730
ok = os.environ.get("SC_BOOT_MODE", "").lower() == "debug"
2831

32+
app_settings = ApplicationSettings.create_from_envs()
33+
34+
35+
def _is_celery_worker_healthy():
36+
assert app_settings.API_SERVER_CELERY
37+
broker_url = app_settings.API_SERVER_CELERY.CELERY_RABBIT_BROKER.dsn
38+
39+
try:
40+
result = subprocess.run(
41+
[
42+
"celery",
43+
"--broker",
44+
broker_url,
45+
"inspect",
46+
"ping",
47+
"--destination",
48+
"celery@" + os.getenv("API_SERVER_WORKER_NAME", "worker"),
49+
],
50+
capture_output=True,
51+
text=True,
52+
check=True,
53+
)
54+
return "pong" in result.stdout
55+
except subprocess.CalledProcessError:
56+
return False
57+
58+
2959
# Queries host
3060
# pylint: disable=consider-using-with
3161
ok = (
3262
ok
63+
or (app_settings.API_SERVER_WORKER_MODE and _is_celery_worker_healthy())
3364
or urlopen(
3465
"{host}{baseurl}".format(
3566
host=sys.argv[1], baseurl=os.environ.get("SIMCORE_NODE_BASEPATH", "")

0 commit comments

Comments
 (0)