Skip to content

Commit

Permalink
🐳 Added code to provide health check for Celery.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeribekker committed Jul 18, 2024
1 parent 1727af9 commit 68003ba
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/open_inwoner/celery.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

from django.conf import settings

from celery import Celery
from celery.signals import setup_logging
from celery import Celery, bootsteps
from celery.signals import setup_logging, worker_ready, worker_shutdown

from .setup import setup_env

Expand All @@ -26,3 +28,45 @@ def config_loggers(*args, **kwargs):
from logging.config import dictConfig

dictConfig(settings.LOGGING)


HEARTBEAT_FILE = Path(settings.BASE_DIR) / "tmp" / "celery_worker_heartbeat"
READINESS_FILE = Path(settings.BASE_DIR) / "tmp" / "celery_worker_ready"


#
# Utilities for checking the health of celery workers
#
class LivenessProbe(bootsteps.StartStopStep):
requires = {"celery.worker.components:Timer"}

def __init__(self, worker, **kwargs):
self.requests = []
self.tref = None

def start(self, worker):
self.tref = worker.timer.call_repeatedly(
10.0,
self.update_heartbeat_file,
(worker,),
priority=10,
)

def stop(self, worker):
HEARTBEAT_FILE.unlink(missing_ok=True)

def update_heartbeat_file(self, worker):
HEARTBEAT_FILE.touch()


@worker_ready.connect
def worker_ready(**_):
READINESS_FILE.touch()


@worker_shutdown.connect
def worker_shutdown(**_):
READINESS_FILE.unlink(missing_ok=True)


app.steps["worker"].add(LivenessProbe)

0 comments on commit 68003ba

Please sign in to comment.