The platform for continuously improving AI agents.
import logging
import logging.config
import os
from celery import Celery
from celery import Task as CeleryTask
from celery.signals import setup_logging
from celery.worker.request import Request
from overbae.core import telemetry
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "overbae.settings")
class TaskRequest(Request):
def on_timeout(self, soft, timeout):
super().on_timeout(soft, timeout)
if not soft:
# The parent kills the child process, so the task itself never sees this.
telemetry.report_failure(
"Celery task exceeded its hard time limit",
task=self.name,
task_id=self.id,
time_limit_s=timeout,
queue=(self.delivery_info or {}).get("routing_key"),
)
class Task(CeleryTask):
Request = TaskRequest
def __call__(self, *args, **kwargs):
# New Relic opens the task's transaction around this call.
with telemetry.scope():
telemetry.annotate(
task=self.name,
queue=(self.request.delivery_info or {}).get("routing_key"),
retries=self.request.retries,
**{
name: value
for name, value in kwargs.items()
if name.endswith("_id") and isinstance(value, str | int)
},
)
return super().__call__(*args, **kwargs)
app = Celery("overbae", task_cls=Task)
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks(["overbae.tasks"])
@setup_logging.connect
def _configure_celery_logging(**_kwargs: object) -> None:
"""Use Django's LOGGING config: Celery's own formatters emit multi-line
output that CloudWatch splits into separate log entries.
"""
from django.conf import settings
logging.config.dictConfig(settings.LOGGING)
def get_celery_app() -> Celery:
return app