简体   繁体   English

芹菜的定期任务已发送但未执行

[英]periodic task for celery sent but not executed

I am using celery in one of my django projects to perform some tasks. 我在django项目之一中使用celery来执行一些任务。 I recently required to add a periodic_task to update a field for the objects in one of my models. 最近,我需要添加periodic_task来更新我的一个模型中对象的字段。 so I added a periocdic_task as follows. 所以我添加了一个periocdic_task ,如下所示。

import datetime
import logger.logger as logger

from celery.decorators import periodic_task



@periodic_task(run_every=datetime.timedelta(seconds=30))
def entries_about_to_expire():
   """ Changes the status of entries that are about to expire.
   """

   try:
       import entries.models as models
       startdate = datetime.datetime.today()
       enddate = startdate + datetime.timedelta(days=7)
       soon = models.EntryStatus.objects.get_or_create(
               name='Expiring Soon')[0]
       models.Entries.objects.filter(expiry_date__range=[startdate, enddate])\
               .update(status=soon)

   except Exception, err:
       logger.exception("Error while changing job status: %s" % (err))

Next thing I did was to run celery as follows: 接下来,我做celery如下:

>>python manage.py celeryd -l info --beat

Everything runs fine until now. 到现在为止一切运行正常。 I can even see the task is sent by celery as follows (this happens every 30 seconds as required). 我什至可以看到该任务是由芹菜发送的,如下所示(此操作每30秒根据需要发生一次)。

[2013-11-15 15:01:10,690: INFO/MainProcess] Scheduler: Sending due task entries.tasks.entries_about_to_expire (entries.tasks.entries_about_to_expire)

But for some reason the task is not executed. 但是由于某种原因,任务没有执行。 I mean I can't find any changes in the status of Entries objects. 我的意思是我找不到Entries对象status的任何变化。 I tested this using the shell. 我使用外壳对此进行了测试。

In [2]: from entries.models import Entries, EntryStatus

In [3]: soon = EntryStatus.objects.get_or_create(name='Expiring Soon')[0]

In [4]: entries = Entries.objects.filter(status=soon)

In [5]: entries
Out[5]: []

I get the same results even after the task is sent multiple times. 即使多次发送任务,我也得到相同的结果。 I am sure the code for entries_about_to_expire runs fine because if I call it manually it works. 我确信entries_about_to_expire的代码可以entries_about_to_expire运行,因为如果我手动调用它,它可以工作。 For example if I do the following in the shell 例如,如果我在外壳中执行以下操作

In [6]: from entries.tasks import entries_about_to_expire

In [7]: entries_about_to_expire()

In [8]: entries = Entries.objects.filter(status=soon)

In [9]: Entries
Out[9]: [<Entries: 223>, <Entries: 512>, <Entries: 526>, <Entries: 591>]

So why is the celery task is not executing ? 那么,为什么celery任务没有执行? any clues ? 有什么线索吗?

Note: I've also tried the celerybeat_schedule method but the same results ie task is sent but not executed 注意:我也尝试过celerybeat_schedule方法,但结果相同,即发送任务但未执行

The database scheduler will not reset when timezone related settings change, so you must do this manually. 当与时区相关的设置更改时,数据库调度程序将不会重置,因此您必须手动执行此操作。

PeriodicTask.objects.update(last_run_at=None)

Refer: http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#time-zones 请参阅: http : //docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#time-zones

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

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