diff --git a/lib/vmpooler/pool_manager.rb b/lib/vmpooler/pool_manager.rb index 4bb95da..9f4e7d7 100644 --- a/lib/vmpooler/pool_manager.rb +++ b/lib/vmpooler/pool_manager.rb @@ -631,8 +631,10 @@ module Vmpooler move_vm_queue(pool['name'], next_vm, 'ready', 'completed') end if total > ready - $redis.smembers("vmpooler__pending__#{pool['name']}").each do |vm| + max_to_remove = total - pool['size'] + $redis.smembers("vmpooler__pending__#{pool['name']}").each_with_index do |vm, i| move_vm_queue(pool['name'], vm, 'pending', 'completed') + if max_to_remove == (i+1) then break end end end end diff --git a/spec/unit/pool_manager_spec.rb b/spec/unit/pool_manager_spec.rb index a93207a..0d5a2ed 100644 --- a/spec/unit/pool_manager_spec.rb +++ b/spec/unit/pool_manager_spec.rb @@ -1669,7 +1669,18 @@ EOT subject.remove_excess_vms(config[:pools][0], provider, 2, 5) end + + it 'should remove excess pending vms, but only the excess' do + create_pending_vm(pool,'vm1') + create_pending_vm(pool,'vm2') + create_pending_vm(pool,'vm3') + create_pending_vm(pool,'vm4') + expect(subject).to receive(:move_vm_queue).exactly(3).times + + subject.remove_excess_vms(config[:pools][0], provider, 1, 5) + end end + end describe 'prepare_template' do