Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django celery beat task not working

celery.py

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

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

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


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


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

settings.py

CELERY_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 = 'America/Los_Angeles'

I want to print "hello" every 10 seconds. So on running celery -A TwitterApiProxy beat in my terminal, I see as below:

LocalTime -> 2018-04-06 23:27:09
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)

it did not print anything related to the task that I scheduled. Where did I go wrong?

like image 449
lch Avatar asked Apr 06 '18 23:04

lch


1 Answers

Nothing is wrong with your set up.

Start your worker and celery beat in two separate cmd windows

celery -A TwitterApiProxy worker -l info
celery -A TwitterApiProxy beat -l info

If you are using Celery 4.0+, you have to install eventlet first, then start your worker like this:

celery -A TwitterApiProxy worker -l info -P eventlet

Task Admin Backend

If you want a task admin backend, you can install and use django-celery

like image 139
Sosthenes Kwame Boame Avatar answered Sep 23 '22 07:09

Sosthenes Kwame Boame