简体   繁体   中英

Django + Celery: how to chain tasks with parameters to periodic task

I have configured Django + Celery: all works, i can execute tasks, that called from views.py ie mul.apply_async((2, 5), queue='celery', countdown=5)

I need to shedule periodic task that will chain simple tasks with argument that passed from users. I read docs http://docs.celeryproject.org/en/latest/userguide/canvas.html and know how to chain, i know how to make periodic task without parameters @periodic_task(run_every=(crontab(hour="*", minute="*", day_of_week="*")))

But how to combine this?

What i want workflow:

  1. User creates project with parameters. 5 tasks executed, using that parameters.
  2. Then i need shedule to repeat all 5 tasks every 24 hours. So here i dont know how to pass parameters (they save to db).

In other answer i saw this syntax:

CELERYBEAT_SCHEDULE = {
# crontab(hour=0, minute=0, day_of_week='saturday')
'schedule-name': {  # example: 'file-backup' 
    'task': 'some_django_app.tasks....',  # example: 'files.tasks.cleanup' 
    'schedule': crontab(...)
    'args': (2, 3)
},

}

But the problem here is that it located in settings.py of Django but not in tasks.py and i cannot dynamically pass args.

The celerybeat task you register could be a wrapper and perform the project/task logic inside of it, firing off other tasks as appropriate. You could fetch the project tasks inside of your celery beat job.

  • CELERYBEAT_SCHEDULE.task -> 'some_django_app.project_beat_task'

Then project beat task could retrieve the correct projects and all tasks associated with them, perhaps spawning a chain of tasks for each project

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