Module: BatchKit::Helpers::Process
- Defined in:
- lib/batch-kit/helpers/process.rb
Overview
Provides support for running an external process. This support consists of support for:
- 
launching the process as a child 
- 
capturing the output of the process and logging it 
- 
handling the return code of the process, and raising an exception for failures. 
Class Method Summary collapse
- 
  
    
      .launch(cmd_line, options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Launch an external process with logging etc. 
- 
  
    
      .popen(cmd_line, options = {}, &block)  ⇒ Fixnum 
    
    
  
  
  
  
  
  
  
  
  
    Provides a means for executing a command-line. 
Class Method Details
.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 | 
.popen(cmd_line, options = {}, &block) ⇒ Fixnum
Provides a means for executing a command-line.
| 31 32 33 34 35 36 37 38 39 40 41 42 43 | # File 'lib/batch-kit/helpers/process.rb', line 31 def popen(cmd_line, = {}, &block) callback = [:callback] input = [:input] IO.popen(cmd_line, input ? 'r+' : 'r') do |pipe| while !pipe.eof? line = pipe.gets.chomp input.call(pipe, line) if input callback.call(line) if callback block.call(line) if block_given? end end $?.exitstatus end |