Class: ProxyMgr::ProcessManager
- Inherits:
-
Object
- Object
- ProxyMgr::ProcessManager
- Includes:
- Callbacks
- Defined in:
- lib/proxymgr/process_manager.rb,
lib/proxymgr/process_manager/signal_handler.rb
Defined Under Namespace
Classes: SignalHandler
Instance Attribute Summary collapse
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(cmd, args = [], opts = {}) ⇒ ProcessManager
constructor
A new instance of ProcessManager.
- #start ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Methods included from Callbacks
Constructor Details
#initialize(cmd, args = [], opts = {}) ⇒ ProcessManager
Returns a new instance of ProcessManager.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/proxymgr/process_manager.rb', line 10 def initialize(cmd, args = [], opts = {}) @cmd = cmd @args = args @pid = nil @exit_code = nil @timeout = opts[:timeout] || 10 @setsid = opts[:setsid] || true @fds = opts[:fds] || [] @io_handler = nil callbacks :on_stdout, :on_stderr, :on_stop end |
Instance Attribute Details
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
8 9 10 |
# File 'lib/proxymgr/process_manager.rb', line 8 def exit_code @exit_code end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
8 9 10 |
# File 'lib/proxymgr/process_manager.rb', line 8 def pid @pid end |
Class Method Details
.register(pid, &blk) ⇒ Object
96 97 98 99 |
# File 'lib/proxymgr/process_manager.rb', line 96 def self.register(pid, &blk) @handler ||= SignalHandler.new @handler.register(pid, &blk) end |
Instance Method Details
#start ⇒ Object
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 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/proxymgr/process_manager.rb', line 25 def start stdout_read, stdout_write = IO.pipe stderr_read, stderr_write = IO.pipe sync_pipe = IO.pipe @pid = Process.fork do $stdout.reopen stdout_write $stderr.reopen stderr_write [stderr_read, stdout_read].each(&:close) begin Process.setsid if @setsid rescue Errno::EPERM end sync_pipe[0].read(1) 3.upto(Platform.max_fd).each do |fd| begin IO.for_fd(fd).close unless @fds.include? fd rescue ArgumentError, Errno::EBADF end end Process.exec(*([@cmd] + @args), :close_others => false) end self.class.register(@pid) { |status| call(:on_stop, status) } sync_pipe[1].write(1) ([stdout_write, stderr_write] + sync_pipe).each(&:close) @thread = Thread.new do stop = false fdset = [stdout_read, stderr_read] until stop r = IO.select(fdset, [], fdset).first out = {} r.each do |pipe| stream = pipe == stdout_read ? :stdout : :stderr buf = out[stream] ||= '' begin loop { buf << pipe.read_nonblock(4096) } rescue Errno::EWOULDBLOCK rescue EOFError stop = true end end out.each do |stream, buf| buf.split(/\n/).each { |line| call("on_#{stream}".to_sym, line) } end end fdset.each(&:close) end @thread.abort_on_exception = true @pid end |
#stop ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/proxymgr/process_manager.rb', line 78 def stop Process.kill('TERM', @pid) begin Timeout.timeout(@timeout) { wait } rescue Timeout::Error Process.kill('KILL', @pid) end @thread.join if @thread end |
#wait ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/proxymgr/process_manager.rb', line 88 def wait begin _pid, result = Process.waitpid2(@pid) @exit_code = result.exitstatus || result.termsig rescue Errno::ECHILD end end |