Class: Temporal::Workflow::Future
- Inherits:
-
Object
- Object
- Temporal::Workflow::Future
- Defined in:
- lib/temporal/workflow/future.rb
Instance Attribute Summary collapse
-
#failure_callbacks ⇒ Object
readonly
Returns the value of attribute failure_callbacks.
-
#success_callbacks ⇒ Object
readonly
Returns the value of attribute success_callbacks.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #cancel ⇒ Object
-
#done(&block) ⇒ Object
When the activity completes successfully, the block will be called with any result.
- #fail(exception) ⇒ Object
-
#failed(&block) ⇒ Object
When the activity fails, the block will be called with the exception.
- #failed? ⇒ Boolean
- #finished? ⇒ Boolean
- #get ⇒ Object
-
#initialize(target, context, cancelation_id: nil) ⇒ Future
constructor
A new instance of Future.
- #ready? ⇒ Boolean
- #set(result) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(target, context, cancelation_id: nil) ⇒ Future
Returns a new instance of Future.
8 9 10 11 12 13 14 15 16 17 18 |
# 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 |
Instance Attribute Details
#failure_callbacks ⇒ Object (readonly)
Returns the value of attribute failure_callbacks.
6 7 8 |
# File 'lib/temporal/workflow/future.rb', line 6 def failure_callbacks @failure_callbacks end |
#success_callbacks ⇒ Object (readonly)
Returns the value of attribute success_callbacks.
6 7 8 |
# File 'lib/temporal/workflow/future.rb', line 6 def success_callbacks @success_callbacks end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/temporal/workflow/future.rb', line 6 def target @target end |
Instance Method Details
#cancel ⇒ Object
78 79 80 81 82 |
# File 'lib/temporal/workflow/future.rb', line 78 def cancel return false if finished? context.cancel(target, cancelation_id) end |
#done(&block) ⇒ Object
When the activity completes successfully, the block will be called with any result
57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
49 50 51 52 53 54 |
# 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) ⇒ Object
When the activity fails, the block will be called with the exception
68 69 70 71 72 73 74 75 76 |
# 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? ⇒ Boolean
28 29 30 |
# File 'lib/temporal/workflow/future.rb', line 28 def failed? @failed end |
#finished? ⇒ Boolean
20 21 22 |
# File 'lib/temporal/workflow/future.rb', line 20 def finished? ready? || failed? end |
#get ⇒ Object
37 38 39 40 |
# File 'lib/temporal/workflow/future.rb', line 37 def get wait exception || result end |
#ready? ⇒ Boolean
24 25 26 |
# File 'lib/temporal/workflow/future.rb', line 24 def ready? @ready end |
#set(result) ⇒ Object
42 43 44 45 46 47 |
# 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 ⇒ Object
32 33 34 35 |
# File 'lib/temporal/workflow/future.rb', line 32 def wait return if finished? context.wait_for(self) end |