简体   繁体   English

python celery - 如何在运行时将 CELERYBEAT_SCHEDULE 任务添加到工作人员?

[英]python celery - how to add CELERYBEAT_SCHEDULE task at runtime to a worker?

I have created a celery worker with a single celerybeat schedule task which runs at 5 seconds time interval.我创建了一个 celery worker,它有一个 celerybeat 计划任务,它以 5 秒的时间间隔运行。 How can I add another beat task dynamically to the celery worker without stopping it?如何在不停止的情况下向芹菜工人动态添加另一个节拍任务?

Example例子

app.conf.update(
   CELERY_TASK_RESULT_EXPIRES=3600,
   CELERY_TIMEZONE = 'UTC',
   CELERYBEAT_SCHEDULE = {
    'long-run-5-secs': {
        'task': 'test_proj.tasks.test',
        'schedule': timedelta(seconds=5),
        'args': (16, )
    }
   }
)

With the above configuration, I am able to run the celery worker with beat mode successfully.有了上面的配置,我就可以成功运行带有beat模式的celery worker了。

Now I need add the below beat schedule dynamically:现在我需要动态添加以下节拍时间表:

'long-run-2-secs': {
    'task': 'test_proj.tasks.test',
    'schedule': timedelta(seconds=2),
    'args': (14, ) },

Thanks谢谢

I've been looking for solution for the very same problem.我一直在为同样的问题寻找解决方案。 I am affraid you'll have to wait for Celery ver.4.0.我担心你必须等待 Celery 4.0 版。 Dynamic task scheduling is only currently supported in the development version : http://docs.celeryproject.org/en/master/userguide/periodic-tasks.html#beat-entries目前仅在开发版本中支持动态任务调度: http : //docs.celeryproject.org/en/master/userguide/periodic-tasks.html#beat-entries

One possible way is to store the tasks in the database and add remove tasks dynamically.一种可能的方法是将任务存储在数据库中并动态添加移除任务。 You can use database backed celery beat scheduler for the same.您可以使用数据库支持的 celery beat 调度程序。 Refer https://django-celery-beat.readthedocs.io/en/latest/ .请参阅https://django-celery-beat.readthedocs.io/en/latest/ PeriodicTask database store the periodic tasks. PeriodicTask 数据库存储周期性任务。 You can manipulate the periodic task by using database commands (Django ORM).您可以使用数据库命令 (Django ORM) 来操作周期性任务。

This is how I handled the dynamic tasks (Create and stop tasks dynamically).这就是我处理动态任务的方式(动态创建和停止任务)。

from django_celery_beat.models import PeriodicTask, IntervalSchedule, CrontabSchedule

chon_schedule = CrontabSchedule.objects.create(minute='40', hour='08', day_of_week='*', day_of_month='*', month_of_year='*') # To create a cron schedule. 
schedule = IntervalSchedule.objects.create(every=10, period=IntervalSchedule.SECONDS) # To create a schedule to run everu 10 min.
PeriodicTask.objects.create(crontab=chon_schedule, name='name_to_identify_task',task='name_of_task') # It creates a entry in the database describing that periodic task (With cron schedule).
task = PeriodicTask.objects.create(interval=schedule, name='run for every 10 min', task='for_each_ten_min', ) # It creates a periodic task with interval schedule

Whenever you update a PeriodicTask a counter in this table is also incremented, which tells the celery beat service to reload the schedule from the database.每当您更新 PeriodicTask 时,此表中的计数器也会增加,这会告诉 celery beat 服务从数据库重新加载计划。

So you don't need to restart the or kill the beat.所以你不需要重新启动或终止节拍。 If you want to stop a task when particular criteria met then如果您想在满足特定条件时停止任务,则

periodic_task = PeriodicTask.objects.get(name='run for every 10 min')
periodic_task.enabled = False
periodic_task.save()

When enabled is False then the periodic task becomes idle.当启用为 False 时,周期性任务变为空闲。 You can again make it active by making enable = True .您可以通过使enable = True再次使其处于活动状态。

If you no longer needs the task then you can simply delete the entry.如果您不再需要该任务,则只需删除该条目即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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