Simran
Simran

Reputation: 633

django celerybeat not invoking tasks.py function

Based on the tutorial: https://www.merixstudio.com/blog/django-celery-beat/

celery.py file code

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from Backend.settings import INSTALLED_APPS

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: INSTALLED_APPS)

tasks.py file code

from celery.task import task
@task(name='task1')
def emailt():
    print("email func invoked")
    # code...

settings.py

from __future__ import absolute_import
import os
import djcelery
from celery.schedules import crontab
djcelery.setup_loader()

INSTALLED_APPS = [
'djcelery',
'django_celery_beat',
 ....
]

REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'   

CELERYBEAT_SCHEDULE = {
'task1': 
    {
        'task': 'proj.tasks.emailt',
        'schedule': crontab(hour='*', minute='1', day_of_week='mon,tue,wed,thu,fri,sat,sun'),
    }
} 

In one command shell, redis server and django py manage.py runserver is running. On another shell, the celery command is run as follows: celery -A proj.tasks beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler

Log files denote that celerybeat is running.

Configuration ->
. broker -> redis://localhost:6379/0
. loader -> djcelery.loaders.DjangoLoader
. scheduler -> django_celery_beat.schedulers.DatabaseScheduler

. logfile -> [stderr]@%INFO
. maxinterval -> 5.00 seconds (5s)
[*************: INFO/MainProcess] beat: Starting...
[*************: INFO/MainProcess] Writing entries...
[*************: INFO/MainProcess] DatabaseScheduler: Schedule changed.
[*************: INFO/MainProcess] Writing entries...
[*************: INFO/MainProcess] Writing entries...

However, the function emailt() within tasks.py is still not getting invoked. I am unable to locate the issue with celerybeat.

Upvotes: 0

Views: 433

Answers (1)

iklinac
iklinac

Reputation: 15718

DatabaseScheduler is the database scheduler implementation and doesn't take tasks from CELERYBEAT_SCHEDULE dictionary

If you are going to use this type of scheduler you should create PeriodicTask through django admin or through data migrations/views

You can use crontab notation with default scheduler in tasks or with DatabaseScheduler by creating CrontabSchedule and attaching it to PeriodicTask

Upvotes: 1

Related Questions