-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🩺 add celery healthcheck #427
Merged
sjoerdie
merged 2 commits into
maykinmedia:master
from
sjoerdie:feature/celery-health-check
Jul 29, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,15 @@ services: | |
- DEMO_TOKEN=demo-random-string | ||
- DEMO_PERSON=Demo | ||
- [email protected] | ||
healthcheck: | ||
test: ["CMD", "python", "-c", "import requests; exit(requests.head('http://localhost:8000/admin/').status_code not in [200, 302])"] | ||
interval: 30s | ||
timeout: 5s | ||
retries: 3 | ||
# This should allow for enough time for migrations to run before the max | ||
# retries have passed. This healthcheck in turn allows other containers | ||
# to wait for the database migrations. | ||
start_period: 30s | ||
ports: | ||
- 8000:8000 | ||
depends_on: | ||
|
@@ -59,9 +68,15 @@ services: | |
build: *web_build | ||
environment: *web_env | ||
command: /celery_worker.sh | ||
healthcheck: | ||
test: ["CMD", "python", "/app/bin/check_celery_worker_liveness.py"] | ||
interval: 30s | ||
timeout: 5s | ||
retries: 3 | ||
start_period: 10s | ||
depends_on: | ||
- db | ||
- redis | ||
web: | ||
condition: service_healthy | ||
volumes: *web_volumes | ||
|
||
celery-flower: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,72 @@ | ||
from celery import Celery | ||
from pathlib import Path | ||
|
||
from django.conf import settings | ||
|
||
from celery import Celery, bootsteps | ||
from celery.signals import setup_logging, worker_ready, worker_shutdown | ||
|
||
from .setup import setup_env | ||
|
||
setup_env() | ||
|
||
app = Celery("objects") | ||
app.config_from_object("django.conf:settings", namespace="CELERY") | ||
app.conf.ONCE = { | ||
"backend": "celery_once.backends.Redis", | ||
"settings": { | ||
"url": settings.CELERY_BROKER_URL, | ||
"default_timeout": 60 * 60, # one hour | ||
}, | ||
} | ||
|
||
app.autodiscover_tasks() | ||
|
||
|
||
# Use django's logging settings as these are reset by Celery by default | ||
@setup_logging.connect() | ||
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this check can be moved to web-init service, because "web" service depends on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better leave it here, the init container will go down anyway when its done, also celery depends on the "web" service health check: