Skip to content

Commit

Permalink
Merge pull request #582 from agrare/improve_save_inventory_thread
Browse files Browse the repository at this point in the history
Improve the save_inventory thread by blocking the dequeue not sleeping
  • Loading branch information
chessbyte committed May 26, 2020
2 parents c24f943 + 3a8dad7 commit 32a5cda
Showing 1 changed file with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 32a5cda

Please sign in to comment.