根据用户输入使用django-celery安排任务

时间:2022-05-01 19:18:02

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.

我正在使用django构建报告门户。在此门户中,我需要让用户能够安排报告在重复的基础上运行。我一直在研究django-celery,并了解你可以使用periodic_task装饰器来安排重复发生的任务,但在我看到的所有例子中,cron调度信息被硬编码到装饰器中。

@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?

有没有办法使用django-celery根据用户的输入动态安排重复发生的任务?

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?

例如,用户使用表单选择他们想要运行的报表,提供报表所需的所有参数以及他们希望运行报表的计划。一旦我处理完表单,我可以调用一个方法或函数来将run_report任务添加到日程表中吗?如果有,是否有办法检索存储在数据库中的所有当前计划,以便显示它们?

3 个解决方案

#1


1  

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

看一下admin-interface中的djcelery: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..

尝试在那里构建所需的任务设置(使用crontabs / interval / periodic tasks)如果是,那么很有可能你可以快速构建它。

#2


1  

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

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.

从现在开始执行10秒。

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

executes 10 seconds from now, specifed using eta

从现在开始执行10秒,使用eta指定

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

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

从现在起一分钟后执行,但在2分钟后过期。

#3


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:

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

your_app / tasks.py

@celery.task()
def my_task(foo, bar):
    #do something

#1


1  

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

看一下admin-interface中的djcelery: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..

尝试在那里构建所需的任务设置(使用crontabs / interval / periodic tasks)如果是,那么很有可能你可以快速构建它。

#2


1  

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

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.

从现在开始执行10秒。

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

executes 10 seconds from now, specifed using eta

从现在开始执行10秒,使用eta指定

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

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

从现在起一分钟后执行,但在2分钟后过期。

#3


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:

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

your_app / tasks.py

@celery.task()
def my_task(foo, bar):
    #do something