2020-01-21 06:35:58 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
|
|
|
|
from celery import Celery
|
|
|
|
from celery.signals import task_failure
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
app = Celery("dmoj")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
from django.conf import settings # noqa: E402, I202, django must be imported here
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
app.config_from_object(settings, namespace="CELERY")
|
|
|
|
|
|
|
|
if hasattr(settings, "CELERY_BROKER_URL_SECRET"):
|
2020-01-21 06:35:58 +00:00
|
|
|
app.conf.broker_url = settings.CELERY_BROKER_URL_SECRET
|
2022-05-14 17:57:27 +00:00
|
|
|
if hasattr(settings, "CELERY_RESULT_BACKEND_SECRET"):
|
2020-01-21 06:35:58 +00:00
|
|
|
app.conf.result_backend = settings.CELERY_RESULT_BACKEND_SECRET
|
|
|
|
|
|
|
|
# Load task modules from all registered Django app configs.
|
|
|
|
app.autodiscover_tasks()
|
|
|
|
|
|
|
|
# Logger to enable errors be reported.
|
2022-05-14 17:57:27 +00:00
|
|
|
logger = logging.getLogger("judge.celery")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task_failure.connect()
|
|
|
|
def celery_failure_log(sender, task_id, exception, traceback, *args, **kwargs):
|
2022-05-14 17:57:27 +00:00
|
|
|
logger.error(
|
|
|
|
"Celery Task %s: %s on %s",
|
|
|
|
sender.name,
|
|
|
|
task_id,
|
|
|
|
socket.gethostname(), # noqa: G201
|
|
|
|
exc_info=(type(exception), exception, traceback),
|
|
|
|
)
|