1

I'm using celery inside a django project, i have a celery scheduled task that run every minute and check inside a db if there is a new task to start, and also the task configured has a time start and duration.

The job of this periodic task is:

  • Start a new async task if there is a newone configured. (task.delay(...))
  • Check if a task previous started is running
  • Stop task that exceed its duration (app.control.revoke(...))
  • .... and other stuff...

But the question is: What is the "best practice" to monitor the status of started async task inside a periodic task?

I mean everytime the sceduled task run, i get from DB all the configured task (started, to start....) but i don't have the related celery task id associated to it, should i store celery task id inside db, to have the db task associated to the related task celery running?

Could django-celery help me?

Thanks.

4

1 回答 1

1

Celery 将使用 Result Backends 自动为您跟踪状态。如果您想使用 Django ORM 存储此状态,那么 django-celery 可以提供帮助:

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-the-django-orm-cache-as-a-result-backend

http://docs.celeryproject.org/en/latest/userguide/tasks.html#result-backends

http://docs.celeryproject.org/en/latest/configuration.html#task-result-backend-settings

可能也有帮助的一件事是 Celery 有几个功能可以停止超过其持续时间的任务。

您可以使用配置来设置全局限制:

http://docs.celeryproject.org/en/latest/configuration.html#celeryd-task-time-limit

您可以使用装饰器参数设置每种任务类型的到期时间:

http://docs.celeryproject.org/en/latest/userguide/tasks.html#list-of-options

您可以设置每个计划实例的到期时间:

http://docs.celeryproject.org/en/latest/userguide/calling.html#expiration

于 2015-02-12T05:50:15.997 回答