Created
December 1, 2010 22:06
-
-
Save carlhoerberg/724318 to your computer and use it in GitHub Desktop.
Resque + New Relic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'lib/scaler' | |
class RecreateFullZip | |
@queue = :full_zip | |
def self.perform | |
zip = CvFile.get_all_as_zip | |
AWS::S3::S3Object.store("all.zip", zip, 'datatjejcv') | |
end | |
extend HerokuResqueAutoScale | |
end | |
class AsyncMail | |
@queue = :mail | |
def self.perform(from, to, subject, body) | |
Pony.mail(:from => from, :to => to, :subject => subject, :body => body) | |
end | |
extend HerokuResqueAutoScale | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :environment do | |
DataMapper.setup(:default, ENV['DATABASE_URL']) | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => ENV['AMAZON_ACCESS_KEY_ID'], | |
:secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY']) | |
uri = URI.parse(ENV["REDISTOGO_URL"]) | |
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) | |
Pony.options = { | |
:via => :smtp, | |
:via_options => { | |
:address => "smtp.sendgrid.net", | |
:port => "25", | |
:authentication => :plain, | |
:user_name => ENV['SENDGRID_USERNAME'], | |
:password => ENV['SENDGRID_PASSWORD'], | |
:domain => ENV['SENDGRID_DOMAIN'] }} | |
end | |
require 'resque/tasks' | |
require 'lib/jobs' | |
require 'rpm_contrib' if ENV['RACK_ENV'] == 'production' | |
task 'resque:setup' => :environment do | |
ENV['QUEUE'] = '*' | |
end | |
task 'jobs:work' => 'resque:work' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'heroku' | |
module HerokuResqueAutoScale | |
module Scaler | |
class << self | |
@@heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASS']) | |
def workers | |
@@heroku.info(ENV['HEROKU_APP'])[:workers].to_i | |
end | |
def workers=(qty) | |
@@heroku.set_workers(ENV['HEROKU_APP'], qty) | |
end | |
def job_count | |
Resque.info[:pending].to_i | |
end | |
end | |
end | |
def after_perform_scale_down(*args) | |
#NewRelic::Agent.shutdown | |
# Nothing fancy, just shut everything down if we have no jobs | |
Scaler.workers = 0 if Scaler.job_count.zero? | |
end | |
def after_enqueue_scale_up(*args) | |
if Scaler.workers < 1 | |
#NewRelic::Agent.manual_start | |
Scaler.workers = 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment