简体   繁体   中英

Django celery run periodic task every day at specific time

I want to run django celery periodic task every day at 6:30 pm

tasks.py

import celery
from datetime import datetime
@celery.task
def my_task():
    print(' task called ')
    print(datetime.now())
    return True

celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery
from tasks import my_task
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
celery_instance = Celery('my_app')

celery_instance.config_from_object('django.conf:settings', namespace='CELERY')
celery_instance.autodiscover_tasks()

@celery_instance.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(
        crontab(minute=30, hour=18, day_of_week='mon,tue,wed,thu,fri,sat,sun'),
        my_task.s(),
    )

    # when i try this it work of every 1 min
    sender.add_periodic_task(
         crontab(minute='*/1'),
         my_task.s(),
   )

when i hit celery -A app_name worker -B

the task is not hitting at 6:30 pm

Simply remove the day_of_week param.

Keep only crontab(hour=18, minute=30) # It will run daily at 6:30 PM at your set timezone.

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