class Temporal::Workflow::Future

Attributes

cancelation_id[R]
context[R]
exception[R]
failure_callbacks[R]
result[R]
success_callbacks[R]
target[R]

Public Class Methods

new(target, context, cancelation_id: nil) click to toggle source
# File lib/temporal/workflow/future.rb, line 8
def initialize(target, context, cancelation_id: nil)
  @target = target
  @context = context
  @cancelation_id = cancelation_id
  @success_callbacks = []
  @failure_callbacks = []
  @ready = false
  @failed = false
  @result = nil
  @exception = nil
end

Public Instance Methods

cancel() click to toggle source
# File lib/temporal/workflow/future.rb, line 78
def cancel
  return false if finished?

  context.cancel(target, cancelation_id)
end
done(&block) click to toggle source

When the activity completes successfully, the block will be called with any result

# File lib/temporal/workflow/future.rb, line 57
def done(&block)
  if ready?
    block.call(result)
  else
    # If the future is still outstanding, schedule a callback for invocation by the
    # workflow context when the workflow or activity is finished
    success_callbacks << block
  end
end
fail(exception) click to toggle source
# File lib/temporal/workflow/future.rb, line 49
def fail(exception)
  raise 'can not fail a fulfilled future' if ready?

  @exception = exception
  @failed = true
end
failed(&block) click to toggle source

When the activity fails, the block will be called with the exception

# File lib/temporal/workflow/future.rb, line 68
def failed(&block)
  if failed?
    block.call(exception)
  else
    # If the future is still outstanding, schedule a callback for invocation by the
    # workflow context when the workflow or activity is finished
    failure_callbacks << block
  end
end
failed?() click to toggle source
# File lib/temporal/workflow/future.rb, line 28
def failed?
  @failed
end
finished?() click to toggle source
# File lib/temporal/workflow/future.rb, line 20
def finished?
  ready? || failed?
end
get() click to toggle source
# File lib/temporal/workflow/future.rb, line 37
def get
  wait
  exception || result
end
ready?() click to toggle source
# File lib/temporal/workflow/future.rb, line 24
def ready?
  @ready
end
set(result) click to toggle source
# File lib/temporal/workflow/future.rb, line 42
def set(result)
  raise 'can not fulfil a failed future' if failed?

  @result = result
  @ready = true
end
wait() click to toggle source
# File lib/temporal/workflow/future.rb, line 32
def wait
  return if finished?
  context.wait_for(self)
end