Skip to content
Permalink
Browse files

DEV: support USR2 to restart unicorn in dev mode

In dev mode sending USR2 to the unicorn master supervisor process will
restart unicorn.

This allows a simple script like this to restart unicorn in dev:

```
#!/usr/bin/env bash

kill -s USR2 `ps aux | grep ruby\ bin\/unicorn | grep -v grep | awk '{print $2}'`
```
  • Loading branch information...
SamSaffron committed Sep 11, 2019
1 parent 7252c14 commit fb8d1f35a40c83d7a290114bfa7efd7926b92090
Showing with 38 additions and 1 deletion.
  1. +38 −1 bin/unicorn
@@ -8,11 +8,15 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
require 'rubygems'
require 'bundler/setup'

dev_mode = false

# in development do some fussing around, to automate config
if !ARGV.include?("-E") &&
!ARGV.include?("--env") &&
(ENV["RAILS_ENV"] == "development" || !ENV["RAILS_ENV"])

dev_mode = true

ARGV.push("-N")
if !ARGV.include?("-c") && !ARGV.include?("--config-file")
ARGV.push("-c")
@@ -46,4 +50,37 @@ if ARGV.include?("--help")
exit
end

load Gem.bin_path('unicorn', 'unicorn')
# this dev_mode hackery enables, the following script to be used to restart unicorn:
#
# #!/usr/bin/env bash
# kill -s USR2 `ps aux | grep ruby\ bin\/unicorn | grep -v grep | awk '{print $2}'`
#
# This is handy if you want to bind a key to restarting unicorn in dev

if dev_mode
restart = true
while restart
restart = false
pid = fork do
load Gem.bin_path('unicorn', 'unicorn')
end
done = false

Signal.trap('INT') do
# wait for parent to be done
end

Signal.trap('USR2') do
Process.kill('QUIT', pid)
puts "RESTARTING UNICORN"
restart = true
end

while !done
sleep 1
done = Process.waitpid(pid, Process::WNOHANG)
end
end
else
load Gem.bin_path('unicorn', 'unicorn')
end

0 comments on commit fb8d1f3

Please sign in to comment.
You can’t perform that action at this time.