Permalink
Browse files

Guard Go processes management fix

  • Loading branch information...
1 parent e89815a commit 2c9b32a450dde4f670a6934464e7257082e38500 @victorcoder committed Apr 5, 2012
Showing with 31 additions and 55 deletions.
  1. +2 −2 guard-go.gemspec
  2. +14 −16 lib/guard/go.rb
  3. +15 −37 lib/guard/go/runner.rb
View
4 guard-go.gemspec
@@ -4,8 +4,8 @@ require File.expand_path('../lib/guard/go/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Victor Castell"]
gem.email = ["victorcoder@gmail.com"]
- gem.description = %q{TODO: Write a gem description}
- gem.summary = %q{TODO: Write a gem summary}
+ gem.description = %q{Guard gem for Go}
+ gem.summary = %q{Guard gem for launching go files}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
View
30 lib/guard/go.rb
@@ -6,40 +6,38 @@
module Guard
class Go < ::Guard::Guard
attr_reader :options
-
- DEFAULT_OPTIONS = {
- :go_file => 'app.go'
- }
def initialize(watchers = [], options = {})
super
- @options = DEFAULT_OPTIONS.merge(options)
- @runner = ::Guard::GoRunner.new(@options)
+ go_file = watchers.first.pattern || 'app.go'
+
+ unless File.exists? go_file
+ raise "Go file #{go_file} not found"
+ end
+ @runner = ::Guard::GoRunner.new(go_file, options)
end
- # Call once when Guard starts. Please override initialize method to init stuff.
- # @raise [:task_has_failed] when start has failed
def start
- run_all if options[:all_on_start]
- end
-
- def run_all
- run_on_change(Watcher.match_files(self, Dir.glob('{,**/}*{,.*}').uniq))
+ UI.info "Starting Go..."
+ if pid = @runner.start
+ UI.info "Started Go app, pid #{pid}"
+ end
end
- def run_on_change(paths)
+ def run_on_change
UI.info "Restarting Go..."
if @runner.restart
- UI.info "Go restarted, pid #{runner.pid}"
+ UI.info "Go restarted, pid #{@runner.pid}"
else
UI.info "Go NOT restarted, check your log files."
end
end
def stop
- Notifier.notify("Until next time...", :title => "Go shutting down.", :image => :pending)
@runner.stop
+ UI.info "Stopping Go..."
+ Notifier.notify("Until next time...", :title => "Go shutting down.", :image => :pending)
end
end
end
View
52 lib/guard/go/runner.rb
@@ -4,22 +4,23 @@ module Guard
class GoRunner
MAX_WAIT_COUNT = 10
- attr_reader :options
+ attr_reader :options, :pid
- def initialize(options)
+ def initialize(file, options)
+ @file = file
@options = options
end
def start
- run_rails_command!
- wait_for_pid
+ run_go_command!
end
def stop
- if File.file?(pid_file)
- system %{kill -KILL #{File.read(pid_file).strip}}
- wait_for_no_pid if $?.exitstatus == 0
- FileUtils.rm pid_file
+ ps_go_pid.each do |pid|
+ system %{kill -KILL #{pid}}
+ end
+ while ps_go_pid.count > 0
+ wait sleep_time
end
end
@@ -29,15 +30,11 @@ def restart
end
def build_go_command
- %{sh -c 'cd #{Dir.pwd} && go run #{options[:go_file]} &'}
- end
-
- def pid_file
- File.expand_path("tmp/pids/#{options[:go_file]}.pid")
+ %{cd #{Dir.pwd} && go run #{@file}}
end
- def pid
- File.file?(pid_file) ? File.read(pid_file).to_i : nil
+ def ps_go_pid
+ `ps aux | awk '/a.out/&&!/awk/{print $2;}'`.split("\n").map { |pid| pid.to_i }
end
def sleep_time
@@ -46,29 +43,10 @@ def sleep_time
private
def run_go_command!
- system build_go_command
- system %{echo $! > #{pid_file}}
- end
-
- def has_pid?
- File.file?(pid_file)
- end
-
- def wait_for_pid_action
- sleep sleep_time
- end
-
- def wait_for_pid
- wait_for_pid_loop
- end
-
- def wait_for_pid_loop
- count = 0
- while !has_pid? && count < MAX_WAIT_COUNT
- wait_for_pid_action
- count += 1
+ @pid = fork do
+ exec build_go_command
end
- !(count == MAX_WAIT_COUNT)
+ @pid
end
end
end

0 comments on commit 2c9b32a

Please sign in to comment.