我如何最好地运行每日脚本,该脚本在数字海洋应用程序平台中使用外部URL的内容更新我的Django应用程序?

发布于 2025-02-03 13:13:00 字数 157 浏览 1 评论 0原文

我在数字海洋应用程序平台上设置了Django Web应用程序。我想每天更新我的Django应用程序,其中包括来自外部URL的内容。不幸的是,APP平台中没有CRON作业。

具体来说,我想从外部URL获取图像,尝试下载图像,并在下载成功(如果下载成功)中在我的Django应用中进行更新。

I have a Django Web App set up on the Digital Ocean App Platform. I want to update my Django App daily with content from external URLs. Unfortunately, cron jobs are not available in the App Platform.

Specifically, I want to fetch images from external URLs, attempt to download the images, and update it in my Django App if the download was successful.

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

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

发布评论

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

评论(1

花海 2025-02-10 13:13:00

您可以考虑配置芹菜并将芹菜节拍用于django,在不需要Cronjob的情况下,您有许多配置计划任务的选项。

文档在这里

daemonize芹菜

您将如何使用芹菜节拍的示例:

from celery import Celery
from celery.schedules import crontab

app = Celery()

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 10 seconds.
    sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')

    # Calls test('world') every 30 seconds
    sender.add_periodic_task(30.0, test.s('world'), expires=10)

    # Executes every Monday morning at 7:30 a.m.
    sender.add_periodic_task(
        crontab(hour=7, minute=30, day_of_week=1),
        test.s('Happy Mondays!'),
    )

@app.task
def test(arg):
    print(arg)

@app.task
def add(x, y):
    z = x + y
    print(z)

You can consider configuring celery and using celery beat for django, there you have many options configuring scheduled tasks without the need of a cronjob.

Documentation here

Daemonize Celery here

Example of how you would use celery beat:

from celery import Celery
from celery.schedules import crontab

app = Celery()

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 10 seconds.
    sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')

    # Calls test('world') every 30 seconds
    sender.add_periodic_task(30.0, test.s('world'), expires=10)

    # Executes every Monday morning at 7:30 a.m.
    sender.add_periodic_task(
        crontab(hour=7, minute=30, day_of_week=1),
        test.s('Happy Mondays!'),
    )

@app.task
def test(arg):
    print(arg)

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