简体   繁体   中英

Django Celery Periodic Task not running (Heroku)?

I have a Django application that I've deployed with Heroku. I'm trying to user celery to create a periodic task every minute. However, when I observe the logs for the worker using the following command:

heroku logs -t -p worker 

I don't see my task being executed. Perhaps there is a step I'm missing? This is my configuration below...

Procfile

web: gunicorn activiist.wsgi --log-file -
worker: celery worker --app=trending.tasks.app

Tasks.py

import celery
app = celery.Celery('activiist')
import os
from celery.schedules import crontab
from celery.task import periodic_task
from django.conf import settings
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
os.environ['DJANGO_SETTINGS_MODULE'] = 'activiist.settings'
from trending.views import *
@periodic_task(run_every=crontab())
def add():
    getarticles(30)

One thing to add. When I run the task using the python shell and the "delay()" command, the task does indeed run (it shows in the logs) -- but it only runs once and only when executed.

You need separate worker for the beat process (which is responsible for executing periodic tasks):

web: gunicorn activiist.wsgi --log-file -
worker: celery worker --app=trending.tasks.app
beat: celery --app=trending.tasks.app

Worker isn't necessary for periodic tasks so the relevant line can be omitted. The other possibility is to embed beat inside the worker:

web: gunicorn activiist.wsgi --log-file -
worker: celery worker --app=trending.tasks.app -B

but to quote the celery documentation :

You can also start embed beat inside the worker by enabling workers -B option, this is convenient if you will never run more than one worker node, but it's not commonly used and for that reason is not recommended for production use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM