-
Notifications
You must be signed in to change notification settings - Fork 11
Docker healthcheck and auto-restart for Celery workers #1024
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Celery Worker Healthcheck Script | ||
| # | ||
| # This script checks if the Celery worker process is running and responsive. | ||
| # It uses two checks: | ||
| # 1. Process check - is celery worker process running? | ||
| # 2. Redis connectivity - can we connect to the broker? | ||
| # | ||
| # When used with the autoheal container, unhealthy workers will be | ||
| # automatically restarted. | ||
|
|
||
| set -e | ||
|
|
||
| # Check 1: Is the celery worker process running? | ||
| if ! pgrep -f "celery.*worker" > /dev/null 2>&1; then | ||
| echo "ERROR: Celery worker process not found" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Check 2: Can we connect to Redis (the broker)? | ||
| # Use redis-cli if available, otherwise skip | ||
| if command -v redis-cli > /dev/null 2>&1; then | ||
| if ! redis-cli -h ${CELERY_BROKER_URL:-redis} ping > /dev/null 2>&1; then | ||
| echo "ERROR: Cannot connect to Redis broker" >&2 | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # All checks passed | ||
| exit 0 | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||||||||||||||||||||||||||||||||
| #!/bin/bash | ||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||
| # Celery Worker Healthcheck Script (Production) | ||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||
| # This script checks if the Celery worker process is running and responsive. | ||||||||||||||||||||||||||||||||||||
| # It uses two checks: | ||||||||||||||||||||||||||||||||||||
| # 1. Process check - is celery worker process running? | ||||||||||||||||||||||||||||||||||||
| # 2. Redis connectivity - can we connect to the broker? | ||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||
| # When used with the autoheal container, unhealthy workers will be | ||||||||||||||||||||||||||||||||||||
| # automatically restarted. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| set -e | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Check 1: Is the celery worker process running? | ||||||||||||||||||||||||||||||||||||
| if ! pgrep -f "celery.*worker" > /dev/null 2>&1; then | ||||||||||||||||||||||||||||||||||||
| echo "ERROR: Celery worker process not found" >&2 | ||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Check 2: Can we connect to Redis (the broker)? | ||||||||||||||||||||||||||||||||||||
| # Use redis-cli if available, otherwise skip | ||||||||||||||||||||||||||||||||||||
| if command -v redis-cli > /dev/null 2>&1; then | ||||||||||||||||||||||||||||||||||||
| if ! redis-cli -h ${CELERY_BROKER_URL:-redis} ping > /dev/null 2>&1; then | ||||||||||||||||||||||||||||||||||||
| echo "ERROR: Cannot connect to Redis broker" >&2 | ||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix Redis broker URL parsing. Line 24 uses Apply this diff to properly extract the hostname from the broker URL: # Check 2: Can we connect to Redis (the broker)?
# Use redis-cli if available, otherwise skip
if command -v redis-cli > /dev/null 2>&1; then
- if ! redis-cli -h ${CELERY_BROKER_URL:-redis} ping > /dev/null 2>&1; then
+ REDIS_HOST=$(echo "${CELERY_BROKER_URL:-redis://redis:6379}" | sed -E 's|^redis://([^:/@]+).*|\1|')
+ if ! timeout 5 redis-cli -h "${REDIS_HOST}" ping > /dev/null 2>&1; then
echo "ERROR: Cannot connect to Redis broker" >&2
exit 1
fi
fiThis also adds a 5-second timeout to prevent the healthcheck from hanging indefinitely if Redis is unresponsive. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # All checks passed | ||||||||||||||||||||||||||||||||||||
| exit 0 | ||||||||||||||||||||||||||||||||||||
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.
Fix Redis broker URL parsing.
Line 24 uses
${CELERY_BROKER_URL:-redis}directly as the hostname argument toredis-cli, butCELERY_BROKER_URLis typically a full URL likeredis://redis:6379/0, not just a hostname. This will cause the healthcheck to fail when the environment variable is set.Apply this diff to properly extract the hostname from the broker URL:
This also adds a 5-second timeout to prevent the healthcheck from hanging indefinitely if Redis is unresponsive.
🤖 Prompt for AI Agents