2017-10-18 79 views

回答

0

取决于规模和您的需求。

你将不得不使用Django芹菜拍为周期任务: http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#beat-custom-schedulers

我就老老实实创建将运行每次3-5分钟,芹菜任务。

models.py

class Foo(models.model): 
     created_at = models.DateTimeField(auto_add_now=True) 
     expiration_date = models.DateTimeField() 

views.py

import datetime 
from django.utils import timezone 

def add_foo(): 
    # Create an instance of foo with expiration date now + one day 
    Foo.objects.create(expiration_date=timezone.now() + datetime.timedelta(days=1)) 

tasks.py

from celery.schedules import crontab 
from celery.task import periodic_task 
from django.utils import timezone 

@periodic_task(run_every=crontab(minute='*/5')) 
def delete_old_foos(): 
    # Query all the foos in our database 
    foos = Foo.objects.all() 

    # Iterate through them 
    for foo in foos: 

     # If the expiration date is bigger than now delete it 
     if foo.expiration_date < timezone.now(): 
      foo.delete() 
      # log deletion 
    return "completed deleting foos at {}".format(timezone.now()) 
+0

有没有其他的方法可以完成这个,w没有芹菜。只想知道选项;)或任务排队是唯一的方式 –

+0

@manishadwani您的问题问怎么应该通过'芹菜'完成,并有'芹菜'标签。确保编辑问题,以便它也反映了这一点。其他可能的解决方案是设置一个cron作业,它可以通过bash运行'manage.py'命令,这将做同样的事情。芹菜是为像这样的用例而构建的。我建议你通过芹菜做到这一点,但如果别人有其他选择等待他们回应! :) –

+0

感谢您的快速响应,我会看看这个 –

相关问题