Creare un TaskΒΆ

Per aggiungere un task creare il file tasks.py

creare la classe in tasks.py:

class NomeTask(JmbJobtasticTask):
   task_name = "NOME DEL TASK" # qui mettere il nome del task
   def calculate_result(self, **kw):
       #qui mettere la logica del task
       pass

esempio:

class AspettaTask(JmbJobtasticTask):
   task_name = "NOME DEL TASK"
   significant_kwargs = []
   herd_avoidance_timeout = 60
   cache_duration = 0
   def calculate_result(self, **kw):
       logger.info('Running aspetta con xtastic ')

       n_of_steps = 20
       sec_for_step = 2
       # Only actually update the progress in the backend every n operations
       update_frequency = 1
       total_delay = 40

       for count, x in enumerate(range(0, total_delay, sec_for_step)):
           time.sleep(sec_for_step)
           self.update_progress(
               count,
               n_of_steps,
               update_frequency=update_frequency,
           )
           logger.info('Running aspetta - %s' % x)
       return "Finito!"

Se si vuole far partire il task dal menu admin della change_list, creare l’azione, importare la classe NomeTask dal file tasks.py e far chiamare NomeTask.delay_or_fail()

esempio:

from jmb.async.tasks import AspettaTask

class TaskAdmin(ExtendibleModelAdmin):

   def action_task(self, request, queryset):
           result = AspettaTask.delay_or_fail()
   actions = [action_task]

per provare un task di test, attivare il debug selezionare dell admin http://127.0.0.1:8000/admin/async/task/ un task e selezionare l’azione “Create task test”

Previous topic

Installazione

This Page