8

I am building a reporting portal using django. In this portal I need to give users the ability to schedule reports to run on a reoccurring basis. I have been researching django-celery and understand that you can use the periodic_task decorator to schedule a reoccurring task but in all the examples I have seen the cron schedule information is hard coded into the decorator.

@periodic_task(run_every=crontab(hours=7, minute=30, day_of_week="mon"))

Is there a way using django-celery to schedule a reoccurring task dynamically based on input from a user?

For example, a user uses a form to select the report they want run, provide all the parameters required by the report and the schedule when they want the report run on. Once I have processed the form is there a method or function I can call to add a run_report task to a schedule? If so is there a way to retrieve all the current schedules stored in the database so they can be displayed?

user1042361
  • 590
  • 4
  • 11
  • alternatively, you could schedule a single task which in turn schedules the next one, and so on... – DanJ Jan 01 '12 at 12:26
  • What if an inner function is created and applied the `@periodic_task` decorator with a dynamically created `crontab` object? I have the same problem, maybe this would help? – Armando Pérez Marqués Feb 04 '12 at 14:00
  • I have the exact same problem. Any infos on how you solved it? – Jay Feb 08 '13 at 15:50

3 Answers3

1

http://celery.readthedocs.org/en/latest/userguide/calling.html

eg:-

from celery import task

@task.task(ignore_result=True)
def T(message=None ):
    print message

.

T.apply_async(countdown=10, message="hi")

executes 10 seconds from now.

T.apply_async(eta=now + timedelta(seconds=10),message="hi")

executes 10 seconds from now, specifed using eta

T.apply_async(countdown=60, expires=120,message="hi")

executes in one minute from now, but expires after 2 minutes.

Ryu_hayabusa
  • 3,666
  • 2
  • 29
  • 32
1

Tak a look at djcelery in the admin-interface: http://localhost:8000/admin/djcelery/

Try if you can build the required task-setup there (using crontabs/intervals/periodic tasks) If yes there is a big chance that you can build this up quickly..

ohrstrom
  • 2,890
  • 2
  • 20
  • 34
0

Override your save method in models. Whenever user enters likes to start a process/task, he will modify the model which triggers the task to start.

your_app/models.py:

class My_Model(models.Model):
customer = models.ForeignKey(User, related_name='original_customer_id')
start_task = models.BooleanField(default=False, blank=True)

def save(self, *args, **kwargs):
    super(NewProject, self).save(*args, **kwargs)
    from .tasks import my_task
    my_task.apply_async(args=[self.pk, self.status, self.file_type],)

your_app/tasks.py

@celery.task()
def my_task(foo, bar):
    #do something
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136