@task
def daterange():
import datetime
some = Poll.objects.all()
schedule = Schedule.objects.all()
for c in schedule :
p = c.poll
e = c.end_time
s = c.start_time
n = int(c.no_of_response)
ph = Response.objects.filter(poll = p).exclude(sid = 'Null').count()
now = timezone.now()
if (c.start_time <= now) & (now <= c.end_time):
if (n == 0) | (n > ph):
c.poll.status='Running'
c.poll.save()
elif(n == ph):
c.poll.status='Complete'
c.poll.save()
#time.sleep(1000)
else:
c.poll.status='Out of Bound'
c.poll.save()
elif c.end_time < now:
c.poll.status='Complete'
c.poll.save()
elif c.start_time > now:
c.poll.status='New'
c.poll.save()
class Schedule(models.Model):
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
no_of_response = models.IntegerField(default = 0)
我每隔10秒安排一次芹菜运行任务。周期性任务包括根据开始时间和结束时间更新民意调查的代码。如果轮询位于开始时间和结束时间之间,则将轮询状态更新为running。如果当前状态正在运行,那么如何在达到结束时间时停止轮询。如何杀死此项目中的芹菜任务?
答案 0 :(得分:0)
重试任务,直到成功执行。可能的实施示例:
from celery import current_task
# decorator to retry task
def retry_if_false(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
if not func(*args, **kwargs):
current_task.retry()
return wrapped
@retry_if_false
@task
def daterange():
..<your code>..
return c.poll.status == 'Complete' # return True if polling finished.
现在您可以将任务作为正常任务开始,无需将其添加到celerybeat。