Class: Snipr::ProcessSignaller
- Inherits:
-
Object
- Object
- Snipr::ProcessSignaller
- Extended by:
- Forwardable
- Defined in:
- lib/snipr/process_signaller.rb
Overview
Class that can send signals to targetted processes or their parent processes and invoke callbacks around the actions. Delegates process location to a ProcessLocator. Is configured using a block on initialization as follows:
signaller = ProcessSignaller.new do |signaller|
signaller.include /resque/
signaller.exclude /scheduler/
signaller.signal "USR1"
signaller.target_parent false
singaller.dry_run
signaller.on_no_processes do
puts "No processes"
end
signaller.before_signal do |signal, process|
puts "Sending #{signal} to #{process.pid}"
end
signaller.after_signal do |signal, process|
puts "Sent #{signal} to #{process.pid}"
end
signaller.on_error do |e, signal, process|
puts "Ooops, got #{e} sending #{signal} to #{process.pid}"
end
end
signaller.send_signals
Instance Attribute Summary collapse
-
#locator(locator = nil) ⇒ Object
Specify or access the locator collaborator that is responsible for collecting the processes to operate on.
-
#signal(signal) ⇒ Object
readonly
Specify the signal to send to the targetted processes.
Instance Method Summary collapse
-
#after_signal(&callback) ⇒ Object
Callback invoked immediately after sending a signal to a process.
-
#before_signal(&callback) ⇒ Object
Callback invoked immediately before sending a signal to a process.
-
#dry_run ⇒ Object
Invoke if you want to have callbacks invoked, but not actually send signals to located processes.
-
#initialize(&block) ⇒ ProcessSignaller
constructor
A new instance of ProcessSignaller.
-
#on_error(&callback) ⇒ Object
Callback invoked if an error is encountered.
-
#on_no_processes(&callback) ⇒ Object
Callback invoked when no processes are found.
-
#send_signals ⇒ Object
Send the specified signal to all located processes, invoking callbacks as appropriate.
-
#target_parent(flag = false) ⇒ Object
Set to true if the signal should be sent to the parent of any located processes.
Constructor Details
#initialize(&block) ⇒ ProcessSignaller
44 45 46 47 48 49 50 51 |
# File 'lib/snipr/process_signaller.rb', line 44 def initialize(&block) @locator = ProcessLocator.new on_no_processes {} before_signal {} after_signal {} on_error {|e, process, signal| raise e} block.call(self) end |
Instance Attribute Details
#locator(locator = nil) ⇒ Object
Specify or access the locator collaborator that is responsible for collecting the processes to operate on
86 87 88 |
# File 'lib/snipr/process_signaller.rb', line 86 def locator(locator=nil) @locator ||= (locator || ProcessLocator.new) end |
#signal(signal) ⇒ Object (readonly)
Specify the signal to send to the targetted processes. This should be a string that maps one of the values listed here:
75 76 77 |
# File 'lib/snipr/process_signaller.rb', line 75 def signal @signal end |
Instance Method Details
#after_signal(&callback) ⇒ Object
Callback invoked immediately after sending a signal to a process. Will send both the signal and the KernelProcess object as returned by the locator.
108 109 110 |
# File 'lib/snipr/process_signaller.rb', line 108 def after_signal(&callback) @after_signal = callback end |
#before_signal(&callback) ⇒ Object
Callback invoked immediately before sending a signal to a process. Will send both the signal and the KernelProcess object as returned by the locator.
100 101 102 |
# File 'lib/snipr/process_signaller.rb', line 100 def before_signal(&callback) @before_signal = callback end |
#dry_run ⇒ Object
Invoke if you want to have callbacks invoked, but not actually send signals to located processes.
131 132 133 |
# File 'lib/snipr/process_signaller.rb', line 131 def dry_run @dry_run = true end |
#on_error(&callback) ⇒ Object
Callback invoked if an error is encountered. If this is within the context of attempting to send a signal to a process, then the exception, signal and KernelProcess object are sent. Otherwise, only the exception is sent.
117 118 119 |
# File 'lib/snipr/process_signaller.rb', line 117 def on_error(&callback) @on_error = callback end |
#on_no_processes(&callback) ⇒ Object
Callback invoked when no processes are found
92 93 94 |
# File 'lib/snipr/process_signaller.rb', line 92 def on_no_processes(&callback) @on_no_processes = callback end |
#send_signals ⇒ Object
Send the specified signal to all located processes, invoking callbacks as appropriate.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/snipr/process_signaller.rb', line 56 def send_signals processes = @locator.locate if processes.empty? @on_no_processes.call else processes.each do |process| signal_process(process) end end rescue StandardError => e @on_error.call(e) end |
#target_parent(flag = false) ⇒ Object
Set to true if the signal should be sent to the parent of any located processes. Defaults to false.
124 125 126 |
# File 'lib/snipr/process_signaller.rb', line 124 def target_parent(flag=false) @target_parent = flag end |