class Procrastinate::Task::Result

A single value result, like from a normal method call. Return an instance of this from your task#result method to enable result handling.

Public Class Methods

new() click to toggle source
# File lib/procrastinate/task/result.rb, line 8
def initialize
  @value_ready    = Procrastinate::Utils::OneTimeFlag.new
  @value          = nil
  @exception      = false
end

Public Instance Methods

incoming_message(obj) click to toggle source

Gets passed all messages sent by the child process for this task.

*control thread*
# File lib/procrastinate/task/result.rb, line 17
def incoming_message(obj)
  fail "Read child answer too late, already marked dead." if ready? && @exception
  fail "Double child answer." if ready?
  
  @value = obj
  @value_ready.set
end
process_died() click to toggle source

Notifies this result that the process has died. If this happens before a process result is passed to incoming_message, that message will never arrive.

*control thread*
# File lib/procrastinate/task/result.rb, line 30
def process_died
  return if ready?
  
  @exception = true    
  @value_ready.set
end
ready?() click to toggle source
# File lib/procrastinate/task/result.rb, line 47
def ready?
  @value_ready.set?
end
value() click to toggle source
# File lib/procrastinate/task/result.rb, line 37
def value
  @value_ready.wait
  
  if @exception
    raise Procrastinate::ChildDeath, "Child process died before producing a value."
  else
    @value
  end
end