Module: Capistrano::Configuration::Execution
- Included in:
- Capistrano::Configuration
- Defined in:
- lib/capistrano/configuration/execution.rb
Defined Under Namespace
Classes: TaskCallFrame
Instance Attribute Summary collapse
-
#rollback_requests ⇒ Object
readonly
The stack of tasks that have registered rollback handlers within the current transaction.
-
#task_call_frames ⇒ Object
readonly
The call stack of the tasks.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
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.
-
#on_rollback(&block) ⇒ Object
Specifies an on_rollback hook for the currently executing task.
-
#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 (readonly)
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.
19 20 21 |
# File 'lib/capistrano/configuration/execution.rb', line 19 def rollback_requests @rollback_requests end |
#task_call_frames ⇒ Object (readonly)
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.
14 15 16 |
# File 'lib/capistrano/configuration/execution.rb', line 14 def task_call_frames @task_call_frames end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
6 7 8 9 |
# File 'lib/capistrano/configuration/execution.rb', line 6 def self.included(base) #:nodoc: base.send :alias_method, :initialize_without_execution, :initialize base.send :alias_method, :initialize, :initialize_with_execution 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.
71 72 73 74 |
# File 'lib/capistrano/configuration/execution.rb', line 71 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.
78 79 80 81 82 83 84 |
# File 'lib/capistrano/configuration/execution.rb', line 78 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 Capistrano::NoSuchTaskError will be raised.
89 90 91 92 93 94 95 96 97 |
# File 'lib/capistrano/configuration/execution.rb', line 89 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 |
#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.
61 62 63 64 65 66 67 |
# File 'lib/capistrano/configuration/execution.rb', line 61 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 |
#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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/capistrano/configuration/execution.rb', line 39 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 @rollback_requests = [] yield logger.info "transaction: commit" rescue Object => e rollback! raise ensure @rollback_requests = nil end end |
#transaction? ⇒ Boolean
Returns true if there is a transaction currently active.
31 32 33 |
# File 'lib/capistrano/configuration/execution.rb', line 31 def transaction? !rollback_requests.nil? end |