Class: CMDx::Executor
- Inherits:
-
Object
- Object
- CMDx::Executor
- 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
-
#task ⇒ Task
readonly
Returns the task being executed.
Class Method Summary collapse
-
.execute(task, raise: false) ⇒ CMDx::Result
Executes a task with optional exception raising.
Instance Method Summary collapse
-
#execute ⇒ CMDx::Result
Executes the task with graceful error handling.
-
#execute! ⇒ CMDx::Result
Executes the task with exception raising on failure.
-
#initialize(task) ⇒ CMDx::Executor
constructor
A new executor instance.
Constructor Details
#initialize(task) ⇒ CMDx::Executor
Returns A new executor instance.
29 30 31 |
# File 'lib/cmdx/executor.rb', line 29 def initialize(task) @task = task end |
Instance Attribute Details
#task ⇒ Task (readonly)
Returns the task being executed.
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.
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
#execute ⇒ CMDx::Result
Executes the task with graceful error handling.
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.}", 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.
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.}", halt: false, cause: e) raise_exception(e) else task.result.executed! post_execution! end finalize_execution! end |