2017-10-16 102 views

回答

0

將任務插入數據庫表background_task中,完成後將任務從background_task表移至background_task_completedtask表。您可以使用此信息創建視圖以獲取所有/特定任務的狀態。

from background_task.models import Task 
from background_task.models_completed import CompletedTask 
from datetime import datetime 
from django.utils import timezone 

def get_status(request): 
    now = timezone.now() 

    # pending tasks will have `run_at` column greater than current time. 
    # Similar for running tasks, it shall be 
    # greater than or equal to `locked_at` column. 
    # Running tasks won't work with SQLite DB, 
    # because of concurrency issues in SQLite. 
    pending_tasks_qs = Task.objects.filter(run_at__gt=now) 
    running_tasks_qs = Task.objects.filter(locked_at__gte=now) 

    # Completed tasks goes in `CompletedTask` model. 
    # I have picked all, you can choose to filter based on what you want. 
    completed_tasks_qs = CompletedTask.objects.all() 

    # main logic here to return this as a response. 

    # just for test 
    print (pending_tasks_qs, running_tasks_qs, completed_tasks_qs) 
    return HttpResponse("ok") 

最後,寄存器在urlpatterns的該視圖並檢查其狀態。