Class: DaemonRunner::ShellOut

Inherits:
Object
  • Object
show all
Defined in:
lib/daemon_runner/shell_out.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: nil, cwd: '/tmp', timeout: 15, wait: true, valid_exit_codes: [0]) ⇒ ShellOut

Returns a new instance of ShellOut.

Parameters:

  • command (String) (defaults to: nil)

    the command to run

  • cwd (String) (defaults to: '/tmp')

    the working directory to run the command

  • timeout (Fixnum) (defaults to: 15)

    the command timeout

  • wait (Boolean) (defaults to: true)

    wheather to wait for the command to finish

  • valid_exit_codes (Array<Fixnum>) (defaults to: [0])

    exit codes that aren’t flagged as failures



24
25
26
27
28
29
30
# File 'lib/daemon_runner/shell_out.rb', line 24

def initialize(command: nil, cwd: '/tmp', timeout: 15, wait: true, valid_exit_codes: [0])
  @command = command
  @cwd = cwd
  @timeout = timeout
  @wait = wait
  @valid_exit_codes = valid_exit_codes
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/daemon_runner/shell_out.rb', line 6

def command
  @command
end

#cwdObject (readonly)

Returns the value of attribute cwd.



6
7
8
# File 'lib/daemon_runner/shell_out.rb', line 6

def cwd
  @cwd
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/daemon_runner/shell_out.rb', line 5

def stdout
  @stdout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



6
7
8
# File 'lib/daemon_runner/shell_out.rb', line 6

def timeout
  @timeout
end

#valid_exit_codesObject (readonly)

Returns the value of attribute valid_exit_codes.



6
7
8
# File 'lib/daemon_runner/shell_out.rb', line 6

def valid_exit_codes
  @valid_exit_codes
end

#waitObject (readonly)

Returns the value of attribute wait.



6
7
8
# File 'lib/daemon_runner/shell_out.rb', line 6

def wait
  @wait
end

Class Method Details

.wait2(pid = nil, flags = 0) ⇒ Process::Status?

Wait for the process with the given pid to finish

Parameters:

  • pid (Fixnum) (defaults to: nil)

    the pid to wait on

  • flags (Fixnum) (defaults to: 0)

    flags to Process.wait2

Returns:

  • (Process::Status, nil)

    the process status or nil if no process was found



12
13
14
15
16
17
# File 'lib/daemon_runner/shell_out.rb', line 12

def self.wait2(pid = nil, flags = 0)
  return nil if pid.nil?
  Process.wait2(pid, flags)[1]
rescue Errno::ECHILD
  nil
end

Instance Method Details

#run!Mixlib::ShellOut, Fixnum

Run command

Returns:

  • (Mixlib::ShellOut, Fixnum)

    mixlib shellout client or a pid depending on the value of #wait



34
35
36
37
38
39
40
41
# File 'lib/daemon_runner/shell_out.rb', line 34

def run!
  validate_command
  if wait
    run_and_wait
  else
    run_and_detach
  end
end

#wait2(flags = 0) ⇒ Process::Status?

Wait for the process to finish

Parameters:

  • flags (Fixnum) (defaults to: 0)

    flags to Process.wait2

Returns:

  • (Process::Status, nil)

    the process status or nil if no process was found



46
47
48
# File 'lib/daemon_runner/shell_out.rb', line 46

def wait2(flags = 0)
  self.class.wait2(@pid, flags)
end