Class: MainLoop::ProcessHandler
- Inherits:
-
Handler
- Object
- Handler
- MainLoop::ProcessHandler
- Defined in:
- lib/main_loop/process_handler.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
-
#id ⇒ Integer|nil
Получить PID процесса.
-
#initialize(dispatcher, name, runnable: nil, **kwargs, &block) ⇒ ProcessHandler
constructor
Инициализация.
-
#kill(*_args) ⇒ Object
Принудительно завершить процесс.
-
#reap(status) ⇒ Object
Обработка завершения процесса.
-
#run ⇒ Object
Запустить процесс.
-
#term(*_args) ⇒ Object
Отправить сигнал терминации.
Constructor Details
#initialize(dispatcher, name, runnable: nil, **kwargs, &block) ⇒ ProcessHandler
Инициализация
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/main_loop/process_handler.rb', line 45 def initialize(dispatcher, name, runnable: nil, **kwargs, &block) super @handler_type = 'Process' @pid = nil dispatcher.add_handler(self) if runnable unless runnable.respond_to?(:run) && runnable.respond_to?(:on_term) raise TypeError, "Runnable object must respond to :run and :on_term" end end @runnable = runnable @block = block run end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
34 35 36 |
# File 'lib/main_loop/process_handler.rb', line 34 def pid @pid end |
Instance Method Details
#id ⇒ Integer|nil
Получить PID процесса
66 67 68 |
# File 'lib/main_loop/process_handler.rb', line 66 def id @pid end |
#kill(*_args) ⇒ Object
Принудительно завершить процесс
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/main_loop/process_handler.rb', line 117 def kill(*_args) unless @pid logger.debug "Process[#{name}] already Killed. Skipped." return end @success = false logger.info "Process[#{name}] send kill: Pid:#{@pid}" ::Process.kill('KILL', @pid) rescue nil end |
#reap(status) ⇒ Object
Обработка завершения процесса
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/main_loop/process_handler.rb', line 73 def reap(status) if status logger.info "Process[#{name}] exited: Pid:#{@pid} Status: #{status.exitstatus.inspect} Termsig: #{status.termsig.inspect} Success: #{status.success?}" @success = !!status.success? else logger.info "Process[#{name}] exited: Pid:#{@pid} with unknown status" @success = true # TODO или false? end @pid = nil @finished = true return if terminating? handle_retry end |
#run ⇒ Object
Запустить процесс
Вызывает #start_fork с блоком или runnable.
131 132 133 134 135 136 137 138 139 |
# File 'lib/main_loop/process_handler.rb', line 131 def run return if terminating? if @runnable start_fork { @runnable.run } elsif @block start_fork(&@block) end end |
#term(*_args) ⇒ Object
Отправить сигнал терминации
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/main_loop/process_handler.rb', line 92 def term(*_args) unless @pid @terminating_at ||= Time.now logger.debug "Process[#{name}] already terminated. Skipped." return end if terminating? @success = false logger.info "Process[#{name}] send force terminate: KILL Pid:#{@pid}" ::Process.kill('KILL', @pid) rescue nil else @terminating_at ||= Time.now logger.info "Process[#{name}] send terminate: Pid:#{@pid}" @runnable&.on_term(@pid) rescue nil @on_term&.call(@pid) rescue nil ::Process.kill('TERM', @pid) rescue nil end end |