Top Level Namespace

Defined Under Namespace

Modules: SalesforceDeployTool Classes: Pinwheel

Instance Method Summary collapse

Instance Method Details

#myexec(cmd, opts = {}) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
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
95
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
# File 'lib/salesforcedeploytool/functions.rb', line 19

def myexec cmd, opts = {}
  
  opts[:stderr] = false if opts[:stderr].nil?
  opts[:stdout] = false if opts[:stdout].nil?
  opts[:exit_on_error] = true if opts[:exit_on_error].nil?
  opts[:timeout] = 600 if opts[:timeout].nil?
  opts[:spinner] = true if opts[:spinner].nil?
  opts[:message] = false if opts[:message].nil?
  opts[:okmsg] = false if opts[:okmsg].nil?
  opts[:failmsg] = false if opts[:failmsg].nil?
  logger = opts[:logger].nil? ? false : opts[:logger]

  command = File.basename($0)
  exit_status = 0
  start = Time.new
  pinwheel = Pinwheel.new if opts[:spinner]

  # Print header message
  print opts[:message] if opts[:message]

  stdout_all = ""
  stderr_all = ""
  begin 
    stdin,stdout,stderr,wait_thr = Open3.popen3(cmd)
    pid = wait_thr.pid
    # This block uses kernel.select to monitor if there is data on stdout IO 
    # object every 1 second. Then we use a nonblocking read ... so the whole 
    # idea is: Check if there is stdin to read, in 1 second unblock and 
    # try to read. Loop every 1 second over and over the same process until 
    # the process finishes or timeout is reached.
    elapsed = 0
    while wait_thr.status and (elapsed = Time.now - start) < opts[:timeout]
      Kernel.select([stdout,stderr],nil,nil,1)

      # Read STDIN in a nonblock way.
      begin
        stdout_line = stdout.read_nonblock(100) 
        print stdout_line if opts[:stdout]
        stdout_all += stdout_line
        # spin the pinwheel
        pinwheel.spin_it if opts[:spinner]
      rescue IO::WaitReadable
        # Exception raised when there is nothing to read
      rescue EOFError
        # Exception raised EOF is reached
        break
      end

      # Read STDERR in a nonblock way.
      begin
        stderr_line = stderr.read_nonblock(100) 
        print stderr_line if opts[:stderr]
        stderr_all += stderr_line
        # spin the pinwheel
        pinwheel.spin_it if opts[:spinner]
      rescue IO::WaitReadable
        # Exception raised when there is nothing to read
      rescue EOFError
        # Exception raised EOF is reached
        break
      end

    end

    # Log stdout output
    logger.info "\n" + stdout_all if logger and ! stdout_all.empty?
    logger.error "\n" + stderr_all if logger and ! stderr_all.empty?

    if elapsed > opts[:timeout]
      # We need to kill the process 
      Process.kill("KILL", pid)
      timeout_error_message = "Timeout #{opts[:timeout]} exceeded for #{cmd}"
      logger.error timeout_error_message if logger
      raise timeout_error_message
    end

    # Clean the pinwheel
    pinwheel.clean if opts[:spinner]

    # Handle the exit status:
    exit_status = wait_thr.value.exitstatus

    # Print fail or ok message
    if exit_status == 0
      puts opts[:okmsg] if opts[:okmsg]
    else
      puts opts[:failmsg] if opts[:failmsg]
    end

    # Exit on error
    if exit_status > 0
      $stderr.puts "#{command} error: Command returned non zero - error was:\n#{stderr_all}" if opts[:stderr]
      exit 1 if opts[:exit_on_error]
    end

  rescue => e
    $stderr.puts "\n#{command} error: Command failed, error was:\n#{e}".red
    exit 1 if opts[:exit_on_error]
    exit_status = 127
  end

  exit_status

end