Class: Shikibu::Activity

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

Overview

Standalone activity definition Use this for reusable activities that can be shared across workflows

Examples:

ProcessPayment = Shikibu::Activity.new(:process_payment) do |ctx, order_id:, amount:|
  PaymentService.charge(order_id, amount)
end

# In a workflow:
result = ProcessPayment.call(ctx, order_id: '123', amount: 99.99)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, retry_policy: nil, &block) ⇒ Activity

Create a new activity

Parameters:

  • name (Symbol, String)

    Activity name

  • retry_policy (RetryPolicy) (defaults to: nil)

    Retry policy

  • block (Proc)

    Activity logic (receives context and kwargs)



22
23
24
25
26
# File 'lib/shikibu/activity.rb', line 22

def initialize(name, retry_policy: nil, &block)
  @name = name.to_s
  @retry_policy = retry_policy || RetryPolicy.default
  @block = block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/shikibu/activity.rb', line 16

def name
  @name
end

#retry_policyObject (readonly)

Returns the value of attribute retry_policy.



16
17
18
# File 'lib/shikibu/activity.rb', line 16

def retry_policy
  @retry_policy
end

Instance Method Details

#call(ctx, **kwargs) ⇒ Object

Execute the activity within a workflow context

Parameters:

  • ctx (WorkflowContext)

    Workflow context

  • kwargs (Hash)

    Activity arguments

Returns:

  • (Object)

    Activity result



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shikibu/activity.rb', line 32

def call(ctx, **kwargs)
  activity_id = ctx.generate_activity_id(@name)
  ctx.current_activity_id = activity_id

  # Check for cached result during replay
  if ctx.replaying? && ctx.cached_result?(activity_id)
    cached = ctx.get_cached_result(activity_id)
    return cached[:result] if cached[:event_type] == EventType::ACTIVITY_COMPLETED

    # Re-raise cached error
    raise reconstruct_error(cached)
  end

  # Execute with retry
  execute_with_retry(ctx, activity_id, kwargs)
ensure
  ctx.current_activity_id = nil
end