如何使用 Sinatra 应用程序在 Heroku 中运行 cron 作业

发布于 2024-09-26 12:58:33 字数 369 浏览 1 评论 0原文

我正在编写一个小型 Sinatra 应用程序,为了简单起见,我想将其托管在 Heroku 上。但是,我的任务是每小时废弃一些网站并将一些数据添加到我的数据库中。目前这只是编写为需要执行的 ruby​​ 脚本。 Heroku 拥有的是基于 rake 的 cron 作业。现在,如果这是一个 Rails 应用程序,我可以轻松地做到这一点,但我想避免像这样简单的事情带来的混乱。

有办法避免这种情况吗?或者我是否还必须在我的应用程序中安装 rake?

谢谢。

埃里克

I'm writing a tiny Sinatra app, and I want to host it on Heroku for simplicity sake. But, what I have is a task that scraps some sites and adds some data into my database every hour. Currently this is just written as a ruby script that needs to be executed. What Heroku has is a rake based cron job. Now if this was a rails app, I could easily do this, but I want to avoid the clutter for something as simple as this.

Is there a way to avoid this? Or do I have to install rake as well with my app?

Thank you.

Eric

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

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

发布评论

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

评论(3

城歌 2024-10-03 12:58:33

你需要一个像这样的 Rakefile:

desc "This task is called by the Heroku cron add-on"
task :cron do
 # Do something
end

Heroku 定期在你的应用程序中执行 rake cron,具体取决于你是否选择了“cron 附加组件”为每小时或每天。

You need a Rakefile like:

desc "This task is called by the Heroku cron add-on"
task :cron do
 # Do something
end

Heroku periodically executes rake cron in your app depending on whether you have selected the "cron add-on" to be hourly or daily.

剩一世无双 2024-10-03 12:58:33

你需要看看鲁弗斯。鲁弗斯是你的朋友。当您的应用程序加载时,Rufus 将成为您的 crontab。

我没有在 Heroku 上尝试过这个东西,但是,尝试一下并回复我们。

http://codex.heroku.com/past/2010/4/15/ rufus_scheduler_for_my_tiny_sinatra_apps/

为什么 Rufus 很酷?好吧,检查一下,它很干净:)

$ sudo gem install rufus-scheduler

require 'rubygems'
require 'rufus/scheduler'

scheduler = Rufus::Scheduler.start_new

scheduler.cron '00 23 30 * *' do
  # Run every 30 days at 23h00m
  # ...your magic code goes here...
end

You need to check out Rufus. Rufus is your friend. Rufus will be your crontab while your app is loaded.

I did not try this stuff on Heroku but, give it a try and reply to us.

http://codex.heroku.com/past/2010/4/15/rufus_scheduler_for_my_tiny_sinatra_apps/

Why Rufus is cool? Well check this out, it's clean :)

$ sudo gem install rufus-scheduler

require 'rubygems'
require 'rufus/scheduler'

scheduler = Rufus::Scheduler.start_new

scheduler.cron '00 23 30 * *' do
  # Run every 30 days at 23h00m
  # ...your magic code goes here...
end
北凤男飞 2024-10-03 12:58:33

又看了一遍,看来我这个问题太仓促了。

对于不是 Rails 的应用程序,只需创建一个 Rakefile 并将任务放在那里。

希望这对其他人有帮助。

干杯!

Looked again and looks like I jumped the gun on the question.

For applications that aren't Rails, one just has to create a Rakefile and put the task there.

Hope this helps other people.

Cheers!

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