Class: Guard::Unicorn

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/unicorn.rb

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

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Unicorn

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the Guard file watchers

  • options (Hash) (defaults to: {})

    the custom Guard options



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 = [], options = {})
  if watchers.empty?
    watchers << Watcher.new( /^app\/(controllers|models|helpers)\/.+\.rb$/ )
    watchers << Watcher.new( /^lib\/.+\.rb$/ )
  end

  @run_as_daemon  = options.fetch(:daemonize, false)
  @enable_bundler = options.fetch(:bundler, true) 
  @pid_file       = options.fetch(:pid_file, DEFAULT_PID_PATH)
  @config_file    = options.fetch(:config_file, DEFAULT_CONFIG_PATH)
  @preloading     = options.fetch(:preloading, false)
  @port           = options.fetch(:port, DEFAULT_PORT)
  @environment    = options.fetch(:environment, DEFAULT_ENVIRONMENT)
  @socket         = options.fetch(:socket, nil)

  super
end

Instance Method Details

#reloadObject

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…

Raises:

  • (:task_has_failed)

    when reload has failed



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_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Raises:

  • (:task_has_failed)

    when run_all has failed



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.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



108
109
110
# File 'lib/guard/unicorn.rb', line 108

def run_on_modifications(paths)
  reload
end

#startObject

Call once when Guard starts. Please override initialize method to init stuff.

Raises:

  • (:task_has_failed)

    when start has failed



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

#stopObject

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).

Raises:

  • (:task_has_failed)

    when stop has failed



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