Skip not applicable tasks on each host #3

Closed
wants to merge 1 commit into
from
@@ -107,10 +107,6 @@ def enhance(deps=nil, &block)
# RemoteTask.
def execute(args = nil)
- raise(Rake::ConfigurationError,
- "No target hosts specified on task #{self.name} for roles #{options[:roles].inspect}") unless
- defined_target_hosts?
-
super args
@remote_actions.each { |act| act.execute(target_hosts, self, args) }
@@ -565,21 +561,6 @@ def target_hosts
end
##
- # Similar to target_hosts, but returns true if user defined any hosts, even
- # an empty list.
-
- def defined_target_hosts?
- return true if ENV["HOSTS"]
- roles = Array options[:roles]
- return true if roles.empty?
- # borrowed from hosts_for:
- roles.flatten.each { |r|
- return true unless @@def_role_hash.eql? Rake::RemoteTask.roles[r]
- }
- return false
- end
-
- ##
# Action is used to run a task's remote_actions in parallel on each
# of its hosts. Actions are created automatically in
# Rake::RemoteTask#enhance.
@@ -621,6 +602,11 @@ def == other # :nodoc:
def execute hosts, task, args
hosts.each do |host|
+ unless task.applies_to? host
+ puts "Skipped task #{task} for host #{host}" if $TRACE
+ next
+ end
+
t = task.clone
t.target_host = host
thread = Thread.new(t) do |task2|
@@ -638,6 +624,10 @@ def execute hosts, task, args
@workers.list.each { |thr| thr.join }
end
end
+
+ def applies_to?(host)
+ options[:roles].empty? || Rake::RemoteTask.hosts_for(options[:roles]).include?(host)
+ end
end
Rake::RemoteTask.set_defaults
@@ -67,16 +67,22 @@ def test_execute_exposes_target_host
def test_execute_with_no_hosts
@rake.host "app.example.com", :app
t = @rake.remote_task(:flunk, :roles => :db) { flunk "should not have run" }
- e = assert_raises(Rake::ConfigurationError) { t.execute nil }
- assert_equal "No target hosts specified on task flunk for roles [:db]",
- e.message
+ t.execute nil
end
def test_execute_with_no_roles
t = @rake.remote_task(:flunk, :roles => :junk) { flunk "should not have run" }
- e = assert_raises(Rake::ConfigurationError) { t.execute nil }
- assert_equal "No target hosts specified on task flunk for roles [:junk]",
- e.message
+ t.execute nil
+ end
+
+ def test_execute_with_incompatible_host_and_task_doesnt_run_task
+ host 'app@example.com', :app
+ set :some_variable, 1
+ x = 5
+ task = @rake.remote_task(:some_task, :roles => :not_app) { x += some_variable }
+ task.execute nil
+ assert_equal 1, task.some_variable
+ assert_equal 5, x
end
def test_execute_with_roles