Using Unicorn web server

Unicorn is a web server that uses forked processes to handle multiple incoming requests concurrently.

Deploy with Unicorn

You need to choose your web server at the time of initial build of the application. Changes to or from Passenger (the default web app server) will not be applied after your application has initially been analyzed. You can however change freely between other supported servers by simply updating your Gems and Procfile.

To run a Unicorn web server, add a line to your Procfile labeled as custom_web. Here is an example:

custom_web: bundle exec unicorn_rails -c config/unicorn.rb -E $RAILS_ENV

You should not daemonize the custom_web process. In other words, please do not use the -D or -daemonize flags in your initialization string. Please also make sure your config file does not enable daemonization.

We do not support old-style daemonization because it is more reliable to allow the system’s process manager (systemd) to handle persistent processes.

Warning

Please ensure to follow the conventions set out in the configuration below if you are having issues, and that you are using an up-to-date version of Unicorn.

Sample config file for Unicorn

This unicorn.rb configuration file is compatible with Cloud 66 requirements (following the Procfile initialization string above, this should be located under the config folder of your Rails app):


worker_processes 2

working_directory "#{ENV['STACK_PATH']}"

listen "#{ENV['CUSTOM_WEB_SOCKET_FILE']}", :backlog => 64

timeout 30

pid "#{ENV['CUSTOM_WEB_PID_FILE']}"

stderr_path "#{ENV['STACK_PATH']}/log/unicorn.stderr.log"
stdout_path "#{ENV['STACK_PATH']}/log/unicorn.stdout.log"

preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
	GC.copy_on_write_friendly = true

check_client_connection false

before_fork do |server, worker|
	old_pid = "#{ENV['CUSTOM_WEB_PID_FILE']}.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

	defined?(ActiveRecord::Base) and
		ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
	defined?(ActiveRecord::Base) and
		ActiveRecord::Base.establish_connection
end

Customizing shutdown and reload signals

The default shutdown command for Unicorn servers on Cloud 66 is USR2 and the default shutdown sequence for applications using systemd (our default process manager) is:

quit, 75, term, 15, kill

If your application still uses Bluepill (our legacy process manager), please refer to the separate guide on the subject.

If you need your web server to shut down using a different command, or in a particular sequence, or with longer or shorter delays, you can define a custom restart sequence in the procfile_metadata section of your Manifest file.

For non-web process signals, please consult our systemd guide.

Controlling Unicorn via your terminal

You can manage your web server directly from your terminal. The commands you need to use will depend on whether your servers use systemd or Bluepill to manage processes. To check which process manager your application uses:

(If you’d like to migrate from Bluepill to systemd, please follow the migration checklist.)

Cloud 66 uses the following signals to control Puma via systemd:

Stop the web server

sudo systemctl stop cloud66_web_server.service

Start the web server

sudo systemctl start cloud66_web_server.service

Restart the web server

sudo systemctl restart cloud66_web_server.service