Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created February 13, 2013 20:49
Show Gist options
  • Save TimFletcher/4948117 to your computer and use it in GitHub Desktop.
Save TimFletcher/4948117 to your computer and use it in GitHub Desktop.
Simple hire/fire for Heroku workers
class GenerateCardsJob < Struct.new(:params)
include HerokuWorker
def perform
card_number = params[:cards][:number_of_cards]
card_number.to_i.times do |i|
card = Card.create!({
season_id: params[:season_id],
batch: params[:cards][:batch].parameterize,
purchasable: params[:cards][:purchasable],
ordinal: i+1
})
p card.errors.messages unless card.valid?
end
end
def success(job)
fire_worker if ['production', 'staging'].include?(Rails.env)
end
end
module HerokuWorker
def connection
return @connection if @connection
raise 'The Heroku API key is mandatory' if ENV['HEROKU_API_KEY'].blank?
@connection = Heroku::API.new(:api_key => ENV['HEROKU_API_KEY'])
end
def app_name
return @app_name if @app_name
raise 'The Heroku application name is mandatory' if ENV['HEROKU_APP_NAME'].blank?
@app_name = ENV['HEROKU_APP_NAME']
end
def hire_worker
begin
self.connection.post_ps_scale(self.app_name, 'worker', '1')
rescue Heroku::API::Errors::RequestFailed => e
ActiveSupport::JSON.decode(e.response.body)['error'] << " #{name}"
end
end
def fire_worker
begin
self.connection.post_ps_scale(self.app_name, 'worker', '0')
rescue Heroku::API::Errors::NotFound => e
ActiveSupport::JSON.decode(e.response.body)['error'] << " #{name}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment