core: BatchAction can run arbitrary code

This commit is contained in:
Mitchell Hashimoto 2014-04-15 17:33:43 -07:00
parent da0db72054
commit 2add94ee28
2 changed files with 22 additions and 1 deletions

View File

@ -24,6 +24,13 @@ module Vagrant
@actions << [machine, action, options]
end
# Custom runs a custom proc against a machine.
#
# @param [Machine] machine The machine to run against.
def custom(machine, &block)
@actions << [machine, block, nil]
end
# Run all the queued up actions, parallelizing if possible.
#
# This will parallelize if and only if the provider of every machine
@ -69,7 +76,11 @@ module Vagrant
start_pid = Process.pid
begin
machine.send(:action, action, options)
if action.is_a?(Proc)
action.call(machine)
else
machine.send(:action, action, options)
end
rescue Exception => e
# If we're not parallelizing, then raise the error. We also
# don't raise the error if we've forked, because it'll hang

View File

@ -34,6 +34,16 @@ describe Vagrant::BatchAction do
expect(called_actions.include?([machine2, "destroy", nil])).to be
end
it "should run the arbitrary methods in order" do
called = []
subject.custom(machine) { |m| called << m }
subject.custom(machine2) { |m| called << m }
subject.run
expect(called[0]).to equal(machine)
expect(called[1]).to equal(machine2)
end
it "should handle forks gracefully", :skip_windows do
# Doesn't need to be tested on Windows since Windows doesn't
# support fork(1)