Class: Temporalio::Activity Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/activity.rb,
lib/temporalio/activity/info.rb,
lib/temporalio/activity/context.rb

Overview

This class is abstract.

This is an abstract superclass for implementing activities.

Temporal SDK users are expected to subclass it and implement the #execute method by adding their desired business logic.

Examples:

“Hello World” Activity

class HelloWorld < Temporalio::Activity
  def execute(name)
    "Hello, #{name}!"
  end
end

Defined Under Namespace

Classes: Context, Info

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Activity

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Activity.



64
65
66
# File 'lib/temporalio/activity.rb', line 64

def initialize(context)
  @context = context
end

Class Method Details

._nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/temporalio/activity.rb', line 54

def self._name
  @activity_name || name || ''
end

._shieldedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
# File 'lib/temporalio/activity.rb', line 59

def self._shielded
  @shielded || false
end

.activity_name(new_name) ⇒ Object

Specify a custom name to be used for this activity.

By default a full class (with any namespace modules/classes) name will be used.

Examples:

class Test < Temporalio::Activity
  activity_name 'custom-activity-name'

  def execute
    ...
  end
end

Parameters:

  • new_name (String)

    Name to be used for this activity



30
31
32
# File 'lib/temporalio/activity.rb', line 30

def self.activity_name(new_name)
  @activity_name = new_name
end

.shielded!Object

Mark the activity as shielded from cancellations.

Activity cancellations are implemented using the ‘Thread#raise`, which can unsafely terminate your implementation. To disable this behaviour make sure to mark critical activities as `shielded!`. For shielding a part of your activity consider using Temporalio::Activity::Context#shield.

Examples:

class Test < Temporalio::Activity
  shielded!

  def execute
    ...
  end
end


49
50
51
# File 'lib/temporalio/activity.rb', line 49

def self.shielded!
  @shielded = true
end

Instance Method Details

#execute(*_args) ⇒ Object

This is the main body of your activity’s implementation.

When implementing this method, you can use as many positional arguments as needed, which will get converted based on the activity invocation in your workflow.

Inside of this method you have access to activity’s context using the ‘activity` method. Check out Context for more information on how to use it.

Raises:

  • (NoMethodError)


75
76
77
# File 'lib/temporalio/activity.rb', line 75

def execute(*_args)
  raise NoMethodError, 'must implement #execute'
end