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

ik4ru52k

To access the info of a celery task, i need the 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). If i use celery-beat, which periodically sends tasks to the worker, that seems not to be possible.

So my question is, how to get the id from the task in the moment beat sends the task to celery's worker?

In the moment the worker receives the task, the console shows the task-id. So my worry is, that in the moment the task got sent by beat to the worker, it has no task id.

Manual case to get 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?

ik4ru52k

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.

Configure Periodic-Task

This is the schedule which "reminds" celery every day at 11:08 am (UTC) to kick off the task.

@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. It uses remote control commands under the hood. With i.active() you will get a dictionary, which you can easily parse.

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.

After you saved the task id you can easily poll task status etc. via AJAX for instance.

Hope that helps you guys :)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get RecursionError / KeyError trying to add Periodic Task in Celery beat

分類Dev

How to pass a class based task into CELERY_BEAT_SCHEDULE

分類Dev

How to get the queue that originated a task execution from Celery

分類Dev

How do i create seperate tasks for every objects with celery beat in Django models

分類Dev

celery task registry for asynchronous tasks

分類Dev

How to get arguments passed to the celery's task?

分類Dev

How to delete scheduled tasks

分類Dev

How to get Task ID from within ECS container?

分類Dev

Celery: How to batch produce tasks?

分類Dev

How to do scheduled tasks in ASP.NET?

分類Dev

spring boot scheduled task get hanging

分類Dev

How to record all tasks information with Django and Celery?

分類Dev

How to execute independent tasks sequentially using Celery?

分類Dev

How to create scheduled task of a batch file (Windows)?

分類Dev

Win10: How bring up VPN from Scheduled Task and/or Command line?

分類Dev

how can i pass argument to celery task?

分類Dev

django celery how to print or check logging for the task

分類Dev

How to set periodic task on Django using celery?

分類Dev

Retrieving Celery task kwargs from task-failed / uuid

分類Dev

django-celery vs django-celery-beat with Django 2.2

分類Dev

django-celery vs django-celery-beat with Django 2.2

分類Dev

How to get ID from insert()

分類Dev

Register multiple tasks with Celery

分類Dev

Python - Download files from Google Drive as a scheduled task

分類Dev

SCHTASKS - How to Create Scheduled Task With Max Run Time (Without Interval)

分類Dev

How to handle errors in celery's Task.map

分類Dev

How to unacknowledge message when I run a celery task?

分類Dev

Celery periodic task not periodic

分類Dev

Celery - ImportError: No module named tasks

Related 関連記事

  1. 1

    Get RecursionError / KeyError trying to add Periodic Task in Celery beat

  2. 2

    How to pass a class based task into CELERY_BEAT_SCHEDULE

  3. 3

    How to get the queue that originated a task execution from Celery

  4. 4

    How do i create seperate tasks for every objects with celery beat in Django models

  5. 5

    celery task registry for asynchronous tasks

  6. 6

    How to get arguments passed to the celery's task?

  7. 7

    How to delete scheduled tasks

  8. 8

    How to get Task ID from within ECS container?

  9. 9

    Celery: How to batch produce tasks?

  10. 10

    How to do scheduled tasks in ASP.NET?

  11. 11

    spring boot scheduled task get hanging

  12. 12

    How to record all tasks information with Django and Celery?

  13. 13

    How to execute independent tasks sequentially using Celery?

  14. 14

    How to create scheduled task of a batch file (Windows)?

  15. 15

    Win10: How bring up VPN from Scheduled Task and/or Command line?

  16. 16

    how can i pass argument to celery task?

  17. 17

    django celery how to print or check logging for the task

  18. 18

    How to set periodic task on Django using celery?

  19. 19

    Retrieving Celery task kwargs from task-failed / uuid

  20. 20

    django-celery vs django-celery-beat with Django 2.2

  21. 21

    django-celery vs django-celery-beat with Django 2.2

  22. 22

    How to get ID from insert()

  23. 23

    Register multiple tasks with Celery

  24. 24

    Python - Download files from Google Drive as a scheduled task

  25. 25

    SCHTASKS - How to Create Scheduled Task With Max Run Time (Without Interval)

  26. 26

    How to handle errors in celery's Task.map

  27. 27

    How to unacknowledge message when I run a celery task?

  28. 28

    Celery periodic task not periodic

  29. 29

    Celery - ImportError: No module named tasks

ホットタグ

アーカイブ