Created
March 1, 2010 10:10
-
-
Save ono/318256 to your computer and use it in GitHub Desktop.
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
# BaseJob is a super class of Resque jobs which you want to track with RPM | |
class BaseJob | |
include NewRelic::Agent::Instrumentation::ControllerInstrumentation | |
# This is called from Resque. | |
# To be wrapped with the common operation, please describe job in the instance's perform method. | |
def self.perform(*args) | |
NewRelic::Control.instance['forked_job'] = true | |
self.new.perform_wrapper(*args) | |
ensure | |
NewRelic::Agent.shutdown | |
end | |
# Sends a hint to our monitoring system, newrelic rpm | |
def add_custom_parameters(key, value) | |
NewRelic::Agent.add_custom_parameters(key => value.to_s) | |
end | |
# Calls :perform with monitoring on newrelic rpm | |
def perform_wrapper(*args) | |
# :task seems available only for charged plan. When you test this with a free plan, you have to get rid of it. | |
# perform_action_with_newrelic_trace(:name=>'perform') do | |
perform_action_with_newrelic_trace(:name=>'perform', :category => :task) do | |
begin | |
perform(*args) | |
ensure | |
logger.flush if logger && logger.respond_to?(:flush) | |
end | |
end | |
end | |
# Override this method instead of the class method. | |
def perform(*args) | |
end | |
def logger | |
RAILS_DEFAULT_LOGGER | |
end | |
end | |
# Test purpose job. It just outputs the information to log. | |
# e.g. | |
# Resque.enqueue HelloWorldJob, 'RPM' | |
class HelloWorldJob < BaseJob | |
@queue = :low | |
def perform(who='World') | |
# This sends the information to NewRelic RPM | |
add_custom_parameters :whom, who | |
logger "#{Time.now}: Hello #{who}!" | |
sleep(3) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment