-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrunner.rb
63 lines (52 loc) · 1.25 KB
/
runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'sys/proctable'
require 'childprocess'
module Guard
class GoRunner
MAX_WAIT_COUNT = 10
attr_reader :options, :pid
def initialize(options)
@options = options
unless @options[:test] && File.exists?(@options[:server])
raise "Server file not found. Check your :server option in your Guardfile."
end
end
def start
run_go_command!
end
def stop
ps_go_pid.each do |pid|
system %{kill -KILL #{pid}}
end
while ps_go_pid.count > 0
sleep sleep_time
end
@proc.stop
end
def ps_go_pid
Sys::ProcTable.ps.select{ |pe| pe.ppid == @pid }.map { |pe| pe.pid }
end
def restart
stop
start
end
def sleep_time
options[:timeout].to_f / MAX_WAIT_COUNT.to_f
end
private
def run_go_command!
if @options[:test]
@proc = ChildProcess.build(@options[:cmd], "test")
else
if @options[:build_only]
@proc = ChildProcess.build(@options[:cmd], "build")
else
@proc = ChildProcess.build(@options[:cmd], "run", @options[:server], @options[:args_to_s])
end
end
@proc.io.inherit!
@proc.cwd = Dir.pwd
@proc.start
@pid = @proc.pid
end
end
end