Class: ScriptCore::Runner

Inherits:
Object show all
Defined in:
lib/script_core/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout:, service_process:, message_processor_factory:) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
# File 'lib/script_core/runner.rb', line 9

def initialize(timeout:, service_process:, message_processor_factory:)
  @timeout = timeout
  @service_process = service_process
  @message_processor_factory = message_processor_factory
end

Instance Attribute Details

#message_processor_factoryObject (readonly)

Returns the value of attribute message_processor_factory.



7
8
9
# File 'lib/script_core/runner.rb', line 7

def message_processor_factory
  @message_processor_factory
end

#service_processObject (readonly)

Returns the value of attribute service_process.



7
8
9
# File 'lib/script_core/runner.rb', line 7

def service_process
  @service_process
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/script_core/runner.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#run(*data) ⇒ Object



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

def run(*data)
  message_processor = message_processor_factory.new

  begin
    code = service_process.open do |channel|
      Timeout.timeout(timeout) do
        data.each { |datum| channel.write(datum) }
        message_processor.process_all(channel)
      end
    end

    if code > 255
      message_processor.signal_signaled(code - 255)
    elsif code != 0
      message_processor.signal_abnormal_exit(code)
    end
  rescue Timeout::Error
    message_processor.signal_error(
      ScriptCore::EngineTimeQuotaError.new(quota: timeout)
    )
  end

  message_processor.to_result
end