Class: MainLoop::ProcessHandler
- Inherits:
-
Handler
- Object
- Handler
- MainLoop::ProcessHandler
show all
- Defined in:
- lib/main_loop/process_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) ⇒ ProcessHandler
Returns a new instance of ProcessHandler.
8
9
10
11
12
13
14
15
|
# File 'lib/main_loop/process_handler.rb', line 8
def initialize(dispatcher, name, **kwargs, &block)
super
@handler_type = 'Process'
@pid = nil
dispatcher.add_handler(self)
run(&block) if block_given?
end
|
Instance Attribute Details
#pid ⇒ Object
Returns the value of attribute pid.
6
7
8
|
# File 'lib/main_loop/process_handler.rb', line 6
def pid
@pid
end
|
Instance Method Details
#id ⇒ Object
17
18
19
|
# File 'lib/main_loop/process_handler.rb', line 17
def id
@pid
end
|
#kill ⇒ Object
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/main_loop/process_handler.rb', line 51
def kill
unless @pid
logger.debug "Process[#{name}] alredy Killed. Skipped."
return
end
@success = false
logger.info "Process[#{name}] send kill: Pid:#{@pid}"
::Process.kill('KILL', @pid) rescue nil
end
|
#reap(status) ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/main_loop/process_handler.rb', line 21
def reap(status)
logger.info "Process[#{name}] exited: Pid:#{@pid} Status: #{status.exitstatus.inspect} Termsig: #{status.termsig.inspect} Success: #{status.success?}"
@pid = nil
@finished = true
@success = !!status.success?
return if terminating?
handle_retry
end
|
#run(&block) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/main_loop/process_handler.rb', line 62
def run(&block)
return if terminating?
@block = block
start_fork(&@block)
end
|
#term ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/main_loop/process_handler.rb', line 32
def term
unless @pid
@terminating_at ||= Time.now
logger.debug "Process[#{name}] alredy terminated. Skipped."
return
end
if terminating?
@success = false
logger.info "Process[#{name}] send force terminate: KILL Pid:#{@pid}"
::Process.kill('KILL', @pid) rescue nil
else
@terminating_at ||= Time.now
logger.info "Process[#{name}] send terminate: Pid:#{@pid}"
@on_term&.call(@pid) rescue nil
::Process.kill('TERM', @pid) rescue nil
end
end
|