Class: Procrastinate::Task::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/procrastinate/task/result.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



8
9
10
11
12
# File 'lib/procrastinate/task/result.rb', line 8

def initialize
  @value_ready    = Procrastinate::Utils::OneTimeFlag.new
  @value          = nil
  @exception      = false
end

Instance Method Details

#incoming_message(obj) ⇒ Object

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

*control thread*


17
18
19
20
21
22
# File 'lib/procrastinate/task/result.rb', line 17

def incoming_message(obj)
  return if ready?
  
  @value = obj
  @value_ready.set
end

#process_diedObject

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*


29
30
31
32
33
34
# File 'lib/procrastinate/task/result.rb', line 29

def process_died
  return if ready?
  
  @exception = true    
  @value_ready.set
end

#ready?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/procrastinate/task/result.rb', line 46

def ready?
  @value_ready.set?
end

#valueObject



36
37
38
39
40
41
42
43
44
# File 'lib/procrastinate/task/result.rb', line 36

def value
  @value_ready.wait
  
  if @exception
    raise Procrastinate::ChildDeath, "Child process died before producing a value."
  else
    @value
  end
end