Django芹菜周期性任务没有在上述crontab上运行

发布于 2025-01-29 12:40:38 字数 1316 浏览 0 评论 0原文

我正在使用以下软件包。

celery==5.1.2
Django==3.1

我有2个定期芹菜任务,其中我希望第一个任务每15分钟运行一次,第二个任务每20分钟运行一次。但是问题在于,第一个任务是按时运行,而第二个任务未运行。

尽管我在控制台上收到一条消息:

Scheduler: Sending due task <task_name> (<task_name>)

但是该功能内部的日志并未在控制台上。

请找到以下文件

芹菜

from celery import Celery, Task
app = Celery('settings')
...

class PeriodicTask(Task):
    @classmethod
    def on_bound(cls, app):
        app.conf.beat_schedule[cls.name] = {
            "schedule": cls.run_every,
            "task": cls.name,
            "args": cls.args if hasattr(cls, "args") else (),
            "kwargs": cls.kwargs if hasattr(cls, "kwargs") else {},
            "options": cls.options if hasattr(cls, "options") else {}
        }

from celery.schedules import crontab
from settings.celery import app, PeriodicTask
...

@app.task(
    base=PeriodicTask,
    run_every=crontab(minute='*/15'),
    name='task1',
    options={'queue': 'queue_name'}
)
def task1():
    logger.info("task1 called")

@app.task(
    base=PeriodicTask,
    run_every=crontab(minute='*/20'),
    name='task2'
)
def task2():
    logger.info("task2 called")

。谢谢!

I am using the below packages.

celery==5.1.2
Django==3.1

I have 2 periodic celery tasks, in which I want the first task to run every 15 mins and the second to run every 20 mins. But the problem is that the first task is running on time, while the second is not running.

Although I'm getting a message on console:

Scheduler: Sending due task <task_name> (<task_name>)

But the logs inside that function are not coming on the console.

Please find the following files,

celery.py

from celery import Celery, Task
app = Celery('settings')
...

class PeriodicTask(Task):
    @classmethod
    def on_bound(cls, app):
        app.conf.beat_schedule[cls.name] = {
            "schedule": cls.run_every,
            "task": cls.name,
            "args": cls.args if hasattr(cls, "args") else (),
            "kwargs": cls.kwargs if hasattr(cls, "kwargs") else {},
            "options": cls.options if hasattr(cls, "options") else {}
        }

tasks.py

from celery.schedules import crontab
from settings.celery import app, PeriodicTask
...

@app.task(
    base=PeriodicTask,
    run_every=crontab(minute='*/15'),
    name='task1',
    options={'queue': 'queue_name'}
)
def task1():
    logger.info("task1 called")

@app.task(
    base=PeriodicTask,
    run_every=crontab(minute='*/20'),
    name='task2'
)
def task2():
    logger.info("task2 called")

Please help me to find the bug here. Thanks!

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

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

发布评论

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

评论(1

不气馁 2025-02-05 12:40:38

您是否启用了芹菜的登录方式。文档说,要记录您必须启用记录。这是文档的链接 celery docs 参考伐木部分。

Have you enabled logging in celery. The docs says that for logging you have to enable logging. Here is the link for the docs celery docs refer to logging section.

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