jam-cloud/ruby/lib/jam_ruby/resque/resque_hooks.rb

55 lines
1.3 KiB
Ruby

require 'resque'
# https://devcenter.heroku.com/articles/forked-pg-connections
Resque.before_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
JamRuby::Stats.destroy!
end
Resque.after_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
config = {
influxdb_database: APP_CONFIG.influxdb_database,
influxdb_username: APP_CONFIG.influxdb_username,
influxdb_password: APP_CONFIG.influxdb_password,
influxdb_hosts: APP_CONFIG.influxdb_hosts,
influxdb_port: APP_CONFIG.influxdb_port,
influxdb_async: false # if we use async=true, the forked job will die before the stat is sent
}
JamRuby::Stats.init(config)
end
# for jobs that do not extend lonely job, just extend this module and get stats
module JamRuby
module ResqueStats
def around_perform(*args)
Stats.timer('job.stats') do
begin
yield
end
end
end
end
end
# for jobs that extend lonely job, we override around_perform already implemented in LonelyJob, and call into it
module Resque
module Plugins
module LonelyJob
def around_perform(*args)
Stats.timer('job.stats') do
begin
yield
ensure
unlock_queue(*args)
end
end
end
end
end
end