Method: Capistrano::Configuration::Actions::Invocation#parallel

Defined in:
lib/capistrano/configuration/actions/invocation.rb

#parallel(options = {}) ⇒ Object

Executes different commands in parallel. This is useful for commands that need to be different on different hosts, but which could be otherwise run in parallel.

The options parameter is currently unused.

Example:

task :restart_everything do
  parallel do |session|
    session.when "in?(:app)", "/path/to/restart/mongrel"
    session.when "in?(:web)", "/path/to/restart/apache"
    session.when "in?(:db)", "/path/to/restart/mysql"
  end
end

Each command may have its own callback block, for capturing and responding to output, with semantics identical to #run:

session.when "in?(:app)", "/path/to/restart/mongrel" do |ch, stream, data|
  # ch is the SSH channel for this command, used to send data
  #    back to the command (e.g. ch.send_data("password\n"))
  # stream is either :out or :err, for which stream the data arrived on
  # data is a string containing data sent from the remote command
end

Also, you can specify a fallback command, to use when none of the conditions match a server:

session.else "/execute/something/else"

The string specified as the first argument to when may be any valid Ruby code. It has access to the following variables and methods:

  • in?(role) returns true if the server participates in the given role

  • server is the ServerDefinition object for the server. This can be used to get the host-name, etc.

  • configuration is the current Capistrano::Configuration object, which you can use to get the value of variables, etc.

For example:

session.when "server.host =~ /app/", "/some/command"
session.when "server.host == configuration[:some_var]", "/another/command"
session.when "in?(:web) || in?(:app)", "/more/commands"

See #run for a description of the valid options.

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/capistrano/configuration/actions/invocation.rb', line 76

def parallel(options={})
  raise ArgumentError, "parallel() requires a block" unless block_given?
  tree = Command::Tree.new(self) { |t| yield t }
  run_tree(tree, options)
end