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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/temporalio/workflow/future.rb', line 74 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 @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.
70 71 72 |
# File 'lib/temporalio/workflow/future.rb', line 70 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.
67 68 69 |
# File 'lib/temporalio/workflow/future.rb', line 67 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.
32 33 34 35 36 37 38 39 |
# File 'lib/temporalio/workflow/future.rb', line 32 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 futures value or raise the first completed futures exception. To not raise the exception, see try_any_of.
16 17 18 19 20 21 22 |
# File 'lib/temporalio/workflow/future.rb', line 16 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.
58 59 60 61 62 63 |
# File 'lib/temporalio/workflow/future.rb', line 58 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.
46 47 48 49 50 51 |
# File 'lib/temporalio/workflow/future.rb', line 46 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.
91 92 93 |
# File 'lib/temporalio/workflow/future.rb', line 91 def done? @done end |
#failure? ⇒ Boolean
Returns True if done and failed, false if still running or succeeded.
113 114 115 |
# File 'lib/temporalio/workflow/future.rb', line 113 def failure? done? && !failure.nil? end |
#result? ⇒ Boolean
Returns True if done and not a failure, false if still running or failed.
96 97 98 |
# File 'lib/temporalio/workflow/future.rb', line 96 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.
135 136 137 138 139 140 |
# File 'lib/temporalio/workflow/future.rb', line 135 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.
145 146 147 148 |
# File 'lib/temporalio/workflow/future.rb', line 145 def wait_no_raise Workflow.wait_condition(cancellation: nil) { done? } result end |