Celery Crontab 中的输出日期调度到 Django 模板

     2023-02-24     75

关键词:

【中文标题】Celery Crontab 中的输出日期调度到 Django 模板【英文标题】:Output Dates in Celery Crontab Schedule to Django Template 【发布时间】:2021-07-15 01:28:54 【问题描述】:

我将 Celery 用于我的 Django 项目,并且我已经安排了一些 crontab 任务以在特定时间向用户发送电子邮件。我需要在 HTML/Django 模板中输出一个时间表,显示用户可以期待电子邮件发出的日期。我的 Celery 的 crontab 时间表如下所示:

import os
from celery import Celery
from celery.schedules import crontab

app = Celery("myapp")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print("Request: 0!r".format(self.request))

first_monday_april_to_july = crontab(
    minute=15, hour=6, day_of_week="monday", day_of_month="1-7", month_of_year="4,5,6,7"
)
august_every_monday_first_two_weeks = crontab(
    minute=15, hour=6, day_of_week="monday", day_of_month="1-14", month_of_year="8"
)
august_weekdays_second_two_weeks = crontab(
    minute=15, hour=6, day_of_week="mon-fri", day_of_month="15-31", month_of_year="8"
)

app.conf.beat_schedule = 
    "report1": 
        "task": "email_report",
        "schedule": first_monday_april_to_july,
    ,
    "report2": 
        "task": "email_report",
        "schedule": august_every_monday_first_two_weeks,
    ,
    "report3": 
        "task": "email_report",
        "schedule": august_weekdays_second_two_weeks,

我希望能够从这些任务中获取发送电子邮件的所有日期,并将它们保存到视图中的列表中,然后将其发送到模板以显示在表格中。这样的事情可能吗?到目前为止,我还没有找到一种方法来做到这一点。

【问题讨论】:

您的意思是,您需要计算下一个执行日期是什么? meybe this package 可以帮助您。 PeriodicTask 此 django 应用程序中定义的任务模型包含有关定期 celery 任务的信息。您可以将此模型的查询集传递给上下文并在模板中使用。 @VictorErmakov 是的,它需要显示任务的所有执行日期。 我想,你需要得到这个包github.com/kiorky/croniter 【参考方案1】:

我最终创建了一个函数来获取任务中的所有日期,然后将它们全部附加到计划列表中,然后通过视图将其传递给模板以在表格中输出。它看起来像这样:

from datetime import date, datetime, timedelta
from .celery import (
    first_monday_april_to_july,
    august_every_monday_first_two_weeks,
    august_weekdays_second_two_weeks,
)


def get_schedule_due_dates(schedule, last_date=datetime.now().date().replace(month=12, day=31)):
    start_date = datetime.now()
    end_date = datetime.combine(last_date, datetime.max.time())
    schedules = []
    while start_date <= end_date:
        next_due_schedule = schedule.remaining_estimate(start_date)
        due_date = datetime.now() + next_due_schedule
        if due_date <= end_date:
            schedules.append(due_date)
        else:
            break
        start_date = due_date + timedelta(days=1)
    return schedules


def report_schedule(request):
    schedules = []
    schedules.extend(get_schedule_due_dates(first_monday_april_to_july))
    schedules.extend(get_schedule_due_dates(august_every_monday_first_two_weeks))
    schedules.extend(get_schedule_due_dates(august_weekdays_second_two_weeks))
    return render(
        request,
        "reports/report_schedule.html",
        "schedules": sorted(schedules),
    )

【讨论】:

使用 Flask 动态调度 Celery Beat 任务

...希望能够让我的应用程序用户使用Celerybeat启动/停止定期crontab样式的任务。现在我用运行Celeryvenv/bin/celeryworker-Acelery_worker.celery--loglevel=inf 查看详情

crond任务调度(代码片段)

crond任务调度定时执行任务,1示意图2基本语法crontab[选项]-e:bianjicrontab定时任务-l:查询crontab-r:删除当前用户所有的crontab任务例子:每分钟执行查看一次/ect目录,把目录内容写进/tml/a.txt下具体实现步骤:1.crontab–e2.*/1****ls-l/etc>>/... 查看详情

为啥使用 Celery 运行计划任务比使用 crontab 更可取?

】为啥使用Celery运行计划任务比使用crontab更可取?【英文标题】:WhywouldrunningscheduledtaskswithCelerybepreferableovercrontab?为什么使用Celery运行计划任务比使用crontab更可取?【发布时间】:2013-08-1319:25:38【问题描述】:考虑到Celery已经... 查看详情

Celery Beat 调度程序不会在 Django 中执行我的任务

...误。我想在每天的特定时间运行一个函数,这是我以前用crontab做的事情。Celery显然更适合这种 查看详情

celery实现定时任务crontab

