2014-12-30 23:10:16 +00:00
|
|
|
require 'resque'
|
|
|
|
|
|
2015-01-01 02:39:22 +00:00
|
|
|
ENV['FORK_PER_JOB'] = 'false'
|
2014-12-30 23:10:16 +00:00
|
|
|
|
2015-01-01 02:39:22 +00:00
|
|
|
def shutdown
|
|
|
|
|
puts "Cleaning up resources..."
|
|
|
|
|
Stats.destroy!
|
|
|
|
|
EventMachine.stop_event_loop
|
|
|
|
|
puts "Terminated!"
|
|
|
|
|
exit!
|
2014-01-31 18:37:43 +00:00
|
|
|
end
|
|
|
|
|
|
2015-01-01 02:39:22 +00:00
|
|
|
Resque.before_first_fork do
|
2015-01-11 22:39:29 +00:00
|
|
|
current = Thread.current
|
|
|
|
|
Thread.new do
|
|
|
|
|
JamWebEventMachine.run_em(current)
|
|
|
|
|
end
|
2014-12-30 23:10:16 +00:00
|
|
|
|
2015-01-01 02:39:22 +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,
|
2015-01-01 02:39:22 +00:00
|
|
|
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
|
|
|
}
|
2015-01-01 02:39:22 +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
|
|
|
|
2015-01-01 02:39:22 +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!
|
2015-01-11 21:43:42 +00:00
|
|
|
|
|
|
|
|
# reconnect between jobs
|
2016-07-17 15:16:27 +00:00
|
|
|
ActiveRecord::Base.clear_active_connections!
|
|
|
|
|
|
|
|
|
|
Resque.logger = Rails.logger
|
2015-01-01 02:39:22 +00:00
|
|
|
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
|
|
|
|
|
|
2015-01-01 05:17:47 +00:00
|
|
|
|
|
|
|
|
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
|
2015-01-01 05:17:47 +00:00
|
|
|
module JamLonelyJob
|
2014-12-30 23:10:16 +00:00
|
|
|
def around_perform(*args)
|
|
|
|
|
Stats.timer('job.stats') do
|
2015-01-01 05:17:47 +00:00
|
|
|
super
|
2014-12-30 23:10:16 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-01 05:17:47 +00:00
|
|
|
|
|
|
|
|
Resque::Plugins::JamLonelyJob.module_eval { include Resque::Plugins::LonelyJob }
|
|
|
|
|
|