Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
Eric Koslow Avatar asked Oct 06 '10 18:10

Eric Koslow


2 Answers

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.

like image 67
Himanshu Avatar answered Nov 15 '22 00:11

Himanshu


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
like image 43
include Avatar answered Nov 14 '22 23:11

include