Class: Guard::Foreman

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/foreman.rb

Constant Summary collapse

DEFAULT_LOG_LOCATION =

Default log location (Rails in mind)

"log/foreman.log"
TITLE =
"Foreman status"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Foreman

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>)

    the Guard file watchers

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

    the custom Guard options



15
16
17
18
19
20
21
22
23
24
# File 'lib/guard/foreman.rb', line 15

def initialize(options = {})
  @log_file       = options.fetch(:log_file, DEFAULT_LOG_LOCATION)
  @concurrency    = options[:concurrency]
  @env            = options[:env]
  @procfile       = options[:procfile]
  @port           = options[:port]
  @root           = options[:root]

  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



70
71
72
73
74
# File 'lib/guard/foreman.rb', line 70

def reload
  info "Restarting Foreman..."
  stop
  start
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



79
80
81
# File 'lib/guard/foreman.rb', line 79

def run_all
  start
end

#run_on_additions(paths) ⇒ Object



90
91
92
# File 'lib/guard/foreman.rb', line 90

def run_on_additions(paths)
  reload
end

#run_on_changes(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



86
87
88
# File 'lib/guard/foreman.rb', line 86

def run_on_changes(paths)
  reload
end

#run_on_modifications(paths) ⇒ Object



94
95
96
# File 'lib/guard/foreman.rb', line 94

def run_on_modifications(paths)
  reload
end

#run_on_removals(paths) ⇒ Object



98
99
100
# File 'lib/guard/foreman.rb', line 98

def run_on_removals(paths)
  reload
end

#startObject

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

Raises:

  • (:task_has_failed)

    when start has failed



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/guard/foreman.rb', line 28

def start
  # Stop if running
  stop if @pid

  cmd = "foreman start"
  cmd += " -c #{@concurrency}" if @concurrency
  cmd += " -e #{@env}"         if @env
  cmd += " -f #{@procfile}"    if @procfile
  cmd += " -p #{@port}"        if @port
  cmd += " -d #{@root}"        if @root
  #cmd += " > #{@log_file}"     # Disabled for now

  debug "About to run #{cmd}"
  @pid = ::Spoon.spawnp(*cmd.split(" "))     # Spoon is a little weird

 info "Foreman started."
 debug "Foreman has pid #{@pid}"
 success "Foreman 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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/guard/foreman.rb', line 50

def stop
  if @pid
    begin
      debug "Asking Foreman to terminate... (#{@pid})"
      ::Process.kill("TERM", @pid)
      debug "Waiting for Foreman to stop..."
      ::Process.waitpid(@pid)
      @pid = nil          # Unset @pid
    rescue Errno::ESRCH
      # Don't do anything, the process does not exist
      debug "Could not find Foreman process"
    end
    info "Foreman stopped."
  end
end