Monitoring Unicorn with Bluepill
November 06, 2009At Serious Business, we recently switched to using unicorn and bluepill. One of the features we implemented in bluepill was the ability to monitor child processes. This was built with Unicorn's workers in mind. Here's the config file we're using to monitor our Unicorn workers:
1 Bluepill.application("app_name-production") do |app| 2 app.process("unicorn") do |process| 3 process.pid_file = File.join(RAILS_ROOT, 'tmp', 'pids', 'unicorn.pid') 4 process.working_dir = RAILS_ROOT 5 6 process.start_command = "/usr/bin/env #{env_vars} unicorn -Dc config/unicorn.rb -E production" 7 process.stop_command = "kill -QUIT {{PID}}" 8 process.restart_command = "kill -USR2 {{PID}}" 9 10 process.uid = process.gid = 'deploy' 11 12 process.start_grace_time = 8.seconds 13 process.stop_grace_time = 5.seconds 14 process.restart_grace_time = 13.seconds 15 16 17 process.monitor_children do |child_process| 18 child_process.stop_command = "kill -QUIT {{PID}}" 19 20 child_process.checks :mem_usage, :every => 10.seconds, :below => 150.megabytes, :times => [3,4], :fires => :stop 21 child_process.checks :cpu_usage, :every => 10.seconds, :below => 20, :times => [3,4], :fires => :stop 22 end 23 end 24 end
One new feature we recently implemented was the support for working_dir, similar to god's config. There is one important difference though: we also set the PWD environmental variable. This is used to reload the unicorn app with a USR2 signal. In order for Unicorn to properly reload the app with the new current symlink, the env variable needs to be set.
Typically, when you chdir into a symlink'd folder, you are stuck in whatever the symlimk referenced, but if we pass the symlink as an environmental variable, unicorn chdir's into the symlink. The alternative would be to put the full path to the config file in the start command.
The montioring of child processes is still fairly new, as is all of bluepill, so please report any bugs you encounter to the github repos' issue page.

