Class: Temporalio::Activity::Definition

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

Overview

Definition of an activity. Activities are usually classes/instances that extend Temporalio::Activity, but definitions can also be manually created with a proc/block.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, proc: nil, executor: :default, cancel_raise: true) { ... } ⇒ Definition

Manually create activity definition. Most users will use an instance/class of Temporalio::Activity.

Parameters:

  • name (String, Symbol)

    Name of the activity.

  • proc (Proc, nil) (defaults to: nil)

    Proc for the activity, or can give block.

  • executor (Symbol) (defaults to: :default)

    Name of the executor.

  • cancel_raise (Boolean) (defaults to: true)

    Whether to raise in thread/fiber on cancellation.

Yields:

  • Use this block as the activity. Cannot be present with ‘proc`.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/temporalio/activity/definition.rb', line 62

def initialize(name:, proc: nil, executor: :default, cancel_raise: true, &block)
  @name = name
  if proc.nil?
    raise ArgumentError, 'Must give proc or block' unless block_given?

    proc = block
  elsif block_given?
    raise ArgumentError, 'Cannot give proc and block'
  end
  @proc = proc
  @executor = executor
  @cancel_raise = cancel_raise
end

Instance Attribute Details

#cancel_raiseBoolean (readonly)

Returns Whether to raise in thread/fiber on cancellation. Default is ‘true`.

Returns:

  • (Boolean)

    Whether to raise in thread/fiber on cancellation. Default is ‘true`.



18
19
20
# File 'lib/temporalio/activity/definition.rb', line 18

def cancel_raise
  @cancel_raise
end

#executorSymbol (readonly)

Returns Name of the executor. Default is ‘:default`.

Returns:

  • (Symbol)

    Name of the executor. Default is ‘:default`.



15
16
17
# File 'lib/temporalio/activity/definition.rb', line 15

def executor
  @executor
end

#nameString, Symbol (readonly)

Returns Name of the activity.

Returns:

  • (String, Symbol)

    Name of the activity.



9
10
11
# File 'lib/temporalio/activity/definition.rb', line 9

def name
  @name
end

#procProc (readonly)

Returns Proc for the activity.

Returns:

  • (Proc)

    Proc for the activity.



12
13
14
# File 'lib/temporalio/activity/definition.rb', line 12

def proc
  @proc
end

Class Method Details

.from_activity(activity) ⇒ Object

Obtain a definition representing the given activity, which can be a class, instance, or definition.

Parameters:

Returns:

  • Definition Obtained definition.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/temporalio/activity/definition.rb', line 24

def self.from_activity(activity)
  # Class means create each time, instance means just call, definition
  # does nothing special
  case activity
  when Class
    raise ArgumentError, "Class '#{activity}' does not extend Activity" unless activity < Activity

    details = activity._activity_definition_details
    new(
      name: details[:activity_name],
      executor: details[:activity_executor],
      cancel_raise: details[:activity_cancel_raise],
      # Instantiate and call
      proc: proc { |*args| activity.new.execute(*args) }
    )
  when Activity
    details = activity.class._activity_definition_details
    new(
      name: details[:activity_name],
      executor: details[:activity_executor],
      cancel_raise: details[:activity_cancel_raise],
      # Just call
      proc: proc { |*args| activity.execute(*args) }
    )
  when Activity::Definition
    activity
  else
    raise ArgumentError, "#{activity} is not an activity class, instance, or definition"
  end
end