diff --git a/src/oca/ruby/opennebula/pool.rb b/src/oca/ruby/opennebula/pool.rb index 92d1490409..5cf5b28ac7 100644 --- a/src/oca/ruby/opennebula/pool.rb +++ b/src/oca/ruby/opennebula/pool.rb @@ -261,27 +261,52 @@ module OpenNebula return rc end - # Iterates over pool pages - # size:: nil => default page size - # > 0 => page size - # state state of objects - # delete true to take always the first page - def each_page(size, state = -1, extended = false, delete = false) + # Iterates over pool page + def loop_page(size, state, extended) current = 0 element = @pool_name.split('_')[0] page = OpenNebula::XMLElement.new loop do - page.initialize_xml(get_page(size, current, extended, state), + page.initialize_xml(get_page(size, + current, + extended, + state), @pool_name) break if page["//#{element}"].nil? + current += yield(element, page) + end + end + + # Iterates over pool pages + # size:: nil => default page size + # > 0 => page size + # state state of objects + def each_page(size, state = -1, extended = false) + loop_page(size, state, extended) do |element, page| page.each("//#{element}") do |obj| yield(obj) end - current += size unless delete + size + end + end + + # Iterates over pool pages to delete them + # size:: nil => default page size + # > 0 => page size + # state state of objects + def each_page_delete(size, state = -1, extended = false) + loop_page(size, state, extended) do |element, page| + no_deleted = 0 + + page.each("//#{element}") do |obj| + no_deleted += 1 if !yield(obj) + end + + no_deleted end end diff --git a/src/onedb/onedb_live.rb b/src/onedb/onedb_live.rb index 2d650e1c22..d39eb83c6e 100644 --- a/src/onedb/onedb_live.rb +++ b/src/onedb/onedb_live.rb @@ -217,15 +217,18 @@ class OneDBLive end_time = ops[:end_time].to_i done = OpenNebula::VirtualMachine::VM_STATE.index('DONE') - vmpool.each_page(ops[:pages], done, false, true) do |obj| - print "VM with ID: #{obj['ID']} purged \r" - + vmpool.each_page_delete(ops[:pages], done, false) do |obj| time = obj['ETIME'].to_i - next unless time >= start_time && time < end_time + # return false because the VM wasn't deleted + next false unless time >= start_time && time < end_time + + print "VM with ID: #{obj['ID']} purged \r" delete('vm_pool', "oid = #{obj['ID']}", false) delete('history', "vid = #{obj['ID']}", false) + + true end end