from __future__ import absolute_import, unicode_literals
import os

import django
from django.core.checks import run_checks
from django.core.exceptions import AppRegistryNotReady

from celery import Celery

from manage import init_hq_python_path, run_patches

init_hq_python_path()
run_patches()

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

app = Celery()
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

django.setup()
try:
    run_checks()
except AppRegistryNotReady:
    pass

@@ -49,16 +49,33 @@
 literals
+%0A%0Aimport datetime
 %0Aimport 
@@ -92,16 +92,111 @@
  django%0A
+from celery.signals import after_task_publish, task_prerun%0Afrom django.core.cache import cache%0A
 from dja
@@ -636,16 +636,16 @@
 tReady:%0A
-
     pass
@@ -645,8 +645,943 @@
    pass%0A
+%0A%0A@after_task_publish.connect%0Adef celery_add_time_sent(headers=None, body=None, **kwargs):%0A    info = headers if 'task' in headers else body%0A    task_id = info%5B'id'%5D%0A    cache.set('task.%7B%7D.time_sent'.format(task_id), datetime.datetime.utcnow())%0A%0A%0A@task_prerun.connect%0Adef celery_record_time_to_start(task_id=None, task=None, **kwargs):%0A    from corehq.util.datadog.gauges import datadog_gauge, datadog_counter%0A    time_sent = cache.get('task.%7B%7D.time_sent'.format(task_id))%0A%0A    if time_sent:%0A        time_to_start = (datetime.datetime.utcnow() - time_sent).total_seconds()%0A%0A        datadog_gauge('commcare.celery.task.time_to_start', time_to_start, tags=%7B%0A            'celery_task_name': task.name,%0A            'celery_queue': task.queue,%0A        %7D)%0A    else:%0A        datadog_counter('commcare.celery.task.time_to_start_unavailable', tags=%7B%0A            'celery_task_name': task.name,%0A            'celery_queue': task.queue,%0A        %7D)%0A

Collect celery time-to-start metric