Skip to content

Instantly share code, notes, and snippets.

@angelbotto
Created July 31, 2013 09:55
Show Gist options
  • Save angelbotto/6120832 to your computer and use it in GitHub Desktop.
Save angelbotto/6120832 to your computer and use it in GitHub Desktop.
keepalive in padrino and heroku easy xd

Heroku Keepalive is a little ruby script that uses the Clockwork gem to keep your free heroku apps up and responsive. It does so by simply pinging whatever endpoints you specify at a specific interval.

Test locally (if you want)

foreman start

12:32:32 keepalive.1  | started with pid 9702
12:32:33 keepalive.1  | I, [2012-07-06T12:32:33.386622 #9702]  INFO -- : Starting clock for 1 events: [ ping ]
12:32:33 keepalive.1  | I, [2012-07-06T12:32:33.386750 #9702]  INFO -- : Triggering 'ping'
12:32:33 keepalive.1  | Contacted url1 in 329ms
12:32:33 keepalive.1  | Contacted url2 in 260ms
12:32:34 keepalive.1  | FAILED to contact url3 -- getaddrinfo: nodename nor servname provided, or not known

Start the keepalive process with heroku

heroku scale keepalive=1
source 'http://rubygems.org'
ruby "1.9.3"
gem 'httparty'
gem 'clockwork'
gem 'foreman'
require 'rubygems'
require 'clockwork'
require 'httparty'
include Clockwork
@urls = ["url1","url2","url3"]
if ENV['INTERVAL']
interval = ENV['INTERVAL'].to_i
abort "INTERVAL must be value in seconds greater than 0" unless interval > 0
else
interval = 10
end
abort "ENDPOINTS must be comma-separated list of http endpoints to query for keepalive" unless ENV['ENDPOINTS']
every(interval, 'ping') {
@urls.each do |endpoint|
s = Time.now
begin
HTTParty.get(endpoint)
duration = (Time.now - s).to_f * 1000
puts "Contacted #{endpoint} in #{duration.round}ms"
rescue Exception => e
puts "FAILED to contact #{endpoint} -- #{e.message}"
#puts e.backtrace.collect{|line| " " + line}.join("\n")
end
end
}
keepalive: foreman run clockwork keepalive.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment