Skip to content

Instantly share code, notes, and snippets.

@mPanasiewicz
Last active August 21, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mPanasiewicz/e69d53cdf751ba719b07e3beb3a74638 to your computer and use it in GitHub Desktop.
Save mPanasiewicz/e69d53cdf751ba719b07e3beb3a74638 to your computer and use it in GitHub Desktop.
Sneakers.configure(
heartbeat: 2,
exchange: 'sneakers',
exchange_type: :direct,
durable: true,
workers: Rails.application.secrets.sneakers_processes,
threads: Rails.application.secrets.sneakers_threads,
prefetch: Rails.application.secrets.sneakers_threads, # this is not a typo: the doc says it's good to match prefetch and threads values
timeout_job_after: 60,
share_threads: true, # both options are supported by `after_hook` hereafter
hooks: {
before_fork: -> {
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.connection_pool.disconnect!
StatsBase.connection_pool.disconnect!
Sneakers.logger.info('Disconnected from ActiveRecord')
end
},
after_fork: -> {
def determine_db_pool_size
worker_classes = Sneakers::Utils.parse_workers(ENV["WORKERS"]).first
threads_per_worker = Rails.application.secrets.sneakers_threads
if Sneakers.const_get(:CONFIG)[:share_threads]
3 + worker_classes.count * 3 + threads_per_worker
else
3 + worker_classes.count * 3 + worker_classes.count * threads_per_worker
end
end
def reconnect_default_database(db_pool_size)
ActiveRecord::Base.establish_connection(Rails.application.config.database_configuration[Rails.env].merge("pool" => db_pool_size))
end
def reconnect_stats_database(db_pool_size)
if Rails.application.secrets.stats_database_url.present?
StatsBase.establish_connection(Rails.application.secrets.stats_database_url + "?pool=#{db_pool_size}")
elsif Rails.application.config.database_configuration.has_key? "stats_#{Rails.env}"
StatsBase.establish_connection(Rails.application.config.database_configuration["stats_#{Rails.env}"].merge("pool" => db_pool_size))
else
raise StandardError.new("Stats database not properly configured.")
end
end
ActiveSupport.on_load(:active_record) do
db_pool_size = determine_db_pool_size
reconnect_default_database(db_pool_size)
reconnect_stats_database(db_pool_size)
Sneakers.logger.info("Connected to ActiveRecord")
end
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment