1

I have a django project which contains a long running process. I have used django-background-tasks library for this. It works but I want to create a pending page for users and display the status of the task. I should refresh that page every 60 seconds and update the status. How can I do that?

Thank you.

Aslı Kök
  • 616
  • 8
  • 19

2 Answers2

2

Hope you know about Ajax.

How to use Ajax with Django: https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

How to run Ajax code every n seconds: https://thisinterestsme.com/ajax-request-every-10-seconds/

If you want to load a page partially, then you should split the page into two parts. One should contain the particular div which you want to refresh or reload, consider the page name as partial_load.html. And other page can have remaining codes consider the file name as full_page.html. The partial_load.html can be included inside the full_page.html by using include tag ({% include "partial_load.html" %})

 def refresh_page(request):
      if request.is_ajax():
           now = timezone.now()
           pending, running = get_process_status()
           context = {
             "pending": count_pending, 
             "running": count_running
             }
           return render(request, "partial_load.html", context)

full_page.html

 <html>
  <div> Something </div>
   .....
   <div id="status">
    {% include "partial_load.html" %}
   </div>
  ......
  <div> some more stuff </div>
 </html>
Balasundar
  • 141
  • 6
0

If you migrated the database changes after the installation of the background_task package (Then only the database have background_task package's tables). You can get the status of the processes running in background by simply querying the background_task models like querying other user defined models.

from background_task.models import Task, CompletedTask
from django.utils import timezone

def get_process_status(parameters):
     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.
     # If your task still not started running and waiting in the queue, then you  can find it in pending_tasks

     pending_tasks = Task.objects.filter(run_at__gt=now)

     # If your your task is in running state, you can find it in running_tasks

     running_tasks = 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.
     # If your task completed you can find it in Completed task model.

     completed_tasks = CompletedTask.objects.all()

     # If you want the result in json format just add .values() at the end of the
     # ORM query like "completed_tasks = CompletedTask.objects.all().values()"     
     print(completed_tasks, running_tasks, pending_tasks)
     ......
     ......
     return process_status

If you want to run the function for every 60 seconds, schedule the task using background_task. Sample code:

@background(schedule=60)
def get_process_status(parameters):
  .....
  code
  .....
  return process_status

Hope it will help you.

Balasundar
  • 141
  • 6