Sha256: 2e8187f50bc13283ec4d07aa9cf631d890fe3c920b51d680e04c82ab99973964

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

module Makesure
  
  class Runner
    
    attr_accessor :cmd
    
    def initialize(cmd, opts = {})
      @cmd = cmd
      @opts = {
        :test => "val"
      }.merge(opts)
    end
    
    def run!
      io = IO.popen(cmd)
      Makesure.log "Running cmd '#{cmd}' with pid #{io.pid}"

      Process.wait # needed to populate $?
      # seems to grab the exitstatus of the last cmd in a chain, ie:
      #   ls -al | grep -i file-that-exits                        # exits w/ 0
      #   ls -al | grep -i -file-that-does-not-exist              # exits w/ 1
      #   ls -al | grep -i -file-that-does-not-exist | echo "yay" # exits w/ 0
      exitstatus = $?.exitstatus

      Makesure.log "cmd exited with status #{exitstatus}"

      output = io.read

      unless exitstatus == 0
        Makesure.warn "oh shitttt something broke, tell everybody!"
        body = ""
        body << "[#{Time.now.to_s}] #{cmd.inspect} [pid #{io.pid}] exited with status #{exitstatus}, output follows:\n\n"
        body << output
        Makesure.send_alert("ERROR in command alert: #{cmd.inspect}", body)
      end

      if output.empty?
        Makesure.log "no output"
      else
        Makesure.log ">>> begin output"
        output.each_line do |l|
          Makesure.log l
        end
        Makesure.log "<<< end output"
      end
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
makesure-0.0.6 lib/makesure/runner.rb
makesure-0.0.5 lib/makesure/runner.rb
makesure-0.0.4 lib/makesure/runner.rb
makesure-0.0.3 lib/makesure/runner.rb
makesure-0.0.2 lib/makesure/runner.rb
makesure-0.0.1 lib/makesure/runner.rb