diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/saver.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/saver.rb index aa0c327ac..42220099d 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/saver.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/saver.rb @@ -4,7 +4,7 @@ class ManageIQ::Providers::Vmware::InfraManager::Inventory::Saver def initialize @join_limit = 30 @queue = Queue.new - @should_exit = false + @should_exit = Concurrent::AtomicBoolean.new @threaded = ENV["RAILS_ENV"] != "test" @thread = nil end @@ -25,7 +25,8 @@ def stop_thread(wait: true) _log.info("Save inventory thread stopping...") - @should_exit = true + should_exit.make_true + queue.push(nil) # Force the blocking queue.pop call to return join_thread if wait end @@ -48,14 +49,11 @@ def queue_save_inventory(persister) attr_reader :join_limit, :queue, :should_exit, :thread, :threaded def saver_thread - loop do - while (persister = dequeue) - save_inventory(persister) - end + until should_exit.true? + persister = queue.pop + next if persister.nil? - break if should_exit - - sleep(5) + save_inventory(persister) end rescue => err _log.warn(err) @@ -77,11 +75,6 @@ def ensure_saver_thread start_thread end - def dequeue - queue.deq(:non_block => true) - rescue ThreadError - end - def save_inventory(persister) persister.persist! update_ems_refresh_stats(persister.manager)