Class: Guard::RailsRunner

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

Constant Summary collapse

MAX_WAIT_COUNT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RailsRunner

Returns a new instance of RailsRunner.



9
10
11
# File 'lib/guard/rails/runner.rb', line 9

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/guard/rails/runner.rb', line 7

def options
  @options
end

Instance Method Details

#build_rails_commandObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/guard/rails/runner.rb', line 36

def build_rails_command
  return %{#{options[:CLI]} --pid #{pid_file}} if options[:CLI]

  rails_options = [
    options[:daemon] ? '-d' : nil,
    options[:debugger] ? '-u' : nil,
    '-e', options[:environment],
    '--pid', pid_file,
    '-p', options[:port],
    options[:server],
  ]

  zeus_options = [
    options[:zeus_plan] || 'server',
  ]

  # omit env when use zeus
  rails_runner = options[:zeus] ? "zeus #{zeus_options.join(' ')}" : "RAILS_ENV=#{options[:environment]} rails server"

  %{#{rails_runner} #{rails_options.join(' ')}}
end

#pidObject



62
63
64
# File 'lib/guard/rails/runner.rb', line 62

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

#pid_fileObject



58
59
60
# File 'lib/guard/rails/runner.rb', line 58

def pid_file
  File.expand_path(options[:pid_file] || "tmp/pids/#{options[:environment]}.pid")
end

#restartObject



31
32
33
34
# File 'lib/guard/rails/runner.rb', line 31

def restart
  stop
  start
end

#sleep_timeObject



66
67
68
# File 'lib/guard/rails/runner.rb', line 66

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

#startObject



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

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

#stopObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/guard/rails/runner.rb', line 19

def stop
  if File.file?(pid_file)
    pid = File.read(pid_file).strip
    system %{kill -SIGINT #{pid}}
    wait_for_no_pid if $?.exitstatus == 0

    # If you lost your pid_file, you are already died.
    system %{kill -KILL #{pid} >&2 2>/dev/null}
    FileUtils.rm pid_file, :force => true
  end
end