diff --git a/lib/vmpooler/pool_manager.rb b/lib/vmpooler/pool_manager.rb index 7c8c727..9129652 100644 --- a/lib/vmpooler/pool_manager.rb +++ b/lib/vmpooler/pool_manager.rb @@ -294,6 +294,12 @@ module Vmpooler move_vm_queue(pool, vm, 'running', 'completed', redis, 'is listed as running, but has no checkouttime data. Removing from running') end + # tag VM if not tagged yet, this ensures the method is only called once + unless redis.hget("vmpooler__vm__#{vm}", 'user_tagged') + success = provider.tag_vm_user(pool, vm) + redis.hset("vmpooler__vm__#{vm}", 'user_tagged', 'true') if success + end + throw :stop_checking if provider.vm_ready?(pool, vm) throw :stop_checking if provider.get_vm(pool, vm) diff --git a/lib/vmpooler/providers/base.rb b/lib/vmpooler/providers/base.rb index 635dfc7..4892ccd 100644 --- a/lib/vmpooler/providers/base.rb +++ b/lib/vmpooler/providers/base.rb @@ -212,6 +212,22 @@ module Vmpooler raise("#{self.class.name} does not implement vm_ready?") end + # tag_vm_user This method is called once we know who is using the VM (it is running). This method enables seeing + # who is using what in the provider pools. + # This method should be implemented in the providers, if it is not implemented, this base method will be called + # and should be a noop. The implementation should check if the vm has a user (as per redis) and add a new tag + # with the information. + # inputs + # [String] pool_name : Name of the pool + # [String] vm_name : Name of the VM to check if ready + # returns + # [Boolean] : true if successful, false if an error occurred and it should retry + def tag_vm_user(_pool_name, _vm_name) + # noop by design. If the provider does not implement this method, this base method is called (because inherited) + # and should basically do nothing. + true + end + # inputs # [String] pool_name : Name of the pool # [String] vm_name : Name of the VM to check if it exists diff --git a/spec/unit/pool_manager_spec.rb b/spec/unit/pool_manager_spec.rb index 8bbdb56..3da1f7b 100644 --- a/spec/unit/pool_manager_spec.rb +++ b/spec/unit/pool_manager_spec.rb @@ -683,6 +683,25 @@ EOT expect(redis.sismember("vmpooler__completed__#{pool}", vm)).to be(true) end end + + it 'should try to tag if it has not been done' do + redis_connection_pool.with do |redis| + expect(provider).to receive(:vm_ready?).and_return(true) + redis.hset("vmpooler__active__#{pool}", vm,(Time.now - timeout*60*60).to_s) + expect(provider).to receive(:tag_vm_user) + subject._check_running_vm(vm, pool, 0, provider) + end + end + + it 'should not try to tag if it has been done' do + redis_connection_pool.with do |redis| + expect(provider).to receive(:vm_ready?).and_return(true) + redis.hset("vmpooler__active__#{pool}", vm,(Time.now - timeout*60*60).to_s) + redis.hset("vmpooler__vm__#{vm}", 'user_tagged', 'true') + expect(provider).not_to receive(:tag_vm_user) + subject._check_running_vm(vm, pool, 0, provider) + end + end end context 'with a locked vm mutex' do