Class: Bolt::PlanFuture

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fiber, id, plan_id:, name: nil, scope: nil) ⇒ PlanFuture

Returns a new instance of PlanFuture.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bolt/plan_future.rb', line 10

def initialize(fiber, id, plan_id:, name: nil, scope: nil)
  @fiber = fiber
  @id    = id
  @name  = name
  @value = nil

  # Default to Puppet's current global_scope, otherwise things will
  # blow up when the Fiber Executor tries to override the global_scope.
  @scope = scope || Puppet.lookup(:global_scope) { nil }

  # The plan invocation ID when the Future is created may be
  # different from the plan ID of the Future when we switch to it if a new
  # plan was run inside the Future, so keep track of the plans that a
  # Future is executing in as a stack. When one plan finishes, pop it off
  # since now we're in the calling plan. These IDs are unique to each plan
  # invocation, not just plan names.
  @plan_stack = [plan_id]
end

Instance Attribute Details

#fiberObject (readonly)

Returns the value of attribute fiber.



7
8
9
# File 'lib/bolt/plan_future.rb', line 7

def fiber
  @fiber
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/bolt/plan_future.rb', line 7

def id
  @id
end

#plan_stackObject

Returns the value of attribute plan_stack.



8
9
10
# File 'lib/bolt/plan_future.rb', line 8

def plan_stack
  @plan_stack
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/bolt/plan_future.rb', line 7

def scope
  @scope
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/bolt/plan_future.rb', line 8

def value
  @value
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/bolt/plan_future.rb', line 45

def alive?
  fiber.alive?
end

#current_planObject



33
34
35
# File 'lib/bolt/plan_future.rb', line 33

def current_plan
  @plan_stack.first
end

#nameObject



37
38
39
# File 'lib/bolt/plan_future.rb', line 37

def name
  @name || @id
end

#original_planObject



29
30
31
# File 'lib/bolt/plan_future.rb', line 29

def original_plan
  @plan_stack.last
end

#raise(exception) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolt/plan_future.rb', line 49

def raise(exception)
  # Make sure the value gets set
  @value = exception
  # This was introduced in Ruby 2.7
  begin
    # Raise an exception to kill the Fiber. If the Fiber has not been
    # resumed yet, or is already terminated this will raise a FiberError.
    # We don't especially care about the FiberError, as long as the Fiber
    # doesn't report itself as alive.
    fiber.raise(exception)
  rescue FiberError
    # If the Fiber is still alive, resume it with a block to raise the
    # exception which will terminate it.
    if fiber.alive?
      fiber.resume { raise(exception) }
    end
  end
end

#resumeObject



68
69
70
71
72
73
74
# File 'lib/bolt/plan_future.rb', line 68

def resume
  if fiber.alive?
    @value = fiber.resume
  else
    @value
  end
end

#stateObject



76
77
78
79
80
81
82
83
84
# File 'lib/bolt/plan_future.rb', line 76

def state
  if fiber.alive?
    "running"
  elsif value.is_a?(Exception)
    "error"
  else
    "done"
  end
end

#to_sObject



41
42
43
# File 'lib/bolt/plan_future.rb', line 41

def to_s
  "Future '#{name}'"
end