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

91 lines
2.0 KiB
Ruby
Raw Permalink Normal View History

2014-12-30 23:10:16 +00:00
require 'resque'
ENV['FORK_PER_JOB'] = 'false'
2014-12-30 23:10:16 +00:00
def shutdown
puts "Cleaning up resources..."
Stats.destroy!
EventMachine.stop_event_loop
puts "Terminated!"
exit!
end
Resque.before_first_fork do
current = Thread.current
Thread.new do
JamWebEventMachine.run_em(current)
end
2014-12-30 23:10:16 +00:00
#ActiveRecord::Base.establish_connection
2014-12-30 23:10:16 +00:00
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: true # if we use async=true, the forked job will die before the stat is sent
2014-12-30 23:10:16 +00:00
}
# handle these events and force a shutdown. this is required I think due to influxdb-client.
Signal.trap("TERM") do
shutdown
end
Signal.trap("INT") do
shutdown
end
2014-12-30 23:10:16 +00:00
JamRuby::Stats.init(config)
2016-04-06 18:35:38 +00:00
end
# https://devcenter.heroku.com/articles/forked-pg-connections
Resque.before_fork do
#defined?(ActiveRecord::Base) and
# ActiveRecord::Base.connection.disconnect!
#JamRuby::Stats.destroy!
# reconnect between jobs
ActiveRecord::Base.clear_active_connections!
Resque.logger = Rails.logger
end
Resque.after_fork do
#defined?(ActiveRecord::Base) and
# ActiveRecord::Base.establish_connection
2014-12-30 23:10:16 +00:00
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
require 'resque-lonely_job'
2014-12-30 23:10:16 +00:00
# for jobs that extend lonely job, we override around_perform already implemented in LonelyJob, and call into it
module Resque
module Plugins
module JamLonelyJob
2014-12-30 23:10:16 +00:00
def around_perform(*args)
Stats.timer('job.stats') do
super
2014-12-30 23:10:16 +00:00
end
end
end
end
end
Resque::Plugins::JamLonelyJob.module_eval { include Resque::Plugins::LonelyJob }