Created
July 8, 2011 11:06
-
-
Save ono/1071604 to your computer and use it in GitHub Desktop.
base_job.rb
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 class is an abstruct class for all jobs. | |
# The common operations such as newrelic monitoring should be written here. | |
# Jobs which is inherited from this class have to override instance method of perform instead of the class method to be monitored. | |
# Please check hello_world_job.rb as an example. | |
class BaseJob | |
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation | |
def self.skip_mode? | |
$rails_config && $rails_config.app_config && $rails_config.app_config['resque_skip_queue'] | |
end | |
# 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['resque'] = true unless skip_mode? | |
self.new.perform_wrapper(*args) | |
ensure | |
# temporarily disabled | |
# NewRelic::Agent.shutdown unless skip_mode? | |
end | |
# Sends a hint to our monitoring system, newrelic rpm | |
def record_hint(key, value) | |
value = value.inspect unless value.is_a?(String) | |
value.gsub! /psql/, "****" | |
# temporarily disabled | |
# NewRelic::Agent.add_custom_parameters(key => value.to_s) | |
# logger.info "#{key}:#{value}" | |
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 | |
# temporarily disabled | |
# perform_action_with_newrelic_trace(:name=>'perform', :category => :task) do | |
begin | |
ActiveRecord::Base.without_memcache do | |
# release connection to pool. In this way, you can ensure to re-create conection when it is reset. #5319 | |
ActiveRecord::Base.connection_handler.connection_pools.values.each do |pool| | |
pool.release_connection | |
end | |
# executes job | |
perform(*args) | |
end | |
rescue=>e | |
# logging | |
record_hint("connection", ActiveRecord::Base.connection) | |
record_hint("Product Class", Product.object_id) | |
record_hint("Product attributes", Product.attribute_names ) | |
record_hint("Registration Class", Registration.object_id) | |
record_hint("Registration attributes", Registration.attribute_names ) | |
record_hint("GraderGroup Class", GraderGroup.object_id) | |
record_hint("GraderGroup attributes", GraderGroup.attribute_names ) | |
issue_number = ApplicationController.generate_issue_number | |
logger.error "##{issue_number}: Resque Error: #{e.to_s}" | |
logger.error "##{issue_number}: Resque Error: #{e.backtrace}" | |
if e.class == ActiveRecord::StatementInvalid | |
# it's likely deadlock error. add more logging. #4420 | |
logger.error "##{issue_number}: Current Activities: " + PgStatActivity.find(:all, :limit=>200, :conditions=>["current_query <> '<IDLE>'"]).inspect | |
end | |
raise e | |
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 | |
# add_transaction_tracer :perform_wrapper, :name=>'perform', :category => :task | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment