-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add save inventory thread for streaming refresh #233
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
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 | ||
|
||
# This method will re-start the saver thread if it has crashed or terminated | ||
# prematurely, but is only safe to be called from a single thread. Given | ||
# wait_for_updates has to be single threaded this should be fine but if you | ||
# intend to queue up save_inventory from multiple calling threads a mutex | ||
# must be added around ensure_saver_thread | ||
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) | ||
persister.persist! | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably some log here would be nice? we can do it in next PR