Class: MainLoop::ThreadHandler
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.
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
#thread ⇒ Object
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
#id ⇒ Object
17
18
19
|
# File 'lib/main_loop/thread_handler.rb', line 17
def id
@thread&.object_id.to_s
end
|
#kill ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/main_loop/thread_handler.rb', line 49
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
@success = false
return if terminating?
handle_retry
end
|
#run(&block) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/main_loop/thread_handler.rb', line 60
def run(&block)
return if terminating?
@block = block
start_thread(&@block)
end
|
#term ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# 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
logger.info "Thread[#{name}] send terminate: thread:#{@thread}"
end
end
|