Module: Capistrano::Configuration::Execution

Included in:
Capistrano::Configuration
Defined in:
lib/capistrano/configuration/execution.rb

Defined Under Namespace

Classes: TaskCallFrame

Class Method Summary collapse

Instance Method Summary collapse

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_taskObject

Returns the TaskDefinition object for the currently executing task. It returns nil if there is no task being executed.


79
80
81
82
# File 'lib/capistrano/configuration/execution.rb', line 79

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.


86
87
88
89
90
91
92
# File 'lib/capistrano/configuration/execution.rb', line 86

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.


97
98
99
100
101
102
103
104
105
# File 'lib/capistrano/configuration/execution.rb', line 97

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.


69
70
71
72
73
74
75
# File 'lib/capistrano/configuration/execution.rb', line 69

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

#rollback_requestsObject

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.


35
36
37
# File 'lib/capistrano/configuration/execution.rb', line 35

def rollback_requests
  Thread.current[:rollback_requests]
end

#rollback_requests=(rollback_requests) ⇒ Object


39
40
41
# File 'lib/capistrano/configuration/execution.rb', line 39

def rollback_requests=(rollback_requests)
  Thread.current[:rollback_requests] = rollback_requests
end

#task_call_framesObject

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.


27
28
29
# File 'lib/capistrano/configuration/execution.rb', line 27

def task_call_frames
  Thread.current[:task_call_frames] ||= []
end

#transactionObject

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.

Raises:

  • (ArgumentError)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano/configuration/execution.rb', line 47

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 => e
    rollback!
    raise
  ensure
    self.rollback_requests = nil if Thread.main == Thread.current
  end
end

#transaction?Boolean

Returns true if there is a transaction currently active.

Returns:

  • (Boolean)

20
21
22
# File 'lib/capistrano/configuration/execution.rb', line 20

def transaction?
  !rollback_requests.nil?
end