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.
41 42 43 |
# File 'lib/cmdx/executor.rb', line 41 def initialize(task) @task = task end |
Instance Attribute Details
#task ⇒ Task (readonly)
Returns the task being executed.
29 30 31 |
# File 'lib/cmdx/executor.rb', line 29 def task @task end |
Class Method Details
.execute(task, raise: false) ⇒ CMDx::Result
Executes a task with optional exception raising.
59 60 61 62 |
# File 'lib/cmdx/executor.rb', line 59 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cmdx/executor.rb', line 73 def execute task.class.settings.middlewares.call!(task) do pre_execution! unless @pre_execution execution! verify_context_returns! rescue UndefinedMethodError => e raise_exception(e) rescue Fault => e result.throw!(e.result, halt: false, cause: e) rescue StandardError => e retry if retry_execution?(e) result.fail!(Utils::Normalize.exception(e), halt: false, cause: e, source: :exception) task.class.settings.exception_handler&.call(task, e) ensure result.executed! post_execution! end verify_middleware_yield! finalize_execution! end |
#execute! ⇒ CMDx::Result
Executes the task with exception raising on failure.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cmdx/executor.rb', line 106 def execute! task.class.settings.middlewares.call!(task) do pre_execution! unless @pre_execution execution! verify_context_returns! rescue UndefinedMethodError => e raise_exception(e) rescue Fault => e result.throw!(e.result, halt: false, cause: e) if halt_execution?(e) raise_exception(e) else result.executed! post_execution! end rescue StandardError => e retry if retry_execution?(e) result.fail!(Utils::Normalize.exception(e), halt: false, cause: e, source: :exception) raise_exception(e) else result.executed! post_execution! end verify_middleware_yield! finalize_execution! end |