0

I have a signals.py inside my project/jobs app. This is the file:

from celery.signals import task_postrun, after_task_publish
from project.jobs.models import JobModel, JOB_STATUS_CHOICES

@after_task_publish.connect
def task_sent_handler(sender=None, headers=None, body=None, **kwargs):
    info = headers if 'task' in headers else body
    print('after_task_publish for task id {info[id]}'.format(info=info,))

@task_postrun.connect
def task_postrun_handler(task_id=None, **kwargs):
    print('CONNECT', task_id)
    JobModel.objects.filter(task_id=task_id).update(status=JOB_STATUS_CHOICES.SUCCESS)

task_sent_handler is fired while task_postrun_handler is not.

signals.py is imported by my AppConfig, inside the ready() function.

This is my celery config:

CELERY_BROKER_URL = env('CELERY_BROKER_URL')
CELERY_SEND_EVENTS = True
CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

Any suggestions as to why task_postrun may not be fired?

They are both events under the Task category, so I can't see why one works and the other does not.

Giannis
  • 5,286
  • 15
  • 58
  • 113
  • 1
    Yes, you generally have to make sure 'by hand' your signals get connected. See e.g. https://stackoverflow.com/questions/7115097/the-right-place-to-keep-my-signals-py-files-in-django/21612050#21612050 – user2390182 Aug 10 '18 at 15:38
  • In which module do you import the signals? This module might be only executed in the process **sending** the task (thus emitting the `after_task_publish` signal), but not in the process **running** the task (emitting the `task_postrun` signal). – user2390182 Aug 10 '18 at 15:42
  • @schwobaseggl I was importing signals inside `models.py`, and later moved it in my `celery.py` file. I am not doing anything with it other than import. – Giannis Aug 10 '18 at 15:57
  • Also, the signals are emitted by celery, so I am not sure why it would matter which model is loading signals.py ? – Giannis Aug 10 '18 at 15:59
  • Have updated the question to remove the `signals.py` part – Giannis Aug 10 '18 at 16:57

1 Answers1

0

The configuration was correct, somehow after a system restart, the CONNECT prints started showing up on the celery task logs..

Giannis
  • 5,286
  • 15
  • 58
  • 113