Module: Rails::Hyperdrive::ConsoleExecutor

Defined in:
lib/rails/hyperdrive/console_executor.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_TIMEOUT_SECONDS =
30

Class Method Summary collapse

Class Method Details

.eval(code, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ Object



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
# File 'lib/rails/hyperdrive/console_executor.rb', line 27

def eval(code, timeout: DEFAULT_TIMEOUT_SECONDS)
  original_stdout = $stdout
  original_stderr = $stderr
  captured_out = StringIO.new
  captured_err = StringIO.new
  $stdout = captured_out
  $stderr = captured_err

  started_at = monotonic_now
  result = nil
  exception = nil

  begin
    ::Timeout.timeout(timeout.to_f) do
      result = TOPLEVEL_BINDING.eval(code, "(hyperdrive run_ruby)", 1)
    end
  rescue Exception => e # rubocop:disable Lint/RescueException
    exception = e
  end

  Result.new(
    result: result,
    stdout: captured_out.string,
    stderr: captured_err.string,
    elapsed_ms: ((monotonic_now - started_at) * 1000).round,
    exception: exception
  )
ensure
  $stdout = original_stdout
  $stderr = original_stderr
end

.monotonic_nowObject



59
60
61
# File 'lib/rails/hyperdrive/console_executor.rb', line 59

def monotonic_now
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end