简体   繁体   中英

Why is Celery periodic task not working? (Django)

It returns no errors, however when running celery -A mysite beat -l info & celery -A mysite worker -l info the task doesn't happen. When trying the same structure in another Django project it works fine. Help please!

My code:

mysite/settings.py (Only CELERY part)

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 = 'Africa/Nairobi'

mysite/ init .py

from __future__ import absolute_import
from .celery import app as celery_app

mysite/celery.py

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('mysite')

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))

personal/tasks.py

from celery.task.schedules import crontab
from celery.decorators import periodic_task
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)


@periodic_task(
    run_every=(crontab(minute='*/1')),
    name="task_deleteFiles",
    ignore_result=True
)
def task_deleteFiles():
    logger.info("Files Deleted")

PS: If necessary I can post the outputs from Celery

Apparently by deleting app.py and app.pyc (which were under the personal file) celery works perfectly normal. Python was possibly trying to import somehow the module, strangely it didn't return any error messages.

For others who find this page: If you are using Docker and you have changed celery beat settings during development, then there's a reasonable chance the container has old .pyc files that it is using as cache.

To avoid this error, delete all *.pyc files from your repo and add **/*.pyc to .dockerignore .

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