Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created October 26, 2012 18:48
Show Gist options
  • Save PatrickTulskie/3960648 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/3960648 to your computer and use it in GitHub Desktop.
Working resque pool setup for NewRelic
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
@gregtaylor99
Copy link

Works great for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment