Eric Koslow
Eric Koslow

Reputation: 2064

How to run a cron job in Heroku, with a Sinatra app

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

Upvotes: 5

Views: 3703

Answers (3)

include
include

Reputation: 2118

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

Upvotes: 3

Himanshu
Himanshu

Reputation: 482

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.

Upvotes: 12

Eric Koslow
Eric Koslow

Reputation: 2064

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!

Upvotes: 1

Related Questions