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

时间:2023-01-31 01:08:38

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.

我正在写一个很小的Sinatra应用程序,为了简单起见,我想在Heroku上托管它。但是,我所拥有的是一个废弃一些网站并每小时向我的数据库中添加一些数据的任务。目前,这只是作为需要执行的ruby脚本编写的。 Heroku有一个基于rake的cron工作。现在,如果这是一个rails应用程序,我可以很容易地做到这一点,但我想避免这种简单的混乱。

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

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

Thank you.

谢谢。

Eric

埃里克

3 个解决方案

#1


10  

You need a Rakefile like:

你需要一个Rakefile:

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.

Heroku会定期在您的应用中执行rake cron,具体取决于您是选择每小时还是每天选择“cron add-on”。

#2


3  

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

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

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

我没有在Heroku上尝试这些东西,但试一试并回复我们。

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

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 :)

为什么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

#3


1  

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.

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

Hope this helps other people.

希望这有助于其他人。

Cheers!

干杯!

#1


10  

You need a Rakefile like:

你需要一个Rakefile:

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.

Heroku会定期在您的应用中执行rake cron,具体取决于您是选择每小时还是每天选择“cron add-on”。

#2


3  

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

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

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

我没有在Heroku上尝试这些东西,但试一试并回复我们。

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

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 :)

为什么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

#3


1  

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.

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

Hope this helps other people.

希望这有助于其他人。

Cheers!

干杯!