芹菜:访问上次运行任务的时间?

时间:2022-08-20 19:17:27

Using celery, is it possible from within a task to check when the last time that task was run is?

使用celery,是否可以在任务中检查上次运行任务的时间是什么?

I'd like to implement "grab everything since the last time you were run". I can either keep track of the last run timestamp myself or hopefully get access to it from celery.

我想实施“自上次运行以来抓住所有东西”。我可以自己跟踪上次运行时间戳,也可以从芹菜中获取它。

This is from within a django project, if that makes a difference.

这是来自django项目,如果这有所不同。

2 个解决方案

#1


3  

By default celery doesn't have a permanent storage of the task results, if you are running in beat mode some clean process will run for clean task results and also execution infos. Want I suggest you is to use a NoSQL for store every last execution date, you can do this by override in your task the method after_return

默认情况下,celery没有任务结果的永久存储,如果您在节拍模式下运行,一些干净的进程将运行干净的任务结果以及执行信息。我想建议您在每个上次执行日期使用NoSQL存储,您可以通过在任务中覆盖after_return方法来执行此操作

def after_return(self, status, retval, task_id, args, kwargs, einfo):
    #exit point for context managers
    self.taskLogger.__exit__(status, retval, task_id, args, kwargs, einfo)

This method is called every time the task ends, with any result, by check the status you can store your date only when the task finish with SUCCESS or implement the behavior that best fits your needs.

每次任务结束时都会调用此方法,任何结果,只有当任务完成SUCCESS或实现最符合您需求的行为时,才能检查状态,即可存储日期。

#2


1  

If you are using django-celery, last_run at is saved in the model. Use the normal django ORM to filter the PeriodicTask model using your task name.

如果您使用的是django-celery,则last_run at将保存在模型中。使用普通的django ORM使用您的任务名称过滤PeriodicTask模型。

from djcelery.models import PeriodicTask

last_run = PeriodicTask.objects.only('last_run_at').get(task='TASK_NAME_HERE').last_run_at

#1


3  

By default celery doesn't have a permanent storage of the task results, if you are running in beat mode some clean process will run for clean task results and also execution infos. Want I suggest you is to use a NoSQL for store every last execution date, you can do this by override in your task the method after_return

默认情况下,celery没有任务结果的永久存储,如果您在节拍模式下运行,一些干净的进程将运行干净的任务结果以及执行信息。我想建议您在每个上次执行日期使用NoSQL存储,您可以通过在任务中覆盖after_return方法来执行此操作

def after_return(self, status, retval, task_id, args, kwargs, einfo):
    #exit point for context managers
    self.taskLogger.__exit__(status, retval, task_id, args, kwargs, einfo)

This method is called every time the task ends, with any result, by check the status you can store your date only when the task finish with SUCCESS or implement the behavior that best fits your needs.

每次任务结束时都会调用此方法,任何结果,只有当任务完成SUCCESS或实现最符合您需求的行为时,才能检查状态,即可存储日期。

#2


1  

If you are using django-celery, last_run at is saved in the model. Use the normal django ORM to filter the PeriodicTask model using your task name.

如果您使用的是django-celery,则last_run at将保存在模型中。使用普通的django ORM使用您的任务名称过滤PeriodicTask模型。

from djcelery.models import PeriodicTask

last_run = PeriodicTask.objects.only('last_run_at').get(task='TASK_NAME_HERE').last_run_at