Class: Bolt::PlanFuture
- Inherits:
-
Object
- Object
- Bolt::PlanFuture
- Defined in:
- lib/bolt/plan_future.rb
Instance Attribute Summary collapse
-
#fiber ⇒ Object
readonly
Returns the value of attribute fiber.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#plan_stack ⇒ Object
Returns the value of attribute plan_stack.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #current_plan ⇒ Object
-
#initialize(fiber, id, plan_id:, name: nil, scope: nil) ⇒ PlanFuture
constructor
A new instance of PlanFuture.
- #name ⇒ Object
- #original_plan ⇒ Object
- #raise(exception) ⇒ Object
- #resume ⇒ Object
- #state ⇒ Object
- #to_s ⇒ Object
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
#fiber ⇒ Object (readonly)
Returns the value of attribute fiber.
7 8 9 |
# File 'lib/bolt/plan_future.rb', line 7 def fiber @fiber end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/bolt/plan_future.rb', line 7 def id @id end |
#plan_stack ⇒ Object
Returns the value of attribute plan_stack.
8 9 10 |
# File 'lib/bolt/plan_future.rb', line 8 def plan_stack @plan_stack end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
7 8 9 |
# File 'lib/bolt/plan_future.rb', line 7 def scope @scope end |
#value ⇒ Object
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
45 46 47 |
# File 'lib/bolt/plan_future.rb', line 45 def alive? fiber.alive? end |
#current_plan ⇒ Object
33 34 35 |
# File 'lib/bolt/plan_future.rb', line 33 def current_plan @plan_stack.first end |
#name ⇒ Object
37 38 39 |
# File 'lib/bolt/plan_future.rb', line 37 def name @name || @id end |
#original_plan ⇒ Object
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 |
#resume ⇒ Object
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 |
#state ⇒ Object
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_s ⇒ Object
41 42 43 |
# File 'lib/bolt/plan_future.rb', line 41 def to_s "Future '#{name}'" end |