Class: Temporalio::Workflow::Future
- Inherits:
-
Object
- Object
- Temporalio::Workflow::Future
- Defined in:
- lib/temporalio/workflow/future.rb
Overview
Asynchronous future for use in workflows to do concurrent and background work. This can only be used inside workflows.
Instance Attribute Summary collapse
-
#failure ⇒ Exception?
Failure if this future failed or nil if it didn't or hasn't yet completed.
-
#result ⇒ Object?
Result if the future is done or nil if it is not.
Class Method Summary collapse
-
.all_of(*futures) ⇒ Future<nil>
Return a future that completes when all of the given futures complete or any future fails.
-
.any_of(*futures) ⇒ Future<Object>
Return a future that completes when any of the given futures complete.
-
.try_all_of(*futures) ⇒ Future<nil>
Return a future that completes when all of the given futures complete regardless of success/fail.
-
.try_any_of(*futures) ⇒ Future<Future<Object>>
Return a future that completes when the first future completes.
Instance Method Summary collapse
-
#done? ⇒ Boolean
True if the future is done, false otherwise.
-
#failure? ⇒ Boolean
True if done and failed, false if still running or succeeded.
-
#initialize(&block) ⇒ Future
constructor
Create a new future.
-
#result? ⇒ Boolean
True if done and not a failure, false if still running or failed.
-
#wait ⇒ Object
Wait on the future to complete.
-
#wait_no_raise ⇒ Object?
Wait on the future to complete.
Constructor Details
#initialize(&block) ⇒ Future
Create a new future. If created with a block, the block is started in the background and its success/raise is the result of the future. If created without a block, the result or failure can be set on it.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/temporalio/workflow/future.rb', line 75 def initialize(&block) @done = false @result = nil @failure = nil @block_given = block_given? return unless block_given? @fiber = Fiber.schedule do @result = block.call # steep:ignore rescue Exception => e # rubocop:disable Lint/RescueException # These errors invalidate the whole workflow task. If stored and only raised on wait, the activation may # complete successfully with commands that were emitted before the error. raise if e.is_a?(Internal::WorkflowTaskFailureError) @failure = e ensure @done = true end end |
Instance Attribute Details
#failure ⇒ Exception?
Returns Failure if this future failed or nil if it didn't or hasn't yet completed.
71 72 73 |
# File 'lib/temporalio/workflow/future.rb', line 71 def failure @failure end |
#result ⇒ Object?
Returns Result if the future is done or nil if it is not. This will return nil if the result is nil too. Users can use #done? to differentiate the situations.
68 69 70 |
# File 'lib/temporalio/workflow/future.rb', line 68 def result @result end |
Class Method Details
.all_of(*futures) ⇒ Future<nil>
Return a future that completes when all of the given futures complete or any future fails. The returned future will return nil on success or raise an exception if any of the futures failed. This means if any future fails, this will not wait for the other futures to complete. To wait for all futures to complete no matter what, see try_all_of.
33 34 35 36 37 38 39 40 |
# File 'lib/temporalio/workflow/future.rb', line 33 def self.all_of(*futures) Future.new do Workflow.wait_condition(cancellation: nil) { futures.all?(&:done?) || futures.any?(&:failure?) } # Raise on error if any futures.find(&:failure?)&.wait nil end end |
.any_of(*futures) ⇒ Future<Object>
Return a future that completes when any of the given futures complete. The returned future will return the first completed future's value or raise the first completed future's exception. To not raise the exception, see try_any_of.
17 18 19 20 21 22 23 |
# File 'lib/temporalio/workflow/future.rb', line 17 def self.any_of(*futures) Future.new do Workflow.wait_condition(cancellation: nil) { futures.any?(&:done?) } # We know a future is always returned from find, the || just helps type checker (futures.find(&:done?) || raise).wait end end |
.try_all_of(*futures) ⇒ Future<nil>
Return a future that completes when all of the given futures complete regardless of success/fail. The returned future will return nil when all futures are complete.
59 60 61 62 63 64 |
# File 'lib/temporalio/workflow/future.rb', line 59 def self.try_all_of(*futures) Future.new do Workflow.wait_condition(cancellation: nil) { futures.all?(&:done?) } nil end end |
.try_any_of(*futures) ⇒ Future<Future<Object>>
Return a future that completes when the first future completes. The result of the future is the future from the list that completed first. The future returned will never raise even if the first completed future fails.
47 48 49 50 51 52 |
# File 'lib/temporalio/workflow/future.rb', line 47 def self.try_any_of(*futures) Future.new do Workflow.wait_condition(cancellation: nil) { futures.any?(&:done?) } futures.find(&:done?) || raise end end |
Instance Method Details
#done? ⇒ Boolean
Returns True if the future is done, false otherwise.
96 97 98 |
# File 'lib/temporalio/workflow/future.rb', line 96 def done? @done end |
#failure? ⇒ Boolean
Returns True if done and failed, false if still running or succeeded.
118 119 120 |
# File 'lib/temporalio/workflow/future.rb', line 118 def failure? done? && !failure.nil? end |
#result? ⇒ Boolean
Returns True if done and not a failure, false if still running or failed.
101 102 103 |
# File 'lib/temporalio/workflow/future.rb', line 101 def result? done? && !failure end |
#wait ⇒ Object
Wait on the future to complete. This will return the success or raise the failure. To not raise, use #wait_no_raise.
140 141 142 143 144 145 |
# File 'lib/temporalio/workflow/future.rb', line 140 def wait Workflow.wait_condition(cancellation: nil) { done? } Kernel.raise failure if failure? # steep:ignore result #: untyped end |
#wait_no_raise ⇒ Object?
Wait on the future to complete. This will return the success or nil if it failed, this will not raise.
150 151 152 153 |
# File 'lib/temporalio/workflow/future.rb', line 150 def wait_no_raise Workflow.wait_condition(cancellation: nil) { done? } result end |