Heroku scheduler
Heroku Scheduler is an add-on for running administrative or maintenance tasks, or jobs, at scheduled time intervals. It’s the polyglot replacement of the Cron add-on, with more power and flexibility. And it’s free; you just pay for the dyno time consumed by the one-off tasks.
Scheduling Jobs
To schedule a frequency and time for a job, open the scheduler dashboard by finding the app in My Apps, clicking “General Info”, then selecting “Scheduler” from the Add-ons dropdown. On the Scheduler Dashboard, click “Add Job…”, enter a task, select a frequency and next run time. Note that the next run time for daily jobs is in UTC. If you want to schedule the job at a certain local time, add the proper UTC offset.
For example, add rake update_feed, select “Hourly” and “:30″ to update feeds every hour on the half-hour. Then add rake send_reminders, select “Daily” and “00:00″ to send reminders every day at midnight.
Defining Tasks
To create your cron task in Rails, copy the code below into lib/tasks/cron.rake and customize it to fit your needs.
For apps built on other frameworks, insert the same code into the Rakefile in your application’s root – you can create a blank Rakefile if you don’t already have one. If you’re not using Rails, remove the => :environment dependency in the task as that is Rails-specific.
desc "This task is called by the Heroku scheduler add-on" task :update_feed => :environment do puts "Updating feed..." NewsFeed.update puts "done." end
task :send_reminders => :environment do User.send_reminders end
If you’re using Python with the popular Fabric automation tool, you can define a fab
For example, add:
rake update_feed
select “Hourly” and “:30″ to update feeds every hour on the half-hour. Then add:
rake send_reminders
select “Daily” and “00:00″ to send reminders every day at midnight.
Create an account on Heroku
It used to be by invite, now the door is wide open. It is still free to use for most needs. Head over to Heroku. Create an account there or sign in.
Install the heroku gem
You need to install the heroku gem only once. Can’t remember if you did it before or not? See if you have it:
Install the Heroku Command-line Client (cmd prompt in Windows):
$ gem install heroku
$ gem update heroku
It may take a while to install, or update. Be patient.
Send your ssh key to Heroku
Once that’s completed, communicate your id_rsa.pub key to Heroku with this command.
$ heroku keys:add
You will be prompted to type in your Heroku credentials. This is why you needed to create an account on Heroku to begin with.
Deploy Your Rails Application with Heroku
Since Heroku provides you a PostgreSQL database for your app, edit yourGemfile and change this line:
gem 'mysql2'
To
gem 'pg'
And re-install your dependencies (to generate a new Gemfile.lock):
$ bundle install
Push your application to Heroku
Do a ‘git status’ to make sure that all your changes have been committed.
$ git status
$ git init
$ git add .
$ git commit -m "first commit"
Deploy to Heroku/bamboo-ree-1.8.7
Create the app on the bamboo-ree-1.8.7 stack:
$ heroku create --stack bamboo-ree-1.8.7
Then push your application to your remote repository on Heroku:
$ git push heroku master
Before looking at the app on the web, let’s check the state of the app’s processes:
$ heroku ps
Console:
Stack allows you to launch a Rails console process attached to your local terminal for experimenting in your app’s environment:
$ heroku console
Rake can be run as an attached process exactly like the console:
$ heroku rake db:create
$ heroku rake db:migrate
Note: If you’re pushing code from another branch than master, replace master with the name of the branch you’re working on.
Prepared by
Suresh & Sankar