Class: CMDx::Executor
- Inherits:
-
Object
- Object
- CMDx::Executor
- Extended by:
- Forwardable
- 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.
33 34 35 |
# File 'lib/cmdx/executor.rb', line 33 def initialize(task) @task = task end |
Instance Attribute Details
#task ⇒ Task (readonly)
Returns the task being executed.
21 22 23 |
# File 'lib/cmdx/executor.rb', line 21 def task @task end |
Class Method Details
.execute(task, raise: false) ⇒ CMDx::Result
Executes a task with optional exception raising.
51 52 53 54 |
# File 'lib/cmdx/executor.rb', line 51 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cmdx/executor.rb', line 65 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 result.throw!(e.result, halt: false, cause: e) rescue StandardError => e retry if retry_execution?(e) result.fail!("[#{e.class}] #{e.}", halt: false, cause: e) task.class.settings[:exception_handler]&.call(task, e) ensure result.executed! post_execution! end finalize_execution! end |
#execute! ⇒ CMDx::Result
Executes the task with exception raising on failure.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cmdx/executor.rb', line 96 def execute! task.class.settings[:middlewares].call!(task) do pre_execution! unless @pre_execution execution! rescue UndefinedMethodError => e raise_exception(e) rescue Fault => e result.throw!(e.result, halt: false, cause: e) halt_execution?(e) ? raise_exception(e) : post_execution! rescue StandardError => e retry if retry_execution?(e) result.fail!("[#{e.class}] #{e.}", halt: false, cause: e) raise_exception(e) else result.executed! post_execution! end finalize_execution! end |