JSRB
JSRB

Reputation: 2623

Django Celery task doesn't fire in development

I'm trying to make use of periodic tasks but can't make it work.

I have this test task

# handler/tasks.py

from celery import Celery

app = Celery()


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 2 seconds.
    sender.add_periodic_task(2, test.s('hello'), name='add every 2')


@app.task
def test(arg):
    print(arg)

Celery is configured

# project dir
# salaryx_django/celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'salaryx_django.settings')
app = Celery('salaryx_django')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
# salaryx_django/settings.py

# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/London'

The workers are intiated

[2022-04-25 14:57:55,424: INFO/MainProcess] Connected to redis://localhost:6379//
[2022-04-25 14:57:55,426: INFO/MainProcess] mingle: searching for neighbors
[2022-04-25 14:57:56,433: INFO/MainProcess] mingle: all alone
[2022-04-25 14:57:56,452: WARNING/MainProcess] /Users/jonas/Desktop/salaryx_django/venv/lib/python3.8/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory

[2022-04-25 14:57:56,453: INFO/MainProcess] celery@Air-von-Jonas ready.

and Redis is waiting for connections

enter image description here

but nothing happens at all..

Celery Beat

(venv) jonas@Air-von-Jonas salaryx_django % celery -A salaryx_django beat
celery beat v5.2.6 (dawn-chorus) is starting.
__    -    ... __   -        _
LocalTime -> 2022-04-26 05:38:27
Configuration ->
    . broker -> redis://localhost:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%WARNING
    . maxinterval -> 5.00 minutes (300s)

Upvotes: 0

Views: 521

Answers (1)

Borut
Borut

Reputation: 3364

You also have to run beat.

From beat entries documentation

The add_periodic_task() function will add the entry to the beat_schedule setting behind the scenes

Simply running celery -A salaryx_django beat in another process should get you going. Read docs for more info.

Upvotes: 1

Related Questions