Created
October 26, 2012 18:48
-
-
Save PatrickTulskie/3960648 to your computer and use it in GitHub Desktop.
Working resque pool setup for NewRelic
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 'resque/pool/tasks' | |
# this task will get called before resque:pool:setup | |
# preload the rails environment in the pool master | |
task "resque:setup" => :environment do | |
# generic worker setup, e.g. Hoptoad for failed jobs | |
end | |
task "resque:pool:setup" do | |
# Close any sockets or files in pool master | |
ActiveRecord::Base.connection.disconnect! | |
# This happens after resque-pool has forked your worker | |
# but before it performs Resque::Worker#startup | |
Resque::Pool.after_prefork do |job| | |
# Re-connect to your database | |
ActiveRecord::Base.establish_connection | |
# Load NewRelic here. We've already forked and we're good to go. | |
NewRelic::Agent.manual_start( | |
:dispatcher => :resque, | |
:sync_startup => true, | |
:start_channel_listener => true | |
) | |
Resque.before_first_fork do | |
# Normally you'd do this here if you aren't using resque-pool | |
# NewRelic::Agent.manual_start( | |
# :dispatcher => :resque, | |
# :sync_startup => true, | |
# :start_channel_listener => true | |
# ) | |
# Empty block so that you nuke newrelic_rpm's built in hooks | |
# You can do other things here though or just set this to nil | |
end | |
Resque.before_fork do |job| | |
NewRelic::Agent.register_report_channel(job.object_id) | |
end | |
Resque.after_fork do |job| | |
NewRelic::Agent.after_fork(:report_to_channel => job.object_id) | |
end | |
end | |
end |
This actually did not work for us, until we set the NEWRELIC_DISPATCHER env variable in our Rakefile, before it just wasn't detecting the resque dispatcher.
Just to further clarify, the NEWRELIC_DISPATCHER env variable (as referenced in the comment above) should be set to "resque", since New Relic normally sets that variable on agent start-up to whatever dispatcher it detects.
FYI, we've just released version 3.6.7 of the newrelic_rpm gem, which should support resque-pool out of the box!
Works great for me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it really work? I've been trying without success...