简体   繁体   English

如果计划任务(节拍),如何从芹菜中获取任务ID

[英]How to get task id from celery in case of scheduled tasks (beat)

To access the info of a celery task, i need the task_id. 要访问芹菜任务的信息,我需要task_id。 When a celery task gets manually started i can easily get the id of this task with task.id (and the write it to DB or do something else). 手动启动celery任务时,我可以通过task.id轻松获取此任务的ID(并将其写入数据库或执行其他操作)。 If i use celery-beat, which periodically sends tasks to the worker, that seems not to be possible. 如果我使用celery-beat,它会定期向工作人员发送任务,那似乎是不可能的。

So my question is, how to get the id from the task in the moment beat sends the task to celery's worker? 所以我的问题是,在节拍将任务发送给芹菜工人的那一刻,如何从任务中获取ID?

In the moment the worker receives the task, the console shows the task-id. 当工作人员接收任务时,控制台将显示任务ID。 So my worry is, that in the moment the task got sent by beat to the worker, it has no task id. 因此,我担心的是,当任务被殴打发送给工作人员时,它没有任务ID。

Manual case to get task_id: 获取task_id的手动案例:

task = tasks.LongRunningTask.delay(username_from_formTargetsLaden, password_from_formTargetsLaden, url_from_formTargetsLaden)

task_id = task.id

Mayhaps some of you got an idea? 也许有些人有个主意?

i found an answer for that little issue: 我找到了这个小问题的答案:

If you need the task id of the tasks which was initially sent by beat, you can simply add an inspect function to your (schedulded) worker task. 如果您需要节拍最初发送的任务的任务ID,则只需将检查函数添加到(预定的) 工作任务即可。

Configure Periodic-Task 配置定期任务

This is the schedule which "reminds" celery every day at 11:08 am (UTC) to kick off the task. 这是每天在11:08 am(UTC)“提醒”芹菜开始任务的时间表。

@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    test = sender.add_periodic_task(crontab(minute=8, hour=11), CheckLists.s(app.config['USR'], app.config['PWD']))

Task to be executed periodically 定期执行的任务

This is the scheduled task which will be executed by celery after the worker received the "reminder" from beat. 这是排定的任务,将在工人收到节拍的“提醒”后​​由芹菜执行。

@celery.task(bind=True)
def CheckLists(self, arg1, arg2):
    #get task_id von scheduled Task 'Check-List'
    i = inspect()
    activetasks = i.active()
    list_of_tasks = {'activetasks': activetasks}
    task_id = list_of_tasks['activetasks']['celery@DESKTOP-XXXXX'][0]['id']   #adapt this section depending on environment (local, webserver, etc...)
    task_type = "CHECK_LISTS"
    task_id_to_db = Tasks(task_id, task_type)
    db.session.add(task_id_to_db)
    db.session.commit()

    long_runnning_task 
    [...more task relevant code here...] 

So i'm making use out of app.control.inspect which lets you inspect running workers. 所以我要利用app.control.inspect来检查正在运行的工作程序。 It uses remote control commands under the hood. 它在引擎盖下使用远程控制命令。 With i.active() you will get a dictionary, which you can easily parse. 使用i.active()您将获得一个字典,您可以轻松地对其进行解析。

As long as i don't find any documentation how to get the task_id from a periodic task more easier, i'll stick to that solution. 只要找不到任何文档,如何更轻松地从定期任务中获取task_id,我就会坚持使用该解决方案。

After you saved the task id you can easily poll task status etc. via AJAX for instance. 保存任务ID后,您可以轻松地通过AJAX查询任务状态等。

Hope that helps you guys :) 希望对大家有帮助:)

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

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