Created
May 10, 2012 00:05
-
-
Save bkayser/2649928 to your computer and use it in GitHub Desktop.
Example of monitoring a ruby background job with New Relic
This file contains hidden or 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
class Account | |
include NewRelic::Agent::MethodTracer | |
def check_usage | |
end | |
def send_invoice | |
end | |
add_method_tracer :check_usage | |
add_method_tracer :send_invoice | |
end |
This file contains hidden or 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 File.expand_path('../../config/application', __FILE__) | |
JobsExample::Application.initialize! | |
require 'optparse' | |
require_dependency 'script/task_instrumentation' | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on("-v", "--verbose", "Run with extra logging") { options[:verbose] = true } | |
end | |
class Job | |
include NewRelic::Agent::Instrumentation::ControllerInstrumentation | |
def initialize(options={}) | |
@verbose = options[:verbose] | |
end | |
def run | |
Account.all.each do | account | | |
process_account(account) | |
end | |
end | |
private | |
def process_account | |
account.check_usage | |
account.send_invoice | |
end | |
add_transaction_tracer :process_account, | |
:name => 'process_one_account', # Change the label from "process_account" to "process_one_account" | |
:category => "OtherTransaction/nightly" # Put in the background tasks under "Nightly" | |
end | |
# Start the New Relic agent synchronously | |
NewRelic::Agent.manual_start :app_name => 'Background Jobs', :transaction_tracer => { :transaction_threshold => 1.5 } | |
status = Job.new(options).run | |
exit status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment