Class: FutureAgent

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

Defined Under Namespace

Classes: ChildDied

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FutureAgent

:nodoc:



50
51
52
# File 'lib/future_agent/future_agent.rb', line 50

def initialize( &block ) # :nodoc:
  @async_block = block
end

Class Method Details

.fork(&block) ⇒ Object

Returns a new agent instances that has been forked and is already running.

Retrieve the result from this agent by calling agent.result


43
44
45
46
47
48
# File 'lib/future_agent/future_agent.rb', line 43

def self.fork( &block )
  agent = new( &block )
  child_pid = agent.send :fork!
  @agents[child_pid] = agent
  agent
end

.handle_pid(signal_number, pid) ⇒ Object

Handles the death of a signel agent child identified by the PID.

Usually invoked automatically by the FutureAgent SIGCHLD/SIGCLD handler. 
If you overwrite this handler with your own however, you will need to 
call this method to clean up the agent state if an agent dies.


26
27
28
29
30
31
32
33
34
# File 'lib/future_agent/future_agent.rb', line 26

def self.handle_pid( signal_number, pid )
  unless @agents.has_key?( pid )
    call_original_child_handler signal_number
    return
  end

  agent = @agents.delete(pid)
  agent.result if( $?.success? )
end

.setup_signal_handlerObject

Installs the SIGCHLD/SIGCLD signal handler for FutureAgent. Usually this

will not need to be invoked manually; it is invoked when the class is required.
If you overwrite the SIGCHLD handler somewhere after requiring this class
and you want to reset the default behaviour, you can call this method.

Caution: The signal handler will try to call the previously installed
handler for non-agent children that die. You can probably build a handler
invocation cycle if you aren't careful.


16
17
18
# File 'lib/future_agent/future_agent.rb', line 16

def self.setup_signal_handler
  @old_handler = Signal.trap( "SIGCLD" ) { |signo| child_handler( signo ) }
end

Instance Method Details

#resultObject

Returns the result of the agent. If the result isn’t available yet, calling

this method will block until the agent is done.

Calling this method will raise a FutureAgent::ChildDied exception if the
child process died with an exit status other than 0 (usually due to a raised
exception)


62
63
64
65
66
67
68
69
70
# File 'lib/future_agent/future_agent.rb', line 62

def result
  return @result if defined?( @result )

  begin
    read_result
  ensure
    @read_pipe.close
  end
end