Class: MainLoop::Handler
- Inherits:
-
Object
- Object
- MainLoop::Handler
- Defined in:
- lib/main_loop/handler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dispatcher ⇒ Object
readonly
Returns the value of attribute dispatcher.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#finished? ⇒ Boolean
Проверка завершения.
-
#handle_retry ⇒ Object
Логика повторов.
-
#id(*_args) ⇒ String
Идентификатор (абстрактный).
-
#initialize(dispatcher, name, *_args, retry_count: 0, logger: nil, **_kwargs) ⇒ Handler
constructor
Инициализация.
-
#kill(*_args) ⇒ Object
Принудительное завершение (абстрактный).
-
#on_term(&block) ⇒ Object
Установить обратный вызов терминации.
-
#publish(event) ⇒ Object
Публикация события.
-
#reap(*_args) ⇒ Object
Обработка завершения (абстрактный).
-
#run(*_args) ⇒ Object
Запуск (абстрактный).
-
#running? ⇒ Boolean
Проверка запущенности.
-
#success? ⇒ Boolean
Проверка успешного завершения.
-
#term(*_args) ⇒ Object
Терминация (абстрактный).
-
#terminating? ⇒ Time|nil
Проверка терминации.
Constructor Details
#initialize(dispatcher, name, *_args, retry_count: 0, logger: nil, **_kwargs) ⇒ Handler
Инициализация
35 36 37 38 39 40 41 42 |
# File 'lib/main_loop/handler.rb', line 35 def initialize(dispatcher, name, *_args, retry_count: 0, logger: nil, **_kwargs) @dispatcher = dispatcher @name = name @code = 0 @retry_count = retry_count @logger = logger || Logger.new(nil) @handler_type = 'Unknown' end |
Instance Attribute Details
#dispatcher ⇒ Object (readonly)
Returns the value of attribute dispatcher.
27 28 29 |
# File 'lib/main_loop/handler.rb', line 27 def dispatcher @dispatcher end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
27 28 29 |
# File 'lib/main_loop/handler.rb', line 27 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/main_loop/handler.rb', line 27 def name @name end |
Instance Method Details
#finished? ⇒ Boolean
Проверка завершения
:nocov:
114 115 116 |
# File 'lib/main_loop/handler.rb', line 114 def finished? @finished end |
#handle_retry ⇒ Object
Логика повторов
Управляет повторами после завершения:
-
:unlimited — бесконечные повторы
-
Integer >= 0 — декремент и повтор
-
иначе — отправляет term через bus
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/main_loop/handler.rb', line 154 def handle_retry if @retry_count == :unlimited logger.info "#{@handler_type}[#{name}] retry...." self.run elsif @retry_count && (@retry_count -= 1) >= 0 logger.info "#{@handler_type}[#{name}] retry...." self.run else publish(:term) end end |
#id(*_args) ⇒ String
Идентификатор (абстрактный)
:nocov:
49 50 51 |
# File 'lib/main_loop/handler.rb', line 49 def id(*_args) raise 'not implemented!' end |
#kill(*_args) ⇒ Object
Принудительное завершение (абстрактный)
Принудительно завершает обработчик. :nocov:
79 80 81 |
# File 'lib/main_loop/handler.rb', line 79 def kill(*_args) raise 'not implemented!' end |
#on_term(&block) ⇒ Object
Установить обратный вызов терминации
106 107 108 |
# File 'lib/main_loop/handler.rb', line 106 def on_term &block @on_term = block end |
#publish(event) ⇒ Object
Публикация события
Отправляет событие в канал событий диспетчера.
99 100 101 |
# File 'lib/main_loop/handler.rb', line 99 def publish(event) dispatcher.bus.puts(event) end |
#reap(*_args) ⇒ Object
Обработка завершения (абстрактный)
:nocov:
89 90 91 |
# File 'lib/main_loop/handler.rb', line 89 def reap(*_args) raise 'not implemented!' end |
#run(*_args) ⇒ Object
Запуск (абстрактный)
Запускает обработчик. :nocov:
69 70 71 |
# File 'lib/main_loop/handler.rb', line 69 def run(*_args) raise 'not implemented!' end |
#running? ⇒ Boolean
Проверка запущенности
:nocov:
132 133 134 |
# File 'lib/main_loop/handler.rb', line 132 def running? !finished? end |
#success? ⇒ Boolean
Проверка успешного завершения
:nocov:
123 124 125 |
# File 'lib/main_loop/handler.rb', line 123 def success? finished? && @success end |
#term(*_args) ⇒ Object
Терминация (абстрактный)
Отправляет сигнал терминации обработчику. :nocov:
59 60 61 |
# File 'lib/main_loop/handler.rb', line 59 def term(*_args) raise 'not implemented!' end |
#terminating? ⇒ Time|nil
Проверка терминации
:nocov:
141 142 143 |
# File 'lib/main_loop/handler.rb', line 141 def terminating? @terminating_at end |