Class: Guard::Unicorn
Constant Summary collapse
- DEFAULT_PID_PATH =
Sensible defaults for Rails projects
File.join("tmp", "pids", "unicorn.pid")
- DEFAULT_CONFIG_PATH =
File.join("config", "unicorn.rb")
- DEFAULT_PORT =
3000
- DEFAULT_ENVIRONMENT =
"development"
Instance Method Summary collapse
-
#initialize(watchers = [], options = {}) ⇒ Unicorn
constructor
Initialize a Guard.
-
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed.
-
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
-
#run_on_modifications(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
-
#start ⇒ Object
Call once when Guard starts.
-
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
Constructor Details
#initialize(watchers = [], options = {}) ⇒ Unicorn
Initialize a Guard.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/guard/unicorn.rb', line 16 def initialize(watchers = [], = {}) if watchers.empty? watchers << Watcher.new( /^app\/(controllers|models|helpers)\/.+\.rb$/ ) watchers << Watcher.new( /^lib\/.+\.rb$/ ) end @run_as_daemon = .fetch(:daemonize, false) @enable_bundler = .fetch(:bundler, true) @pid_file = .fetch(:pid_file, DEFAULT_PID_PATH) @config_file = .fetch(:config_file, DEFAULT_CONFIG_PATH) @preloading = .fetch(:preloading, false) @port = .fetch(:port, DEFAULT_PORT) @environment = .fetch(:environment, DEFAULT_ENVIRONMENT) @socket = .fetch(:socket, nil) super end |
Instance Method Details
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/guard/unicorn.rb', line 77 def reload # If the `preload_app` directive is false, then the workers will pick up # any code changes using a `HUP` signal, but if the application is # preloaded, then a `USR2 + QUIT` signal must be used. # # For now, let's rely on the fact that the user does know how to write # a good unicorn configuration and he will have a block of code that # will properly handle `USR2` signals. if @preloading oldpid = pid UI.debug "Sending USR2 to unicorn with pid #{oldpid}" ::Process.kill 'USR2', oldpid UI.debug "Sending QUIT to unicorn with pid #{oldpid}" ::Process.kill 'QUIT', oldpid else ::Process.kill 'HUP', pid end UI.info "Done reloading unicorn." success "Unicorn reloaded" end |
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
102 103 |
# File 'lib/guard/unicorn.rb', line 102 def run_all end |
#run_on_modifications(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
108 109 110 |
# File 'lib/guard/unicorn.rb', line 108 def run_on_modifications(paths) reload end |
#start ⇒ Object
Call once when Guard starts. Please override initialize method to init stuff.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/guard/unicorn.rb', line 36 def start # Make sure unicorn is stopped stop cmd = [] cmd << "bundle exec" if @enable_bundler cmd << "unicorn_rails" cmd << "-c #{@config_file}" cmd << "-p #{@port}" if @port cmd << "-l #{@socket}" if @socket cmd << "-E #{@environment}" cmd << "-D" if @run_as_daemon @pid = ::Process.fork do system "#{cmd.join " "}" end success "Unicorn started." end |
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/guard/unicorn.rb', line 58 def stop return unless pid begin ::Process.kill("QUIT", pid) if ::Process.getpgid(pid) # Unicorn won't always shut down right away, so we're waiting for # the getpgid method to raise an Errno::ESRCH that will tell us # the process is not longer active. sleep 1 while ::Process.getpgid(pid) success "Unicorn stopped." rescue Errno::ESRCH # Don't do anything, the process does not exist end end |