Class: Shellshot::Command

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

Constant Summary collapse

DEFAULT_TIMEOUT =

1 hour

60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/shellshot.rb', line 15

def options
  @options
end

#pidObject

Returns the value of attribute pid.



15
16
17
# File 'lib/shellshot.rb', line 15

def pid
  @pid
end

#statusObject

Returns the value of attribute status.



15
16
17
# File 'lib/shellshot.rb', line 15

def status
  @status
end

Instance Method Details

#exec(command, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shellshot.rb', line 17

def exec(command, options = {})

  self.options = options
  timeout = options[:timeout] || DEFAULT_TIMEOUT

  prepare_pipes
  self.pid = fork do
    close_reading_pipes
    redefine_stds
    system_exec(*command)
  end

  begin
    wait_for(timeout)
  rescue Timeout::Error => e
    terminate_child_process
    raise Timeout::Error, "execution of #{command} exceeded #{timeout} seconds"
  end

  true
end

#stderr_contentsObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/shellshot.rb', line 39

def stderr_contents
  @stderr_contents ||= unless stderr_defined?
    @stderr_wr.close
    contents = @stderr_rd.read
    @stderr_rd.close
    contents
  else
    File.read(stderr_location)
  end
end

#stdout_contentsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/shellshot.rb', line 50

def stdout_contents
  @stdout_contents ||= unless stdout_defined?
    @stdout_wr.close
    contents = @stdout_rd.read
    @stdout_rd.close
    contents
  else
    File.read(stdout_location)
  end
end

#system_execObject



11
# File 'lib/shellshot.rb', line 11

alias_method :system_exec, :exec