Class: RigidWorkflow::Activity

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

Overview

Base class for all activities. Activities represent a single, idempotent unit of work within a workflow.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step) ⇒ 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.



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

def initialize(step)
  @workflow_run_id = step.rigid_workflow_run_id
  @step_name = step.step_name
  @step = step
end

Class Method Details

.force_async(value) ⇒ Object

Forces all instances of this activity to run asynchronously.

Parameters:

  • value (Boolean)


11
12
13
14
# File 'lib/rigid_workflow/activity.rb', line 11

def self.force_async(value)
  self.configs = self.configs.merge(force_async: value)
  self
end

.force_async?Boolean

Returns Whether this activity is forced to run asynchronously.

Returns:

  • (Boolean)

    Whether this activity is forced to run asynchronously



17
18
19
# File 'lib/rigid_workflow/activity.rb', line 17

def self.force_async?
  self.configs[:force_async] == true
end

Instance Method Details

#compensateObject

Called during compensation to undo this activity’s side effects. Override in subclasses. Default does nothing.



51
52
# File 'lib/rigid_workflow/activity.rb', line 51

def compensate
end

#log(message, severity: Logger::INFO) ⇒ Object

Logs a message associated with this activity execution.

Parameters:

  • message (String)


38
39
40
41
42
43
44
45
46
# File 'lib/rigid_workflow/activity.rb', line 38

def log(message, severity: Logger::INFO)
  return if !RigidWorkflow.config.logging? && Rails.env.test?

  puts message unless Rails.env.test?
  Rails.logger.log(
    severity,
    "[#{@workflow_run_id.to_s[0..12]}][#{@step_name}] #{message}"
  )
end

#perform(**args) ⇒ Object

The main activity logic. Must be implemented by subclasses.

Parameters:

  • args (Hash)

    Keyword arguments passed from the workflow step input

Returns:

  • (Object)

    The result of the activity, which will be persisted

Raises:

  • (NotImplementedError)


31
32
33
34
# File 'lib/rigid_workflow/activity.rb', line 31

def perform(**args)
  return if Rails.env.test? && !RigidWorkflow.config.logging?
  raise NotImplementedError, "Subclass must implement #perform"
end