Class: Temporalio::Workflow::Definition
- Inherits:
-
Object
- Object
- Temporalio::Workflow::Definition
- Defined in:
- lib/temporalio/workflow/definition.rb
Overview
Base class for all workflows.
Workflows are instances of this class and must implement #execute. Inside the workflow code, class methods on Temporalio::Workflow can be used.
By default, the workflow is named as its unqualified class name. This can be customized with Definition.workflow_name.
Defined Under Namespace
Classes: Info, Query, Signal, Update
Class Method Summary collapse
-
.workflow_dynamic(value = true) ⇒ Object
protected
Set a workflow as dynamic.
-
.workflow_failure_exception_type(*types) ⇒ Object
protected
Configure workflow failure exception types.
-
.workflow_init(value = true) ⇒ Object
protected
Mark an ‘initialize` as needing the workflow start arguments.
-
.workflow_name(workflow_name) ⇒ Object
protected
Customize the workflow name.
-
.workflow_query(name: nil, description: nil, dynamic: false, raw_args: false) ⇒ Object
protected
Mark the next method as a workflow query with a default name as the name of the method.
-
.workflow_query_attr_reader(*attr_names, description: nil) ⇒ Object
protected
Expose an attribute as a method and as a query.
-
.workflow_raw_args(value = true) ⇒ Object
protected
Have workflow arguments delivered to ‘execute` (and `initialize` if Definition.workflow_init in use) as Converters::RawValues.
-
.workflow_signal(name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON) ⇒ Object
protected
Mark the next method as a workflow signal with a default name as the name of the method.
-
.workflow_update(name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON) ⇒ Object
protected
Mark the next method as a workflow update with a default name as the name of the method.
-
.workflow_update_validator(update_method) ⇒ Object
protected
Mark the next method as a workflow update validator to the given update method.
Instance Method Summary collapse
-
#execute(*args) ⇒ Object
Execute the workflow.
Class Method Details
.workflow_dynamic(value = true) ⇒ Object (protected)
Set a workflow as dynamic. Dynamic workflows do not have names and handle any workflow that is not otherwise registered. A worker can only have one dynamic workflow. It is often useful to use workflow_raw_args with this.
37 38 39 |
# File 'lib/temporalio/workflow/definition.rb', line 37 def workflow_dynamic(value = true) # rubocop:disable Style/OptionalBooleanParameter @workflow_dynamic = value end |
.workflow_failure_exception_type(*types) ⇒ Object (protected)
Configure workflow failure exception types. This sets the types of exceptions that, if a workflow-thrown exception extends, will cause the workflow/update to fail instead of suspending the workflow via task failure. These are applied in addition to the worker option. If Exception is set, it effectively will fail a workflow/update in all user exception cases.
56 57 58 59 60 61 62 |
# File 'lib/temporalio/workflow/definition.rb', line 56 def workflow_failure_exception_type(*types) types.each do |t| raise ArgumentError, 'All types must classes inheriting Exception' unless t.is_a?(Class) && t < Exception end @workflow_failure_exception_types ||= [] @workflow_failure_exception_types.concat(types) end |
.workflow_init(value = true) ⇒ Object (protected)
Mark an ‘initialize` as needing the workflow start arguments. Otherwise, `initialize` must accept no required arguments. This must be placed above the `initialize` method or it will fail.
98 99 100 |
# File 'lib/temporalio/workflow/definition.rb', line 98 def workflow_init(value = true) # rubocop:disable Style/OptionalBooleanParameter self.pending_handler_details = { type: :init, value: } end |
.workflow_name(workflow_name) ⇒ Object (protected)
Customize the workflow name. By default the workflow is named the unqualified class name of the class provided to the worker.
23 24 25 26 27 28 29 30 |
# File 'lib/temporalio/workflow/definition.rb', line 23 def workflow_name(workflow_name) if !workflow_name.is_a?(Symbol) && !workflow_name.is_a?(String) raise ArgumentError, 'Workflow name must be a symbol or string' end @workflow_name = workflow_name.to_s end |
.workflow_query(name: nil, description: nil, dynamic: false, raw_args: false) ⇒ Object (protected)
Mark the next method as a workflow query with a default name as the name of the method. Queries can not have any side effects, meaning they should never mutate state or try to wait on anything.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/temporalio/workflow/definition.rb', line 138 def workflow_query( name: nil, description: nil, dynamic: false, raw_args: false ) raise 'Cannot provide name if dynamic is true' if name && dynamic self.pending_handler_details = { type: :query, name:, description:, dynamic:, raw_args: } end |
.workflow_query_attr_reader(*attr_names, description: nil) ⇒ Object (protected)
Expose an attribute as a method and as a query. A ‘workflow_query_attr_reader :foo` is the equivalent of: “` workflow_query def foo
@foo
end “‘ This means it is a superset of `attr_reader“ and will not work if also using `attr_reader` or `attr_accessor`. If a writer is needed alongside this, use `attr_writer`.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/temporalio/workflow/definition.rb', line 77 def workflow_query_attr_reader(*attr_names, description: nil) @workflow_queries ||= {} attr_names.each do |attr_name| raise 'Expected attr to be a symbol' unless attr_name.is_a?(Symbol) if method_defined?(attr_name, false) raise 'Method already defined for this attr name. ' \ 'Note that a workflow_query_attr_reader includes attr_reader behavior. ' \ 'If you also want a writer for this attribute, use a separate attr_writer.' end # Just run this as if done manually workflow_query(description:) define_method(attr_name) { instance_variable_get("@#{attr_name}") } end end |
.workflow_raw_args(value = true) ⇒ Object (protected)
Have workflow arguments delivered to ‘execute` (and `initialize` if workflow_init in use) as Converters::RawValues. These are wrappers for the raw payloads that have not been converted to types (but they have been decoded by the codec if present). They can be converted with Temporalio::Workflow.payload_converter.
46 47 48 |
# File 'lib/temporalio/workflow/definition.rb', line 46 def workflow_raw_args(value = true) # rubocop:disable Style/OptionalBooleanParameter @workflow_raw_args = value end |
.workflow_signal(name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON) ⇒ Object (protected)
Mark the next method as a workflow signal with a default name as the name of the method. Signals cannot return values.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/temporalio/workflow/definition.rb', line 115 def workflow_signal( name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON ) raise 'Cannot provide name if dynamic is true' if name && dynamic self.pending_handler_details = { type: :signal, name:, description:, dynamic:, raw_args:, unfinished_policy: } end |
.workflow_update(name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON) ⇒ Object (protected)
Mark the next method as a workflow update with a default name as the name of the method. Updates can return values. Separate validation methods can be provided via workflow_update_validator.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/temporalio/workflow/definition.rb', line 162 def workflow_update( name: nil, description: nil, dynamic: false, raw_args: false, unfinished_policy: HandlerUnfinishedPolicy::WARN_AND_ABANDON ) raise 'Cannot provide name if dynamic is true' if name && dynamic self.pending_handler_details = { type: :update, name:, description:, dynamic:, raw_args:, unfinished_policy: } end |
.workflow_update_validator(update_method) ⇒ Object (protected)
Mark the next method as a workflow update validator to the given update method. The validator is expected to have the exact same parameter signature. It will run before an update and if it raises an exception, the update will be rejected, possibly before even reaching history. Validators cannot have any side effects or do any waiting, and they do not return values.
180 181 182 |
# File 'lib/temporalio/workflow/definition.rb', line 180 def workflow_update_validator(update_method) self.pending_handler_details = { type: :update_validator, update_method: } end |
Instance Method Details
#execute(*args) ⇒ Object
Execute the workflow. This is the primary workflow method. The workflow is completed when this method completes. This must be implemented by all workflows.
405 406 407 |
# File 'lib/temporalio/workflow/definition.rb', line 405 def execute(*args) raise NotImplementedError, 'Workflow did not implement "execute"' end |