Class: Guard::GoRunner

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

Constant Summary collapse

MAX_WAIT_COUNT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GoRunner

Returns a new instance of GoRunner.



7
8
9
10
11
# File 'lib/guard/go/runner.rb', line 7

def initialize(options)
  @options = options

  raise "Server file not found. Check your :server option in your Guarfile." unless File.exists? @options[:server]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/guard/go/runner.rb', line 5

def options
  @options
end

#pidObject (readonly)

Returns the value of attribute pid.



5
6
7
# File 'lib/guard/go/runner.rb', line 5

def pid
  @pid
end

Instance Method Details

#build_go_commandObject



31
32
33
34
35
36
37
# File 'lib/guard/go/runner.rb', line 31

def build_go_command
  if @options[:test]
    %{cd #{Dir.pwd} && go test}
  else
    %{cd #{Dir.pwd} && go run #{@options[:server]} &}
  end
end

#ps_go_pidObject



39
40
41
# File 'lib/guard/go/runner.rb', line 39

def ps_go_pid
  `ps aux | awk '/a.out/&&!/awk/{print $2;}'`.split("\n").map { |pid| pid.to_i }
end

#restartObject



26
27
28
29
# File 'lib/guard/go/runner.rb', line 26

def restart
  stop
  start
end

#sleep_timeObject



43
44
45
# File 'lib/guard/go/runner.rb', line 43

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

#startObject



13
14
15
# File 'lib/guard/go/runner.rb', line 13

def start
  run_go_command!
end

#stopObject



17
18
19
20
21
22
23
24
# File 'lib/guard/go/runner.rb', line 17

def stop
  ps_go_pid.each do |pid|
    system %{kill -KILL #{pid}}
  end
  while ps_go_pid.count > 0
    sleep sleep_time
  end
end