Class: Temporalio::Workflow::Future

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#failureException?



70
71
72
# File 'lib/temporalio/workflow/future.rb', line 70

def failure
  @failure
end

#resultObject?



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 future’s value or raise the first completed future’s 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



91
92
93
# File 'lib/temporalio/workflow/future.rb', line 91

def done?
  @done
end

#failure?Boolean



113
114
115
# File 'lib/temporalio/workflow/future.rb', line 113

def failure?
  done? && !failure.nil?
end

#result?Boolean



96
97
98
# File 'lib/temporalio/workflow/future.rb', line 96

def result?
  done? && !failure
end

#waitObject

Wait on the future to complete. This will return the success or raise the failure. To not raise, use #wait_no_raise.

Raises:

  • (Exception)

    Failure if occurred.



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_raiseObject?

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