Class: RobotLab::DelegationFuture

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

Overview

A promise-like object returned by robot.delegate(async: true).

The delegated task runs in a background thread. The caller can check whether the result is ready with resolved? and block until it arrives with value (or its alias wait).

Examples:

Fan-out to two specialists in parallel

f1 = manager.delegate(to: summarizer, task: "summarize ...", async: true)
f2 = manager.delegate(to: analyst,    task: "analyze ...",   async: true)

# Other work here while both run in parallel

summary  = f1.value           # blocks if not yet done
analysis = f2.value

With a timeout

result = future.value(timeout: 10)   # raises DelegationTimeout if too slow

Defined Under Namespace

Classes: DelegationTimeout

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot_name:, delegated_by:) ⇒ DelegationFuture

Returns a new instance of DelegationFuture.

Parameters:

  • robot_name (String)
  • delegated_by (String)


34
35
36
37
38
39
40
41
42
# File 'lib/robot_lab/delegation_future.rb', line 34

def initialize(robot_name:, delegated_by:)
  @robot_name   = robot_name
  @delegated_by = delegated_by
  @mutex        = Mutex.new
  @cv           = ConditionVariable.new
  @result       = nil
  @error        = nil
  @resolved     = false
end

Instance Attribute Details

#delegated_byString (readonly)

Returns name of the robot that created this future.

Returns:

  • (String)

    name of the robot that created this future



30
31
32
# File 'lib/robot_lab/delegation_future.rb', line 30

def delegated_by
  @delegated_by
end

#robot_nameString (readonly)

Returns name of the robot that was delegated to.

Returns:

  • (String)

    name of the robot that was delegated to



27
28
29
# File 'lib/robot_lab/delegation_future.rb', line 27

def robot_name
  @robot_name
end

Instance Method Details

#reject!(error) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/robot_lab/delegation_future.rb', line 85

def reject!(error)
  @mutex.synchronize do
    @error    = error
    @resolved = true
    @cv.broadcast
  end
end

#resolve!(result) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/robot_lab/delegation_future.rb', line 76

def resolve!(result)
  @mutex.synchronize do
    @result   = result
    @resolved = true
    @cv.broadcast
  end
end

#resolved?Boolean

True once the delegated task has completed (successfully or with an error).

Returns:

  • (Boolean)


47
48
49
# File 'lib/robot_lab/delegation_future.rb', line 47

def resolved?
  @mutex.synchronize { @resolved }
end

#value(timeout: nil) ⇒ RobotResult Also known as: wait

Block until the result is available and return it.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    maximum seconds to wait; nil means wait forever

Returns:

Raises:

  • (DelegationTimeout)

    if timeout is given and expires

  • (StandardError)

    re-raises any error thrown by the delegated task



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/robot_lab/delegation_future.rb', line 57

def value(timeout: nil)
  @mutex.synchronize do
    unless @resolved
      @cv.wait(@mutex, timeout)
    end

    unless @resolved
      raise DelegationTimeout,
            "Delegation to '#{@robot_name}' timed out after #{timeout}s"
    end

    raise @error if @error

    @result
  end
end