Celery实现定时任务crontab一.搭建celery定时任务架构在项目中适合的位置新建一个定时任务目录celery_crontab,​​​在目录下创建config.py​​​,​​main.py​​,tasks.py三个文件,分别用于编写配置代码,定时任务实现代码,任务函数代码#... 查看详情

celery收下这捆芹菜!(代码片段)

...我们通常使用它来实现异步任务(asynctask)和定时任务(crontab)Celery官网:http://www.celeryproject.org/Celery官方文档英文版:http://docs.celeryproject.org/en/latest/index.htmlCelery官方文档中文版:http://docs.jinkan.org/docs/celery/Celery构成Task任务模... 查看详情

django+django-celery+celery的整合实战(代码片段)

...去参考官方文档,而且本篇的记录不一定正确,仅仅实现crontab的功能而已。希望深入学习的人可以参考http://docs.jinkan.org/docs/celery/。首先简单介绍一下,Celery是一个强大的分布式任务队列,它可以让任务的执行完全脱离主程序,... 查看详情

Celery 每日计划任务与 Crontab

】Celery每日计划任务与Crontab【英文标题】:CeleryDailyScheduledTaskswithCrontab【发布时间】:2019-02-0819:57:42【问题描述】:我对使用crontab的每日计划任务有疑问。这是我的celery.pyapp.conf.beat_schedule=\'run-cache-updater\':\'task\':\'tasks.run_cache_u... 查看详情

crontab 不能与 celery 多启动一起使用

】crontab不能与celery多启动一起使用【英文标题】:crontabnotworkingwithcelerymultistart【发布时间】:2014-06-1310:50:39【问题描述】:我现在正在尝试让Celery工作一段时间。当我同步测试时,我所有的crontab都可以正常工作sudocelery-Atestdjang... 查看详情

Django celery:安排重复任务,但开始日期固定

...重复任务。例如,一个任务以每分钟的间隔运行,或者由crontab指定的间隔,如每个月的第一天中午。然而,我真正想做的是安排一个固定 查看详情

django + celery - 如何在我的 django 应用程序中为 celery 设置 crontab 计划?

】django+celery-如何在我的django应用程序中为celery设置crontab计划?【英文标题】:django+celery-HowdoIsetupacrontabscheduleforceleryinmydjangoapp?【发布时间】:2014-07-1801:08:43【问题描述】:我正在按照这里的说明进行操作:http://celery.readthedocs.... 查看详情

通过 bash 脚本将 bash 脚本添加到 crontab

】通过bash脚本将bash脚本添加到crontab【英文标题】:addbashscripttocrontabviabashscript【发布时间】:2022-01-2010:48:20【问题描述】:我想将带有文件名的日期添加到我的bash脚本中,该脚本将通过脚本添加到crontab文件中。问题是crontab文... 查看详情

linux下定时任务(系统任务调度、用户任务调度)crontab使用详解

...行的任务,则自动执行该任务。用户在cron表(也被称为crontab文件)指定了定时任务,crontab也就是我们常见的定时任务设置命令。Linux下的任务调度分为两类,系统任务调度和用户任务调度。系统任务调度:系统周期性所要执行... 查看详情

schedule与apscheduler与celery

...lt;celery三者都支持定时任务配置:--schedule相当于linux下的crontab,使用最简单,但不支持动态添加任务和任务实例化,所以在实际项目中使用不多。--apschedule解决了schedule的不足,项目中定时任务使用最多--celery的功能强点在异步... 查看详情

使用 Celery 的单个 Django 模型的每个对象的不同 crontab

】使用Celery的单个Django模型的每个对象的不同crontab【英文标题】:DifferentcrontabforeachobjectsofsingleDjangomodelusingCelery【发布时间】:2019-09-2108:48:05【问题描述】:我能够创建celery_beat_schedule并且它可以工作。耶!但我想知道是否有任... 查看详情

0118定时任务调度

...程序,如对MySQL数据库的备份1.crond任务调度基本语法crontab[选项]常用选项演示:设置任务调度文件:/etc/crontab设置个人任务调度,执行crontab-e命令输入任务到调度文件:如:*/1****ls-l/etc>/tmp/to.txt命令即每... 查看详情

例行性工作(crontab)

例行性工作使用的是crontab这个命令来进行工作,根据设置将会循环下去,循环的时间从秒到年crontab的工作调度是由服务crond来控制1、用户的限制/etc/cron.deny   将不可以使用crontab的账号写入其中,若未记录,则可以使... 查看详情

Django celery crontab 每 30 秒 - 有可能吗?

】Djangocelerycrontab每30秒-有可能吗?【英文标题】:Djangocelerycrontabevery30seconds-isitevenpossible?【发布时间】:2012-05-2217:37:00【问题描述】:是否可以在特定时间运行djangocelerycrontab非常30秒?只有分钟、小时和天的设置。我有crontab工... 查看详情