Method: BatchKit::Helpers::Process.launch
- Defined in:
- lib/batch-kit/helpers/process.rb
.launch(cmd_line, options = {}, &block) ⇒ Object
Launch an external process with logging etc. By default, an exception will be raised if the process returns a non-zero exit code.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/batch-kit/helpers/process.rb', line 61 def launch(cmd_line, = {}, &block) exe = cmd_line.is_a?(String) ? File.basename(Shellwords.shellwords(cmd_line.gsub(/\\/, '/')).first) : File.basename(cmd_line.first) raise_on_error = .fetch(:raise_on_error, true) show_duration = .fetch(:show_duration, true) success_code = .fetch(:success_code, 0) log = .fetch(:logger, BatchKit::LogManager.logger(exe)) log_level = .fetch(:log_level, :detail) unless block_given? || [:callback] = .dup [:callback] = lambda{ |line| log.send(log_level, line) } end log.trace("Executing command line: #{cmd_line}") if log begin start = Time.now rc = popen(cmd_line, , &block) ensure if log && show_duration log.detail "#{exe} completed in #{Time.now - start} seconds with exit code #{rc}" end end if raise_on_error ok = case success_code when Fixnum then success_code == rc when Array then success_code.include?(rc) end raise "#{exe} returned failure exit code #{rc}" unless ok end rc end |