Skip to content

Commit

Permalink
Use childprocess and sys-proctables gems for more robust starting/sto…
Browse files Browse the repository at this point in the history
…pping
  • Loading branch information
Sam Day committed Jan 7, 2014
1 parent 30a199b commit 05e1f54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions guard-go.gemspec
Expand Up @@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
gem.version = Guard::GoVersion::VERSION

gem.add_dependency 'guard', '>= 1.0.0'
gem.add_dependency 'sys-proctable', '>= 0.9'
gem.add_dependency 'childprocess', '>= 0.3'
end
32 changes: 18 additions & 14 deletions lib/guard/go/runner.rb
@@ -1,3 +1,6 @@
require 'sys/proctable'
require 'childprocess'

module Guard
class GoRunner
MAX_WAIT_COUNT = 10
Expand All @@ -21,33 +24,34 @@ def stop
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 build_go_command
if @options[:test]
%{cd #{Dir.pwd} && go test}
else
%{cd #{Dir.pwd} && go run #{@options[:server]} #{@options[:args_to_s]} &}
end
end

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

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

private
def run_go_command!
system build_go_command
@pid = $?.pid
if @options[:test]
@proc = ChildProcess.build("go", "test")
else
@proc = ChildProcess.build("go", "run", @options[:server], @options[:args_to_s])
end

@proc.io.inherit!
@proc.cwd = Dir.pwd
@proc.start
@pid = @proc.pid
end
end
end

0 comments on commit 05e1f54

Please sign in to comment.