Class: LitmusPaper::Metric::Script
- Inherits:
-
Object
- Object
- LitmusPaper::Metric::Script
- Defined in:
- lib/litmus_paper/metric/script.rb
Instance Attribute Summary collapse
-
#script_pid ⇒ Object
readonly
Returns the value of attribute script_pid.
Instance Method Summary collapse
- #current_health ⇒ Object
-
#initialize(command, weight, options = {}) ⇒ Script
constructor
A new instance of Script.
- #kill_and_reap_script(pid) ⇒ Object
- #result ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(command, weight, options = {}) ⇒ Script
Returns a new instance of Script.
8 9 10 11 12 |
# File 'lib/litmus_paper/metric/script.rb', line 8 def initialize(command, weight, = {}) @command = command @weight = weight @timeout = .fetch(:timeout, 5) end |
Instance Attribute Details
#script_pid ⇒ Object (readonly)
Returns the value of attribute script_pid.
6 7 8 |
# File 'lib/litmus_paper/metric/script.rb', line 6 def script_pid @script_pid end |
Instance Method Details
#current_health ⇒ Object
14 15 16 |
# File 'lib/litmus_paper/metric/script.rb', line 14 def current_health @weight * result end |
#kill_and_reap_script(pid) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/litmus_paper/metric/script.rb', line 45 def kill_and_reap_script(pid) Process.kill(9, pid) stop_time = Time.now + 2 while Time.now < stop_time if Process.waitpid(pid, Process::WNOHANG) LitmusPaper.logger.info("Reaped PID #{pid}") return else sleep 0.1 end end LitmusPaper.logger.error("Unable to reap PID #{pid}") rescue Errno::ESRCH LitmusPaper.logger.info("Attempted to kill non-existent PID #{pid} (ESRCH)") rescue Errno::ECHILD LitmusPaper.logger.info("Attempted to reap PID #{pid} but it has already been reaped (ECHILD)") end |
#result ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/litmus_paper/metric/script.rb', line 18 def result value = 0 script_status = script_stdout = script_stderr = nil Open3.popen3(@command, :pgroup=>true) do |stdin, stdout, stderr, wait_thr| @script_pid = wait_thr.pid thstderr = Thread.new { stderr.read } thstdout = Thread.new { stdout.read } if !wait_thr.join(@timeout) # wait thread does not end within timeout kill_and_reap_script(-@script_pid) # kill the process group raise Timeout::Error end script_stderr = thstderr.value script_stdout = thstdout.value value = script_stdout.strip script_status = wait_thr.value end unless script_status.success? LitmusPaper.logger.info("Available check to #{@command} failed with status #{script_status.exitstatus}") LitmusPaper.logger.info("Failed stdout: #{script_stdout}") LitmusPaper.logger.info("Failed stderr: #{script_stderr}") end value.to_f rescue Timeout::Error LitmusPaper.logger.info("Available check to '#{@command}' timed out") 0 end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/litmus_paper/metric/script.rb', line 63 def to_s "Metric::Script(#{@command}, #{@weight})" end |