Class: MainLoop::ThreadHandler
- Inherits:
-
Handler
- Object
- Handler
- MainLoop::ThreadHandler
- Defined in:
- lib/main_loop/thread_handler.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
-
#id ⇒ String
Получить ID потока.
-
#initialize(dispatcher, name, runnable: nil, **kwargs, &block) ⇒ ThreadHandler
constructor
Инициализация.
-
#kill(*_args) ⇒ Object
Принудительно завершить поток.
-
#reap(status) ⇒ Object
Обработка завершения потока.
-
#run ⇒ Object
Запустить поток.
-
#term(*_args) ⇒ Object
Терминация потока.
Constructor Details
#initialize(dispatcher, name, runnable: nil, **kwargs, &block) ⇒ ThreadHandler
Инициализация
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/main_loop/thread_handler.rb', line 47 def initialize(dispatcher, name, runnable: nil, **kwargs, &block) super @handler_type = 'Thread' @thread = 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
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
38 39 40 |
# File 'lib/main_loop/thread_handler.rb', line 38 def thread @thread end |
Instance Method Details
#id ⇒ String
Получить ID потока
68 69 70 |
# File 'lib/main_loop/thread_handler.rb', line 68 def id @thread&.object_id.to_s end |
#kill(*_args) ⇒ Object
Принудительно завершить поток
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/main_loop/thread_handler.rb', line 114 def kill(*_args) unless @thread logger.debug "Thread[#{name}] already Killed. Skipped." return end @success = false logger.info "Thread[#{name}] send kill: thread:#{@thread}" @thread.kill rescue nil end |
#reap(status) ⇒ Object
Обработка завершения потока
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/main_loop/thread_handler.rb', line 75 def reap(status) logger.info "Thread[#{name}] exited: thread:#{@thread} Status:#{status}" exit_reason = @thread[:exit_reason] if @thread @thread = nil @finished = true return if terminating? @success = (exit_reason == :normal) handle_retry end |
#run ⇒ Object
Запустить поток
Вызывает #start_thread с блоком или runnable.
128 129 130 131 132 133 134 135 136 |
# File 'lib/main_loop/thread_handler.rb', line 128 def run return if terminating? if @runnable start_thread { @runnable.run(self) } elsif @block start_thread(&@block) end end |
#term(*_args) ⇒ Object
Терминация потока
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/main_loop/thread_handler.rb', line 90 def term(*_args) unless @thread @terminating_at ||= Time.now logger.debug "Thread[#{name}] already terminated. Skipped." return end if terminating? @success = false logger.info "Thread[#{name}] send force terminate: KILL thread:#{@thread}" @thread.kill rescue nil else @terminating_at ||= Time.now @success = true logger.info "Thread[#{name}] send terminate: thread:#{@thread}" @runnable&.on_term(@thread) rescue nil @on_term&.call(@thread) rescue nil end end |