Class: Guard::RackRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/rack/runner.rb

Constant Summary collapse

MAX_WAIT_COUNT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RackRunner

Returns a new instance of RackRunner.



10
11
12
# File 'lib/guard/rack/runner.rb', line 10

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/guard/rack/runner.rb', line 8

def options
  @options
end

Instance Method Details

#build_rack_commandObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/guard/rack/runner.rb', line 44

def build_rack_command
  rack_options = [
    '--env', options[:environment],
    '--port', options[:port],
    '--pid', pid_file
  ]

  rack_options << '--daemonize' if options[:daemon]
  rack_options << '--debug' if options[:debugger]

  %{sh -c 'cd #{Dir.pwd} && rackup #{rack_options.join(' ')} &'}
end

#kill(pid) ⇒ Object



14
15
16
17
# File 'lib/guard/rack/runner.rb', line 14

def kill pid
  system %{kill -INT #{pid}}
  $?.exitstatus
end

#pidObject



61
62
63
# File 'lib/guard/rack/runner.rb', line 61

def pid
  File.file?(pid_file) ? File.read(pid_file).to_i : nil
end

#pid_fileObject



57
58
59
# File 'lib/guard/rack/runner.rb', line 57

def pid_file
  File.expand_path(".guard-rack-#{options[:environment]}.pid")
end

#remove_pid_fileObject



65
66
67
# File 'lib/guard/rack/runner.rb', line 65

def remove_pid_file
  FileUtils.rm pid_file if File.exist? pid_file
end

#restartObject



40
41
42
# File 'lib/guard/rack/runner.rb', line 40

def restart
  stop and start
end

#sleep_timeObject



69
70
71
# File 'lib/guard/rack/runner.rb', line 69

def sleep_time
  options[:timeout].to_f / MAX_WAIT_COUNT.to_f
end

#startObject



19
20
21
22
23
# File 'lib/guard/rack/runner.rb', line 19

def start
  kill_unmanaged_pid! if options[:force_run]
  run_rack_command!
  wait_for_pid
end

#stopObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/guard/rack/runner.rb', line 25

def stop
  # Rely on kill_unmanaged_pid if there's no pid file
  return true unless File.file?(pid_file)
 
  if kill(pid) == 0
    wait_for_no_pid
    remove_pid_file
  else
    UI.info "Rackup exited with non-zero exit status whilst trying to stop."
    return false
  end

  true
end