Class: Guard::Rails::Runner

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) ⇒ Runner

Returns a new instance of Runner.



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

def initialize(options)
  @options = options
  @root = options[:root] ? File.expand_path(options[:root]) : Dir.pwd
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#build_commandObject



45
46
47
48
49
50
# File 'lib/guard/rails/runner.rb', line 45

def build_command
  command = build_cli_command if options[:CLI]
  command ||= build_zeus_command if options[:zeus]
  command ||= build_rails_command
  "sh -c 'cd \"#{@root}\" && #{command} &'"
end

#environmentObject



52
53
54
55
56
57
58
59
60
# File 'lib/guard/rails/runner.rb', line 52

def environment
  rails_env = if options[:zeus]
                nil
              else
                options[:environment]
              end

  { "RAILS_ENV" => rails_env }
end

#pidObject



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

def pid
  has_pid? ? read_pid : nil
end

#pid_fileObject



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

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

#restartObject



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

def restart
  stop
  start
end

#sleep_timeObject



70
71
72
# File 'lib/guard/rails/runner.rb', line 70

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

#startObject



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

def start
  kill_unmanaged_pid! if options[:force_run]

  if options[:zeus] && !wait_for_zeus
    UI.info "[Guard::Rails::Error] Could not find zeus socket file."
    return false
  end

  run_rails_command!
  wait_for_pid
end

#stopObject



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

def stop
  return unless has_pid?

  if (pid = read_pid)
    sig_sent = kill_process("INT", pid)
    wait_for_no_pid if sig_sent

    # If you lost your pid_file, you are already died.
    kill_process("KILL", pid)
  end
  remove_pid_file_and_wait_for_no_pid
end

#wait_for_zeusObject



74
75
76
# File 'lib/guard/rails/runner.rb', line 74

def wait_for_zeus
  wait_for_loop { File.exist?(zeus_sockfile) }
end