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, **kwargs, &block) ⇒ ThreadHandler

Returns a new instance of ThreadHandler.



8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#idObject



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

def id
  @thread&.object_id.to_s
end

#killObject



51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



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

def run(&block)
  return if terminating?

  @block = block
  start_thread(&@block)
end

#termObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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