2017-10-18 87 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

感謝您的快速響應,我會看看這個 –

相關問題