如何使用Heroku和Django实现简单的cron作业

时间:2022-12-17 01:08:29

I have a Django app running on Heroku and using a Heroku Postgres database. It's pretty low-traffic, and I'd like to keep the cost per month as low as possible (right now, it's running for free with Heroku's 750 free hrs/month). I have some simple cron jobs, implemented as calls to Django management commands, that I'd like to run, but I'm not sure how to do this with Heroku. I've read a bit about Scheduler, but am still confused about a few things. First, if Scheduler runs on a self-contained dyno, how can it make a call to Django's manage.py file? Second, how many dyno-hours does a Scheduler task usually take up? If the task only takes 30 seconds to run, for example, am I only billed for 30 seconds of usage, or are there more costs associated with spinning up (and then down) the dyno?

我有一个Django应用程序在Heroku上运行,并使用Heroku Postgres数据库。这是相当低的流量,我想保持每月尽可能低的成本(现在,它是免费运行的与Heroku的750免费hrs/月)。我有一些简单的cron作业,实现为对Django管理命令的调用,我想要运行它们,但是我不确定如何使用Heroku来实现这些。我读过一些关于调度器的文章,但仍然对一些事情感到困惑。首先,如果调度程序运行在一个自包含的dyno上,它如何调用Django的管理。py文件?第二,调度器任务通常要占用多少动态时间?例如,如果任务只需要30秒就可以运行,那么我是否只需要30秒的使用时间,或者是否需要花费更多的成本来旋转(然后向下)dyno?

2 个解决方案

#1


1  

First, if Scheduler runs on a self-contained dyno, how can it make a call to Django's manage.py file?

首先,如果调度程序运行在一个自包含的dyno上,它如何调用Django的管理。py文件?

When you upload (push) your project, it is compiled into a slug. This slug is distributed to every dyno that is running your web app, and every 1 off dyno as well. This means that there will be a copy of manage.py on every dyno that you run, either through scheduler, or the heroku run command.

当你上传(推)你的项目,它被编译成一个鼻涕虫。这个段代码被分发给运行web应用的每一个dyno,以及每一个dyno。这意味着将会有一个管理的副本。通过调度器或heroku运行命令运行的每个dyno上的py。


Second, how many dyno-hours does a Scheduler task usually take up?

第二,调度器任务通常要占用多少动态时间?

Scheduler add-on runs one-off dynos that will count toward your dyno-hours that you will be charged for each month.

调度器插件运行一次性的dynos,它将计算你的dyno小时,你将被收取每月。


If the task only takes 30 seconds to run, for example, am I only billed for 30 seconds of usage, or are there more costs associated with spinning up (and then down) the dyno?

例如,如果任务只需要30秒就可以运行,那么我是否只需要30秒的使用时间,或者是否需要花费更多的成本来旋转(然后向下)dyno?

Any time spent executing a one-off dyno will contribute to usage and will be charged just like any other dyno.

任何时候执行一次性的dyno将有助于使用,并将像其他的dyno一样受到指控。

This means that using 30 seconds for a task takes 30 seconds of dyno time (REF). I don't believe that you're charged for spin-up/spin-down time, but you will be charged for the time that python takes to load your app to the point that it can run your management command, so try to keep your app as light as possible in terms of boostrap time.

这意味着对一个任务使用30秒需要30秒的动态时间(REF)。我不相信你会因为自旋/自旋时间而被收费,但是你会因为python加载你的应用程序到它可以运行你的管理命令的时间而被收费,所以在引导时间方面尽量保持你的应用程序尽可能的轻。

#2


1  

Actually, you can try APScheduler instead.

实际上,你可以试试APScheduler。

It allows even 1 minute period task and is totally free.

它甚至允许一分钟的任务,而且是完全免费的。

You just have to define the clock process and turn on the clock.

你只需要定义时钟进程并打开时钟。

You can see the step-by-step tutorial on the page.

您可以在页面上看到逐步教程。

#1


1  

First, if Scheduler runs on a self-contained dyno, how can it make a call to Django's manage.py file?

首先,如果调度程序运行在一个自包含的dyno上,它如何调用Django的管理。py文件?

When you upload (push) your project, it is compiled into a slug. This slug is distributed to every dyno that is running your web app, and every 1 off dyno as well. This means that there will be a copy of manage.py on every dyno that you run, either through scheduler, or the heroku run command.

当你上传(推)你的项目,它被编译成一个鼻涕虫。这个段代码被分发给运行web应用的每一个dyno,以及每一个dyno。这意味着将会有一个管理的副本。通过调度器或heroku运行命令运行的每个dyno上的py。


Second, how many dyno-hours does a Scheduler task usually take up?

第二,调度器任务通常要占用多少动态时间?

Scheduler add-on runs one-off dynos that will count toward your dyno-hours that you will be charged for each month.

调度器插件运行一次性的dynos,它将计算你的dyno小时,你将被收取每月。


If the task only takes 30 seconds to run, for example, am I only billed for 30 seconds of usage, or are there more costs associated with spinning up (and then down) the dyno?

例如,如果任务只需要30秒就可以运行,那么我是否只需要30秒的使用时间,或者是否需要花费更多的成本来旋转(然后向下)dyno?

Any time spent executing a one-off dyno will contribute to usage and will be charged just like any other dyno.

任何时候执行一次性的dyno将有助于使用,并将像其他的dyno一样受到指控。

This means that using 30 seconds for a task takes 30 seconds of dyno time (REF). I don't believe that you're charged for spin-up/spin-down time, but you will be charged for the time that python takes to load your app to the point that it can run your management command, so try to keep your app as light as possible in terms of boostrap time.

这意味着对一个任务使用30秒需要30秒的动态时间(REF)。我不相信你会因为自旋/自旋时间而被收费,但是你会因为python加载你的应用程序到它可以运行你的管理命令的时间而被收费,所以在引导时间方面尽量保持你的应用程序尽可能的轻。

#2


1  

Actually, you can try APScheduler instead.

实际上,你可以试试APScheduler。

It allows even 1 minute period task and is totally free.

它甚至允许一分钟的任务,而且是完全免费的。

You just have to define the clock process and turn on the clock.

你只需要定义时钟进程并打开时钟。

You can see the step-by-step tutorial on the page.

您可以在页面上看到逐步教程。