Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule tasks using django-celery based on user input

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?

like image 578
user1042361 Avatar asked Dec 08 '11 21:12

user1042361


People also ask

How do I schedule a task in Django Celery?

Here are some key points: DJANGO_SETTINGS_MODULE must be set in the environment before starting a Celery process. Its presence in the environment triggers internal magic in Celery to run the Django setup at the right time. The Celery "application" must be created and configured during startup of both Django and Celery.

How do I schedule a task in Django?

Run the scheduler When using Django, you will usually have one process that is responsible for serving web requests and a separate one that takes care of processing tasks. During local development, these two processes are: web requests: ./manage.py runserver. async tasks: ./manage.py qcluster.

How does Celery execute tasks?

Celery workers are worker processes that run tasks independently from one another and outside the context of your main service. Celery beat is a scheduler that orchestrates when to run tasks. You can use it to schedule periodic tasks as well.

How do you pass arguments to Celery task?

To pass arguments to task with apply_async() you need to wrap them in a list and then pass the list as first argument, I.e. apply_async([arg1, arg2, arg3]) . See the documentation for more details and examples. Use delay() as an alternative.


2 Answers

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..

like image 73
ohrstrom Avatar answered Nov 15 '22 08:11

ohrstrom


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.

like image 25
Ryu_hayabusa Avatar answered Nov 15 '22 09:11

Ryu_hayabusa