Class: Pups::ExecCommand
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#cd ⇒ Object
readonly
Returns the value of attribute cd.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#raise_on_fail ⇒ Object
Returns the value of attribute raise_on_fail.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stop_signal ⇒ Object
Returns the value of attribute stop_signal.
Class Method Summary collapse
- .from_hash(hash, params) ⇒ Object
- .from_str(str, params) ⇒ Object
- .terminate_async(opts = {}) ⇒ Object
Instance Method Summary collapse
- #add(cmd) ⇒ Object
-
#initialize(params, cd = nil) ⇒ ExecCommand
constructor
A new instance of ExecCommand.
- #process_params(cmd) ⇒ Object
- #run ⇒ Object
- #spawn(command) ⇒ Object
Methods inherited from Command
#interpolate_params, interpolate_params, run
Constructor Details
#initialize(params, cd = nil) ⇒ ExecCommand
Returns a new instance of ExecCommand.
74 75 76 77 78 79 |
# File 'lib/pups/exec_command.rb', line 74 def initialize(params, cd = nil) @commands = [] @params = params @cd = interpolate_params(cd) @raise_on_fail = true end |
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
9 10 11 |
# File 'lib/pups/exec_command.rb', line 9 def background @background end |
#cd ⇒ Object (readonly)
Returns the value of attribute cd.
8 9 10 |
# File 'lib/pups/exec_command.rb', line 8 def cd @cd end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
8 9 10 |
# File 'lib/pups/exec_command.rb', line 8 def commands @commands end |
#raise_on_fail ⇒ Object
Returns the value of attribute raise_on_fail.
9 10 11 |
# File 'lib/pups/exec_command.rb', line 9 def raise_on_fail @raise_on_fail end |
#stdin ⇒ Object
Returns the value of attribute stdin.
9 10 11 |
# File 'lib/pups/exec_command.rb', line 9 def stdin @stdin end |
#stop_signal ⇒ Object
Returns the value of attribute stop_signal.
9 10 11 |
# File 'lib/pups/exec_command.rb', line 9 def stop_signal @stop_signal end |
Class Method Details
.from_hash(hash, params) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pups/exec_command.rb', line 50 def self.from_hash(hash, params) cmd = new(params, hash["cd"]) case c = hash["cmd"] when String cmd.add(c) when Array c.each { |i| cmd.add(i) } end cmd.background = hash["background"] cmd.stop_signal = hash["stop_signal"] || "TERM" cmd.raise_on_fail = hash["raise_on_fail"] if hash.key? "raise_on_fail" cmd.stdin = interpolate_params(hash["stdin"], params) cmd end |
.from_str(str, params) ⇒ Object
68 69 70 71 72 |
# File 'lib/pups/exec_command.rb', line 68 def self.from_str(str, params) cmd = new(params) cmd.add(str) cmd end |
.terminate_async(opts = {}) ⇒ Object
11 12 13 14 15 16 17 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 44 45 46 47 48 |
# File 'lib/pups/exec_command.rb', line 11 def self.terminate_async(opts = {}) return unless defined?(@@asyncs) Pups.log.info("Terminating async processes") @@asyncs.each do |async| Pups.log.info( "Sending #{async[:stop_signal]} to #{async[:command]} pid: #{async[:pid]}" ) begin Process.kill(async[:stop_signal], async[:pid]) rescue StandardError nil end end @@asyncs .map do |async| Thread.new do Timeout.timeout(opts[:wait] || 10) do Process.wait(async[:pid]) rescue StandardError nil end rescue Timeout::Error Pups.log.info( "#{async[:command]} pid:#{async[:pid]} did not terminate cleanly, forcing termination!" ) begin Process.kill("KILL", async[:pid]) Process.wait(async[:pid]) rescue Errno::ESRCH rescue Errno::ECHILD end end end .each(&:join) end |
Instance Method Details
#add(cmd) ⇒ Object
81 82 83 |
# File 'lib/pups/exec_command.rb', line 81 def add(cmd) @commands << process_params(cmd) end |
#process_params(cmd) ⇒ Object
138 139 140 141 |
# File 'lib/pups/exec_command.rb', line 138 def process_params(cmd) processed = interpolate_params(cmd) @cd ? "cd #{cd} && #{processed}" : processed end |
#run ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pups/exec_command.rb', line 85 def run commands.each do |command| Pups.log.info("> #{command}") pid = spawn(command) Pups.log.info(@result.readlines.join("\n")) if @result pid end rescue StandardError raise if @raise_on_fail end |
#spawn(command) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pups/exec_command.rb', line 96 def spawn(command) if background pid = Process.spawn(command) (@@asyncs ||= []) << { pid: pid, command: command, stop_signal: (stop_signal || "TERM") } Thread.new do begin Process.wait(pid) rescue Errno::ECHILD # already exited so skip end @@asyncs.delete_if { |async| async[:pid] == pid } end return pid end IO.popen(command, "w+") do |f| if stdin # need a way to get stdout without blocking Pups.log.info(stdin) f.write stdin f.close else Pups.log.info(f.readlines.join) end end unless $CHILD_STATUS == 0 err = Pups::ExecError.new( "#{command} failed with return #{$CHILD_STATUS.inspect}" ) err.exit_code = $CHILD_STATUS.exitstatus raise err end nil end |