Class: Temporalio::Activity::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/activity/definition.rb

Overview

Base class for all activities.

Activities can be given to a worker as instances of this class, which will call execute on the same instance for each execution, or given to the worker as the class itself which instantiates the activity for each execution.

All activities must implement #execute. Inside execute, Context.current can be used to access the current context to get information, issue heartbeats, etc.

By default, the activity is named as its unqualified class name. This can be customized with Definition.activity_name.

By default, the activity uses the ‘:default` executor which is usually the thread-pool based executor. This can be customized with Definition.activity_executor.

By default, upon cancellation Thread.raise or Fiber.raise is called with a Error::CanceledError. This can be disabled by passing ‘false` to Definition.activity_cancel_raise.

See documentation for more detail on activities.

Defined Under Namespace

Classes: Info

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activity_cancel_raise(cancel_raise) ⇒ Object (protected)

Override whether the activity uses Thread/Fiber raise for cancellation which is defaulted to true.

Parameters:

  • cancel_raise (Boolean)

    Whether to raise.



52
53
54
55
56
57
58
# File 'lib/temporalio/activity/definition.rb', line 52

def activity_cancel_raise(cancel_raise)
  unless cancel_raise.is_a?(TrueClass) || cancel_raise.is_a?(FalseClass)
    raise ArgumentError, 'Must be a boolean'
  end

  @activity_cancel_raise = cancel_raise
end

.activity_dynamic(value = true) ⇒ Object (protected)

Set an activity as dynamic. Dynamic activities do not have names and handle any activity that is not otherwise registered. A worker can only have one dynamic activity. It is often useful to use activity_raw_args with this.

Parameters:

  • value (Boolean) (defaults to: true)

    Whether the activity is dynamic.

Raises:

  • (ArgumentError)


65
66
67
68
69
# File 'lib/temporalio/activity/definition.rb', line 65

def activity_dynamic(value = true) # rubocop:disable Style/OptionalBooleanParameter
  raise ArgumentError, 'Must be a boolean' unless value.is_a?(TrueClass) || value.is_a?(FalseClass)

  @activity_dynamic = value
end

.activity_executor(executor_name) ⇒ Object (protected)

Override the activity executor which is defaulted to ‘:default`.

Parameters:

  • executor_name (Symbol)

    Executor to use.

Raises:

  • (ArgumentError)


43
44
45
46
47
# File 'lib/temporalio/activity/definition.rb', line 43

def activity_executor(executor_name)
  raise ArgumentError, 'Executor name must be a symbol' unless executor_name.is_a?(Symbol)

  @activity_executor = executor_name
end

.activity_name(name) ⇒ Object (protected)

Override the activity name which is defaulted to the unqualified class name.

Parameters:

  • name (String, Symbol)

    Name to use.



31
32
33
34
35
36
37
38
# File 'lib/temporalio/activity/definition.rb', line 31

def activity_name(name)
  if !name.is_a?(Symbol) && !name.is_a?(String)
    raise ArgumentError,
          'Activity name must be a symbol or string'
  end

  @activity_name = name.to_s
end

.activity_raw_args(value = true) ⇒ Object (protected)

Have activity arguments delivered to ‘execute` 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 Context#payload_converter.

Parameters:

  • value (Boolean) (defaults to: true)

    Whether the activity accepts raw arguments.

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/temporalio/activity/definition.rb', line 76

def activity_raw_args(value = true) # rubocop:disable Style/OptionalBooleanParameter
  raise ArgumentError, 'Must be a boolean' unless value.is_a?(TrueClass) || value.is_a?(FalseClass)

  @activity_raw_args = value
end

Instance Method Details

#execute(*args) ⇒ Object

Implementation of the activity. The arguments should be positional and this should return the value on success or raise an error on failure.

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/temporalio/activity/definition.rb', line 100

def execute(*args)
  raise NotImplementedError, 'Activity did not implement "execute"'
end