Class: ForkBreak::Process

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false, &block) ⇒ Process

Returns a new instance of Process.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fork_break/process.rb', line 11

def initialize(debug = false, &block)
  @debug = debug
  @fork = Fork.new(:return, :to_fork, :from_fork) do |child_fork|
    self.class.breakpoint_setter = breakpoints = BreakpointSetter.new(child_fork, debug)

    breakpoints << :forkbreak_start
    returned_value = block.call(breakpoints)
    breakpoints << :forkbreak_end

    self.class.breakpoint_setter = nil
    returned_value
  end
end

Class Attribute Details

.breakpoint_setterObject

Returns the value of attribute breakpoint_setter.



4
5
6
# File 'lib/fork_break/process.rb', line 4

def breakpoint_setter
  @breakpoint_setter
end

Instance Attribute Details

#return_valueObject (readonly)

Returns the value of attribute return_value.



7
8
9
# File 'lib/fork_break/process.rb', line 7

def return_value
  @return_value
end

Instance Method Details

#finishObject



53
54
55
# File 'lib/fork_break/process.rb', line 53

def finish
  run_until(:forkbreak_end)
end

#run_until(breakpoint) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/fork_break/process.rb', line 25

def run_until(breakpoint)
  @next_breakpoint = breakpoint
  @fork.execute unless @fork.pid
  puts "Parent is sending object #{breakpoint} to #{@fork.pid}" if @debug
  @fork.send_object(breakpoint)
  self
end

#wait(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fork_break/process.rb', line 33

def wait(options = {})
  # A timeout value of nil will execute the block without any timeout
  Timeout.timeout(options[:timeout], WaitTimeout) do
    loop do
      brk = @fork.receive_object
      puts "Parent is receiving object #{brk} from #{@fork.pid}" if @debug

      @return_value = @fork.return_value if brk == :forkbreak_end

      if brk == @next_breakpoint
        return self
      elsif brk == :forkbreak_end
        raise BreakpointNotReachedError, "Never reached breakpoint #{@next_breakpoint.inspect}"
      end
    end
  end
rescue EOFError => exception
  raise @fork.exception || exception
end