Valerian
Valerian

Reputation: 435

Django + background-tasks how to initialize

I have a basic django projects that I use as a front end interface for a (Condor) computing cluster for generating simulations. From the django app the users can start simulations (in Condor). The simulation related meta-data and the simulation state are kept in a DB.

I need to add a new feature: notification when (some) simulations are done.

Since I want a simple solution (and I already using background tasks) I was thinking to use repeating task that at fixed intervals query Condor about the tasks, updates the DB and if necessary sends notifications.

So if I want to update every 10 min that statuses I will have something like:

@background(schedule=1)
def check_simulations(repeat=600):
    # lookup simulation statuses
    simulation_list = get_Simulations()
    for sim in simulations_list:
       if sim.status == Simulation.DONE:
            user.email_user('Simulation Complete', 'You have been notified')

def initialize():
     check_simulations()

However this task (or better say the initialize() method) must be started (called once) to create and schedule the check_simulations() task (which will practically serialize the call and save it in the DB); after that the background-tasks thread will read it and execute and also reschedule it (if there is error)

My questions:

One such place could be for instance the urls.py but this is an extremely ugly solution. Is there a better way ?

Upvotes: 2

Views: 4013

Answers (1)

Faiya
Faiya

Reputation: 151

i had a similar problem and i solved it this way.

i initialize my task in urls.py, i dont know if you can use other places to put it ,also added and if, to check if the task its allready in the database

from background_task.models import Task
if not Task.objects.filter(verbose_name="update_orders").exists():
   tasks.update_orders(repeat=300, verbose_name="update_orders")

i have tested it and it works fine, you can also search for the order with other parameters like name, hash ,...

you can check the task model here: https://github.com/arteria/django-background-tasks/blob/master/background_task/models.py

Upvotes: 5

Related Questions