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, #publish, #running?, #success?, #terminating?

Constructor Details

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

Returns a new instance of ThreadHandler.



9
10
11
12
13
14
15
16
# File 'lib/main_loop/thread_handler.rb', line 9

def initialize(dispatcher, name, **kwargs, &block)
  super
  @handler_type = 'Thread'
  @thread = nil
  dispatcher.add_handler(self)

  run(&block) if block_given?
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



7
8
9
# File 'lib/main_loop/thread_handler.rb', line 7

def thread
  @thread
end

Instance Method Details

#idObject



18
19
20
# File 'lib/main_loop/thread_handler.rb', line 18

def id
  @thread&.object_id.to_s
end

#killObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/main_loop/thread_handler.rb', line 50

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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/main_loop/thread_handler.rb', line 22

def reap(status)
  logger.info "Thread[#{name}] exited: thread:#{@thread} Status:#{status}"
  @thread = nil
  @finished = true
  @success = false

  return if terminating?

  handle_retry
end

#run(&block) ⇒ Object



61
62
63
64
65
66
# File 'lib/main_loop/thread_handler.rb', line 61

def run(&block)
  return if terminating?

  @block = block
  start_thread(&@block)
end

#termObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/main_loop/thread_handler.rb', line 33

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
    logger.info "Thread[#{name}] send terminate: thread:#{@thread}"
  end
end