Class: ForkBreak::Process

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Process.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fork_break/process.rb', line 7

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
    block.call(breakpoints)
    breakpoints << :forkbreak_end

    self.class.breakpoint_setter = nil
  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 Method Details

#finishObject



45
46
47
# File 'lib/fork_break/process.rb', line 45

def finish
  run_until(:forkbreak_end)
end

#run_until(breakpoint) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/fork_break/process.rb', line 20

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fork_break/process.rb', line 28

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
      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