Skip to content

Instantly share code, notes, and snippets.

@julien51
Created March 29, 2010 14:51
Show Gist options
  • Save julien51/347907 to your computer and use it in GitHub Desktop.
Save julien51/347907 to your computer and use it in GitHub Desktop.
# Use at least one worker per core
worker_processes 16
# Help ensure your application will always spawn in the symlinked "current" directory that Capistrano sets up
working_directory "/var/www/apps/superfeedr.com/current"
# Listen on a Unix domain socket, use the default backlog size
listen "/tmp/unicorn.sock", :backlog => 1024
# Nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# Lets keep our process id's in one place for simplicity
pid "/var/www/apps/superfeedr.com/shared/pids/unicorn.pid"
# Logs are very useful for trouble shooting, use them
stderr_path "/var/log/unicorn/stderr.log"
stdout_path "/var/log/unicorn/stdout.log"
# Use "preload_app true"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
MongoMapper.database.connection.close
old_pid = '/tmp/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
##
# Start Redis
begin
redis_conf = YAML.load(File.read(File.expand_path(File.join(RAILS_ROOT,"config","redis.yml"))))[RAILS_ENV].symbolize_keys
$redis = Redis.new(redis_conf)
rescue
RAILS_DEFAULT_LOGGER.error("Couldn't connect to Redis Server : #{redis_conf.inspect}")
end
##
# Start Mongo
begin
include MongoMapper
mongo_conf = YAML.load(File.read(File.expand_path(File.join(RAILS_ROOT,"config","mongo.yml"))))[RAILS_ENV]
MongoMapper.database = mongo_conf["database"]
MongoMapper.connection = Mongo::Connection.new(mongo_conf["host"], mongo_conf["port"], :logger => Rails.logger)
MongoMapper.database.authenticate(mongo_conf["username"], mongo_conf["password"])
ActionController::Base.rescue_responses['MongoMapper::DocumentNotFound'] = :not_found
MongoMapper.database.connection.connect_to_master
rescue
RAILS_DEFAULT_LOGGER.error("Couldn't connect to Mongo Server : #{mongo_conf.inspect}")
end
##
# Start the Queue
begin
queue_conf = YAML.load(File.read(File.expand_path(File.join(RAILS_ROOT,"config","queue.yml"))))[RAILS_ENV]
$bunny = Bunny.new(:host => queue_conf["host"], :logging => queue_conf["logging"])
$bunny.start_session
$ping_queue = $bunny.queue(queue_conf["queue"])
rescue
RAILS_DEFAULT_LOGGER.error("Couldn't connect to RabbitMq Server : #{queue_conf.inspect}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment