-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move celery logging to Django (#3809)
* fix: move celery logging to Django * fix: use json formatter for k8s console logs * fix: bump celery to 5.4.0
- Loading branch information
Showing
6 changed files
with
63 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import os | ||
import celery | ||
|
||
from celery import Celery | ||
from django.conf import settings | ||
|
||
|
||
# Disable celery's internal logging configuration, we set it up via Django | ||
@celery.signals.setup_logging.connect | ||
def on_setup_logging(**kwargs): | ||
pass | ||
|
||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mlarchive.settings.settings') | ||
|
||
app = Celery('mlarchive') | ||
app = celery.Celery('mlarchive') | ||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
|
||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) | ||
|
||
|
||
@app.task(bind=True) | ||
def debug_task(self): | ||
print('Request: {0!r}'.format(self.request)) | ||
|
||
|
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# settings/settings.py | ||
from .base import * | ||
|
||
# Console logs as JSON instead of plain when running in k8s | ||
LOGGING["handlers"]["console"]["formatter"] = "json" |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright The IETF Trust 2024, All Rights Reserved | ||
from pythonjsonlogger import jsonlogger | ||
import time | ||
|
||
|
||
class MailArchiveJsonFormatter(jsonlogger.JsonFormatter): | ||
converter = time.gmtime # use UTC | ||
default_msec_format = "%s.%03d" # '.' instead of ',' |
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,8 +1,34 @@ | ||
#!/bin/bash | ||
#!/bin/bash -e | ||
# | ||
# Run a celery worker | ||
# | ||
# echo "Running Mailarchive checks..." | ||
# ./backend/manage.py check | ||
|
||
celery "$@" | ||
# echo "Running Datatracker checks..." | ||
# ./ietf/manage.py check | ||
|
||
# if ! ietf/manage.py migrate --check ; then | ||
# echo "Unapplied migrations found, waiting to start..." | ||
# sleep 5 | ||
# while ! ietf/manage.py migrate --check ; do | ||
# echo "... still waiting for migrations..." | ||
# sleep 5 | ||
# done | ||
# fi | ||
|
||
echo "Starting Celery..." | ||
|
||
cleanup () { | ||
# Cleanly terminate the celery app by sending it a TERM, then waiting for it to exit. | ||
if [[ -n "${celery_pid}" ]]; then | ||
echo "Gracefully terminating celery worker. This may take a few minutes if tasks are in progress..." | ||
kill -TERM "${celery_pid}" | ||
wait "${celery_pid}" | ||
fi | ||
} | ||
|
||
trap 'trap "" TERM; cleanup' TERM | ||
|
||
# start celery in the background so we can trap the TERM signal | ||
celery "$@" & | ||
celery_pid=$! | ||
wait "${celery_pid}" |
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