Class: CMDx::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/executor.rb

Overview

Executes CMDx tasks with middleware support, error handling, and lifecycle management.

The Executor class is responsible for orchestrating task execution, including pre-execution validation, execution with middleware, post-execution callbacks, and proper error handling for different types of failures.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ CMDx::Executor

Returns A new executor instance.

Examples:

executor = CMDx::Executor.new(my_task)

Parameters:



29
30
31
# File 'lib/cmdx/executor.rb', line 29

def initialize(task)
  @task = task
end

Instance Attribute Details

#taskTask (readonly)

Returns the task being executed.

Examples:

executor.task.id # => "abc123"

Returns:

  • (Task)

    The task instance



19
20
21
# File 'lib/cmdx/executor.rb', line 19

def task
  @task
end

Class Method Details

.execute(task, raise: false) ⇒ CMDx::Result

Executes a task with optional exception raising.

Examples:

CMDx::Executor.execute(my_task)
CMDx::Executor.execute(my_task, raise: true)

Parameters:

  • task (CMDx::Task)

    The task to execute

  • raise (Boolean) (defaults to: false)

    Whether to raise exceptions (default: false)

Returns:

Raises:

  • (StandardError)

    When raise is true and execution fails



47
48
49
50
# File 'lib/cmdx/executor.rb', line 47

def self.execute(task, raise: false)
  instance = new(task)
  raise ? instance.execute! : instance.execute
end

Instance Method Details

#executeCMDx::Result

Executes the task with graceful error handling.

Examples:

executor = CMDx::Executor.new(my_task)
result = executor.execute

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cmdx/executor.rb', line 61

def execute
  task.class.settings[:middlewares].call!(task) do
    pre_execution! unless @pre_execution
    execution!
  rescue UndefinedMethodError => e
    raise(e) # No need to clear the Chain since exception is not being re-raised
  rescue Fault => e
    task.result.throw!(e.result, halt: false, cause: e)
  rescue StandardError => e
    retry if retry_execution?(e)
    task.result.fail!("[#{e.class}] #{e.message}", halt: false, cause: e)
    task.class.settings[:exception_handler]&.call(task, e)
  ensure
    task.result.executed!
    post_execution!
  end

  finalize_execution!
end

#execute!CMDx::Result

Executes the task with exception raising on failure.

Examples:

executor = CMDx::Executor.new(my_task)
result = executor.execute!

Returns:

Raises:

  • (StandardError)

    When execution fails



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cmdx/executor.rb', line 92

def execute!
  task.class.settings[:middlewares].call!(task) do
    pre_execution! unless @pre_execution
    execution!
  rescue UndefinedMethodError => e
    raise_exception(e)
  rescue Fault => e
    task.result.throw!(e.result, halt: false, cause: e)
    halt_execution?(e) ? raise_exception(e) : post_execution!
  rescue StandardError => e
    retry if retry_execution?(e)
    task.result.fail!("[#{e.class}] #{e.message}", halt: false, cause: e)
    raise_exception(e)
  else
    task.result.executed!
    post_execution!
  end

  finalize_execution!
end