psadmin-io / psadmin-plus

A psadmin helper script
MIT License
17 stars 4 forks source link

Setup timeout for all commands #89

Open NateWerner opened 6 years ago

NateWerner commented 6 years ago

psadmin command can regularily hang up on a corrupted web domains or tuxedo domain with a hung process. Add freature that all commands run a process with timeout and capture stderr and stdout, as most output from tuxedo is from stderr. This way the output will look familiar to manually running psadmin.

New to ruby, so still learning its structure, but this is a good example of a timeout function that could be called from do_cmd(). Not sure how to handle the exitCode return in function. I may try this if I get time.

def do_cmd_with_timeout(cmd, timeout)
  begin
    # stdout, stderr pipes
    rout, wout = IO.pipe
    rerr, werr = IO.pipe
    stdout, stderr = nil

    pid = Process.spawn(cmd, pgroup: true, :out => wout, :err => werr)

    Timeout.timeout(timeout) do
      Process.waitpid(pid)
      exitCode = $?.exitstatus
      # close write ends so we can read from them
      wout.close
      werr.close

      stdout = rout.readlines.join
      stderr = rerr.readlines.join
    end
  rescue Timeout::Error
    Process.kill(-9, pid)
    Process.detach(pid)
  ensure
    wout.close unless wout.closed?
    werr.close unless werr.closed?
    # dispose the read ends of the pipes
    rout.close
    rerr.close
  end
  # display as it normally appears
  p "#{stderr}#{stdout}"
end