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.