Class: SafeExec::PipeExecutor::TimeoutDelegate

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

Instance Method Summary collapse

Constructor Details

#initialize(executor, timeout, exception) ⇒ TimeoutDelegate

Returns a new instance of TimeoutDelegate.



77
78
79
# File 'lib/safe_exec/pipe_executor.rb', line 77

def initialize(executor, timeout, exception)
  @executor, @timeout, @exception = executor, timeout, exception
end

Instance Method Details

#run(cmd, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/safe_exec/pipe_executor.rb', line 81

def run(cmd, *args)
  @executor.run(cmd, *args) do |t|
    begin
      Timeout.timeout(@timeout, @exception) do
        yield t if block_given?
        t.value
      end
    rescue @exception => e
      begin
        @executor.send(:abort)
        Process.kill("TERM", t.pid)
      rescue Errno::ESRCH
      ensure
        raise e
      end
    end
  end
end