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?

Returns Failure if this future failed or nil if it didn’t or hasn’t yet completed.

Returns:

  • (Exception, nil)

    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

#resultObject?

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.

Returns:

  • (Object, nil)

    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.

Parameters:

  • futures (Array<Future<Object>>)

    Futures to wait for all to complete (or first to fail).

Returns:

  • (Future<nil>)

    Future that completes successfully with nil when all futures complete, or raises on first future failure.



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.

Parameters:

  • futures (Array<Future<Object>>)

    Futures to wait for the first to complete.

Returns:

  • (Future<Object>)

    Future that relays the first completed future’s result/failure.



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.

Parameters:

  • futures (Array<Future<Object>>)

    Futures to wait for all to complete (regardless of success/fail).

Returns:

  • (Future<nil>)

    Future that completes successfully with nil when all futures 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.

Parameters:

  • futures (Array<Future<Object>>)

    Futures to wait for the first to complete.

Returns:

  • (Future<Future<Object>>)

    Future with the first completing future regardless of success/fail.



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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

#waitObject

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

Returns:

  • (Object)

    Result on success.

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.

Returns:

  • (Object, nil)

    Result on success or nil on failure.



145
146
147
148
# File 'lib/temporalio/workflow/future.rb', line 145

def wait_no_raise
  Workflow.wait_condition(cancellation: nil) { done? }
  result
end