Module: Minestrone::Configuration::Execution
- Included in:
- Minestrone::Configuration
- Defined in:
- lib/minestrone/configuration/execution.rb
Defined Under Namespace
Classes: TaskCallFrame
Instance Attribute Summary collapse
-
#rollback_requests ⇒ Object
The stack of tasks that have registered rollback handlers within the current transaction.
Instance Method Summary collapse
-
#current_task ⇒ Object
Returns the TaskDefinition object for the currently executing task.
-
#execute_task(task) ⇒ Object
Executes the task with the given name, without invoking any associated callbacks.
-
#find_and_execute_task(path, hooks = {}) ⇒ Object
Attempts to locate the task at the given fully-qualified path, and execute it.
-
#initialize_execution ⇒ Object
:nodoc:.
-
#on_rollback(&block) ⇒ Object
Specifies an on_rollback hook for the currently executing task.
-
#task_call_frames ⇒ Object
The call stack of the tasks.
-
#transaction ⇒ Object
Invoke a set of tasks in a transaction.
-
#transaction? ⇒ Boolean
Returns true if there is a transaction currently active.
Instance Attribute Details
#rollback_requests ⇒ Object
The stack of tasks that have registered rollback handlers within the current transaction. If this is nil, then there is no transaction that is currently active.
31 32 33 |
# File 'lib/minestrone/configuration/execution.rb', line 31 def rollback_requests @rollback_requests end |
Instance Method Details
#current_task ⇒ Object
Returns the TaskDefinition object for the currently executing task. It returns nil if there is no task being executed.
73 74 75 76 |
# File 'lib/minestrone/configuration/execution.rb', line 73 def current_task return nil if task_call_frames.empty? task_call_frames.last.task end |
#execute_task(task) ⇒ Object
Executes the task with the given name, without invoking any associated callbacks.
80 81 82 83 84 85 86 |
# File 'lib/minestrone/configuration/execution.rb', line 80 def execute_task(task) logger.debug "executing `#{task.fully_qualified_name}'" push_task_call_frame(task) invoke_task_directly(task) ensure pop_task_call_frame end |
#find_and_execute_task(path, hooks = {}) ⇒ Object
Attempts to locate the task at the given fully-qualified path, and execute it. If no such task exists, a Minestrone::NoSuchTaskError will be raised.
92 93 94 95 96 97 98 99 100 |
# File 'lib/minestrone/configuration/execution.rb', line 92 def find_and_execute_task(path, hooks = {}) task = find_task(path) or raise NoSuchTaskError, "the task `#{path}' does not exist" trigger(hooks[:before], task) if hooks[:before] result = execute_task(task) trigger(hooks[:after], task) if hooks[:after] result end |
#initialize_execution ⇒ Object
:nodoc:
11 12 13 14 |
# File 'lib/minestrone/configuration/execution.rb', line 11 def initialize_execution #:nodoc: @task_call_frames = [] @rollback_requests = nil end |
#on_rollback(&block) ⇒ Object
Specifies an on_rollback hook for the currently executing task. If this or any subsequent task then fails, and a transaction is active, this hook will be executed.
62 63 64 65 66 67 68 |
# File 'lib/minestrone/configuration/execution.rb', line 62 def on_rollback(&block) if transaction? # don't note a new rollback request if one has already been set rollback_requests << task_call_frames.last unless task_call_frames.last.rollback task_call_frames.last.rollback = block end end |
#task_call_frames ⇒ Object
The call stack of the tasks. The currently executing task may inspect this to see who its caller was. The current task is always the last element of this stack.
24 25 26 |
# File 'lib/minestrone/configuration/execution.rb', line 24 def task_call_frames @task_call_frames end |
#transaction ⇒ Object
Invoke a set of tasks in a transaction. If any task fails (raises an exception), all tasks executed within the transaction are inspected to see if they have an associated on_rollback hook, and if so, that hook is called.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/minestrone/configuration/execution.rb', line 38 def transaction raise ArgumentError, "expected a block" unless block_given? raise ScriptError, "transaction must be called from within a task" if task_call_frames.empty? return yield if transaction? logger.info "transaction: start" begin self.rollback_requests = [] yield logger.info "transaction: commit" rescue Object rollback! raise ensure self.rollback_requests = nil end end |
#transaction? ⇒ Boolean
Returns true if there is a transaction currently active.
17 18 19 |
# File 'lib/minestrone/configuration/execution.rb', line 17 def transaction? !rollback_requests.nil? end |