-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/models/manageiq/providers/vmware/infra_manager/inventory/saver.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
class ManageIQ::Providers::Vmware::InfraManager::Inventory::Saver | ||
include Vmdb::Logging | ||
|
||
def initialize(threaded: true) | ||
@join_limit = 30 | ||
@queue = Queue.new | ||
@should_exit = false | ||
@threaded = threaded | ||
@thread = nil | ||
end | ||
|
||
def start_thread | ||
return unless threaded | ||
|
||
@thread = Thread.new do | ||
saver_thread | ||
_log.info("Save inventory thread exiting") | ||
end | ||
|
||
_log.info("Save inventory thread started") | ||
end | ||
|
||
def stop_thread(wait: true) | ||
return unless threaded | ||
|
||
_log.info("Save inventory thread stopping...") | ||
|
||
@should_exit = true | ||
join_thread if wait | ||
end | ||
|
||
def queue_save_inventory(persister) | ||
if threaded | ||
ensure_saver_thread | ||
queue.push(persister) | ||
else | ||
save_inventory(persister) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :join_limit, :queue, :should_exit, :thread, :threaded | ||
|
||
def saver_thread | ||
loop do | ||
while (persister = dequeue) | ||
save_inventory(persister) | ||
end | ||
|
||
break if should_exit | ||
|
||
sleep(5) | ||
end | ||
rescue => err | ||
_log.warn(err) | ||
_log.log_backtrace(err) | ||
end | ||
|
||
def join_thread | ||
return unless thread&.alive? | ||
|
||
unless thread.join(join_limit) | ||
thread.kill | ||
end | ||
end | ||
|
||
def ensure_saver_thread | ||
return if thread&.alive? | ||
|
||
_log.warn("Save inventory thread exited, restarting") | ||
start_thread | ||
end | ||
|
||
def dequeue | ||
queue.deq(:non_block => true) | ||
rescue ThreadError | ||
end | ||
|
||
def save_inventory(persister) | ||
ems = persister.manager | ||
inventory = persister.inventory_collections | ||
|
||
ManagerRefresh::SaveInventory.save_inventory(ems, inventory) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters