class MainLoop::ThreadHandler

Attributes

thread[R]

Public Class Methods

new(dispatcher, name, **kwargs, &block) click to toggle source
Calls superclass method MainLoop::Handler::new
# File lib/main_loop/thread_handler.rb, line 8
def initialize(dispatcher, name, **kwargs, &block)
  super
  @handler_type = 'Thread'
  @thread = nil
  dispatcher.add_handler(self)

  run(&block) if block_given?
end

Public Instance Methods

id() click to toggle source
# File lib/main_loop/thread_handler.rb, line 17
def id
  @thread&.object_id.to_s
end
kill() click to toggle source
# File lib/main_loop/thread_handler.rb, line 51
def kill
  unless @thread
    logger.debug "Thread[#{name}] alredy Killed. Skipped."
    return
  end

  @success = false
  logger.info "Thread[#{name}] send kill: thread:#{@thread}"
  @thread.kill rescue nil
end
reap(status) click to toggle source
# File lib/main_loop/thread_handler.rb, line 21
def reap(status)
  logger.info "Thread[#{name}] exited: thread:#{@thread} Status:#{status}"
  @thread = nil
  @finished = true

  return if terminating?
  @success = false

  handle_retry
end
run(&block) click to toggle source
# File lib/main_loop/thread_handler.rb, line 62
def run(&block)
  return if terminating?

  @block = block
  start_thread(&@block)
end
term() click to toggle source
# File lib/main_loop/thread_handler.rb, line 32
def term
  unless @thread
    @terminating_at ||= Time.now
    logger.debug "Thread[#{name}] alredy 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}"
    @on_term&.call(@thread) rescue nil
  end
end

Protected Instance Methods

start_thread() { |self| ... } click to toggle source
# File lib/main_loop/thread_handler.rb, line 71
def start_thread
  @thread = Thread.new do
    yield(self)
  ensure
    publish("reap:#{id}:exited")
  end
  @finished = false
  logger.info "Thread[#{name}] created: thread:#{@thread}"
end