Schedule tasks using django-celery based on user input

I am creating a reporting portal using django. On this portal, I need to give users the ability to schedule reports to run on a repeated basis. I am learning django-celery and understand that you can use the period_task decorator to schedule a repeat task, but in all the examples I saw, the cron schedule information is hard-coded in the decorator.

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

Is there a way to use django-celery to schedule a dynamic task dynamically based on user input?

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

+8
django celery user-input django-celery
source share
3 answers

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

Try it if you can build the necessary task settings there (using crontabs / intervals / periodic tasks) If yes, there is a big chance that you can quickly create it.

+1
source share

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") 

runs after 10 seconds.

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

executed after 10 seconds, indicated by eta p>

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

runs after a minute, but expires after 2 minutes.

+1
source share

Override the save method in models. Whenever a user enters a greeting to start a process / task, he will modify the model that launches the task.

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 
0
source share

All Articles