Sha256: 9fa506a1b75b84c5247cb1c287d0cab68c272ced962cd40b239a3a83a720305b

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require 'open3'
require 'ramaze/tool/bin'

# class Servant provides process invocations.
#
# @example
#   pid = Servant.watch("some-app -p 12345"){
#     # this block is called when app finishes
#   }
#
#   Servant.kill(pid) #=> stop the app immediately
#
#   Servant.communicate("gem install foobar"){|line|
#     # each line is yielded form process
#   }
class Servant
  extend Ramaze::Tool::Bin::Helpers # provides is_running?

  def self.watch(cmd, args=[], &block)
    begin
      pid = Process.spawn(cmd, *args)
    rescue SystemCallError
      return nil
    end

    Thread.new{
      Process.waitpid(pid)
      block.call
    }
    return pid
  end

  def self.communicate(cmd, args=[])
    command = cmd + args.map{|x| ' '+x}.join
    Open3.popen3(command){|stdin, stdout, stderr|
      stdin.close
      while line = stdout.gets
        yield line
      end
      while line = stderr.gets
        yield line
      end
    }
  end

  def self.kill(pid)
    Process.kill("INT", pid)

    if is_running?(pid)
      sleep 2
      Ramaze::Log.warn "Process #{pid} did not die, forcing it with -9"
      Process.kill(9, pid)
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-station-0.1.1 util/servant.rb
ruby-station-0.1.0 util/servant.rb