Class: SolargraphTestCoverage::ForkProcess

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

Overview

stackoverflow.com/questions/1076257/returning-data-from-forked-processes When called with a block, runs the content of said block in a new (forked) process the return value of the process/block can be captured and used in parent process

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForkProcess

Returns a new instance of ForkProcess.



12
13
14
# File 'lib/solargraph_test_coverage/fork_process.rb', line 12

def initialize
  @read, @write = IO.pipe
end

Class Method Details

.call(&block) ⇒ Object



8
9
10
# File 'lib/solargraph_test_coverage/fork_process.rb', line 8

def self.call(&block)
  new.run(&block)
end

Instance Method Details

#run(&block) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solargraph_test_coverage/fork_process.rb', line 16

def run(&block)
  pid = fork do
    @read.close
    Marshal.dump(run_block_with_timeout(&block), @write)
    exit!(0) # skips exit handlers.
  end

  @write.close
  result = @read.read

  Process.wait(pid)
  raise ChildFailedError, "Couldn't read pipe" if result.nil?

  Marshal.load(result).tap do |r|
    raise ChildFailedError, "Marshal.load(result) returned nil" if r.nil?
    raise ChildFailedError, r.message if r.is_a? Exception
  end
end