Class: MainLoop::ThreadHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/main_loop/thread_handler.rb

Instance Attribute Summary collapse

Attributes inherited from Handler

#dispatcher, #logger, #name

Instance Method Summary collapse

Methods inherited from Handler

#finished?, #handle_retry, #on_term, #publish, #running?, #success?, #terminating?

Constructor Details

#initialize(dispatcher, name, runnable: nil, **kwargs, &block) ⇒ ThreadHandler

Инициализация

Parameters:

  • dispatcher (Dispatcher)

    ссылка на диспетчер

  • name (String)

    имя обработчика

  • runnable (Object) (defaults to: nil)

    объект с методами run и on_term (опционально)

  • retry_count (Integer, :unlimited)

    количество повторов

  • logger (Logger)

    логгер



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

#threadObject (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

#idString

Получить ID потока

Returns:

  • (String)

    object_id потока в виде строки



68
69
70
# File 'lib/main_loop/thread_handler.rb', line 68

def id
  @thread&.object_id.to_s
end

#kill(*_args) ⇒ Object

Принудительно завершить поток

Parameters:

  • *_args (Unused)


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

Обработка завершения потока

Parameters:

  • status (String)

    статус завершения (описание)



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

#runObject

Запустить поток

Вызывает #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

Терминация потока

Parameters:

  • *_args (Unused)


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