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

发布于 2024-12-20 02:07:13 字数 427 浏览 4 评论 0原文

我正在使用 django 构建一个报告门户。在此门户中,我需要让用户能够安排报告定期运行。我一直在研究 django-celery 并了解您可以使用 periodical_task 装饰器来安排重复发生的任务,但在所有示例中我都看到 cron 计划信息被硬编码到装饰器中。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-12-27 02:07:13

在管理界面中查看 djcelery:
http://localhost:8000/admin/djcelery/

尝试是否可以构建所需的任务设置那里(使用 crontabs/间隔/定期任务)
如果是的话,您很有可能可以快速建立起来。

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

说谎友 2024-12-27 02:07:13

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

例如:-

from celery import task

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

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

从现在起 10 秒后执行。

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

从现在起 10 秒后执行,使用 eta 指定

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

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

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.

终难愈 2024-12-27 02:07:13

覆盖模型中的保存方法。每当用户输入喜欢启动流程/任务时,他都会修改触发任务启动的模型。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文