Skip to content

Instantly share code, notes, and snippets.

@nthj
Created February 26, 2014 23:22
Show Gist options
  • Save nthj/9240938 to your computer and use it in GitHub Desktop.
Save nthj/9240938 to your computer and use it in GitHub Desktop.
unicorn
# This file is used by Rack-based servers to start the application.
$stdout.sync = true
require 'unicorn/worker_killer'
##
## Maximum Requests
##
max_request_min = 500
max_request_max = 600
# Max requests per worker
use Unicorn::WorkerKiller::MaxRequests, max_request_min, max_request_max
##
## Maximum Memory
##
oom_min = (1024**2) * (ENV['WORKER_MEMORY'] || 340).to_i
oom_max = (1024**2) * (ENV['WORKER_MEMORY'] || 340).to_i + 25
# Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, oom_min, oom_max
##
## Blastoff
##
require ::File.expand_path('../config/environment', __FILE__)
run DiscoveryEngine::Application
# amount of unicorn workers to spin up
worker_processes (ENV['WORKER_PROCESSES'] || 5).to_i
# restarts workers that timeout
timeout (ENV['WORKER_TIMEOUT'] || 15).to_i
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM. Wait for master to send QUIT'
end
Sidekiq.configure_client do |config|
config.redis = { size: 1 }
end
ActiveRecord::Base.configurations[Rails.env].tap do |config|
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || 2
ActiveRecord::Base.establish_connection(config)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment