Skip to content

Commit

Permalink
Set the ems last_refresh error/time attributes
Browse files Browse the repository at this point in the history
After a refresh finishes set the last_refresh_error and
last_refresh_time attributes on the ems.
  • Loading branch information
agrare committed Jul 10, 2018
1 parent 1895408 commit 3b9118b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ def initialize(ems, run_once: false, threaded: true)
end

def run
_log.info("Monitor updates thread started")
_log.info("#{log_header} Monitor updates thread started")

saver.start_thread

vim = connect
property_filter = create_property_filter(vim)

_log.info("Refreshing initial inventory")
_log.info("#{log_header} Refreshing initial inventory")
version = initial_refresh(vim, property_filter)
_log.info("Refreshing initial inventory...Complete")
_log.info("#{log_header} Refreshing initial inventory...Complete")

self.initial = false

Expand All @@ -38,15 +38,20 @@ def run
version = monitor_updates(vim, property_filter, version, persister, parser)
end

_log.info("Monitor updates thread exited")
_log.info("#{log_header} Monitor updates thread exited")
rescue => err
_log.error("#{log_header} Refresh failed")
_log.log_backtrace(err)

ems.update_attributes(:last_refresh_error => err.to_s, :last_refresh_time => Time.now.utc)
ensure
saver.stop_thread
destroy_property_filter(property_filter)
disconnect(vim)
end

def stop
_log.info("Monitor updates thread exiting...")
_log.info("#{log_header} Monitor updates thread exiting...")
self.exit_requested = true
end

Expand Down Expand Up @@ -84,7 +89,7 @@ def connect
host = ems.hostname
username, password = ems.auth_user_pwd

_log.info("Connecting to #{username}@#{host}...")
_log.info("#{log_header} Connecting to #{username}@#{host}...")

vim_opts = {
:ns => 'urn:vim25',
Expand All @@ -102,7 +107,7 @@ def connect
vim.serviceContent.sessionManager.Login(:userName => username, :password => password)
end

_log.info("Connected")
_log.info("#{log_header} Connected")
conn
end

Expand Down Expand Up @@ -134,11 +139,11 @@ def process_update_set(property_filter, update_set)
object_update_set = property_filter_update.objectSet
return if object_update_set.blank?

_log.info("Processing #{object_update_set.count} updates...")
_log.info("#{log_header} Processing #{object_update_set.count} updates...")

updates = process_object_update_set(object_update_set)

_log.info("Processing #{object_update_set.count} updates...Complete")
_log.info("#{log_header} Processing #{object_update_set.count} updates...Complete")

updates
end
Expand Down Expand Up @@ -223,6 +228,10 @@ def save_inventory(persister)
saver.queue_save_inventory(persister)
end

def log_header
"EMS: [#{ems.name}], id: [#{ems.id}]"
end

def log_object_update(object_update)
return if initial

Expand All @@ -232,7 +241,7 @@ def log_object_update(object_update)
prop_changes = object_update.changeSet.map(&:name).take(5).join(", ")
prop_changes << ", ..." if object_update.changeSet.length > 5

s = "Object: [#{object_str}] Kind: [#{object_update.kind}]"
s = "#{log_header} Object: [#{object_str}] Kind: [#{object_update.kind}]"
s << " Props: [#{prop_changes}]" if object_update.kind == "modify"
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,12 @@ def dequeue

def save_inventory(persister)
persister.persist!
update_ems_refresh_stats(persister.manager)
rescue => err
update_ems_refresh_stats(persister.manager, :error => err.to_s)
end

def update_ems_refresh_stats(ems, error: nil)
ems.update_attributes(:last_refresh_error => error, :last_refresh_date => Time.now.utc)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def run_full_refresh

def assert_ems
expect(ems.api_version).to eq("5.5")
expect(ems.last_refresh_error).to be_nil
expect(ems.last_refresh_date).not_to be_nil
expect(ems.uid_ems).to eq("D6EB1D64-05B2-4937-BFF6-6F77C6E647B7")
expect(ems.ems_clusters.count).to eq(4)
expect(ems.ems_folders.count).to eq(11)
Expand Down

0 comments on commit 3b9118b

Please sign in to comment.