lib/onering/plugins/reporter.rb in onering-client-0.1.4 vs lib/onering/plugins/reporter.rb in onering-client-0.1.5

- old
+ new

@@ -140,18 +140,19 @@ Onering::Logger.debug3("-> Set metric #{name.to_s}", "Onering::Reporter") @_report[:properties][:metrics].set(name.to_s, value) end end - def report() + def report(options={}) + options = @options.merge(options) @id = (@options[:id] || Onering::Util.fact('hardwareid', nil)) if not @id.nil? - if @options[:nocache] + if options[:nocache] return _generated_report() else - rv = _cached_report() + rv = _cached_report(options) return _generated_report() if rv.nil? or rv.empty? return rv end else Onering::Logger.fatal!("Cannot generate report without a hardware ID", "Onering::Reporter") @@ -182,21 +183,36 @@ end return {} end - def _cached_report() - cachefile = (@options[:cachefile] || DEFAULT_CACHE_FILE) + def _cached_report(options={}) + options = @options.merge(options) + cachefile = (options[:cachefile] || DEFAULT_CACHE_FILE) + tries = 0 catch(:retry) do + tries += 1 + + if tries > 10 + Onering::Logger.error("Too many retries reading cache #{cachefile}, generating report", "Onering::Reporter") + return _generated_report() + end + if File.readable?(cachefile) Onering::Logger.debug("Loading cache file at #{cachefile}", "Onering::Reporter") cache = File.read(cachefile) cache = (MultiJson.load(cache) rescue {}) - if _cache_expired?(cache, @options[:maxage]) - Onering::Logger.debug("Cache expired, regenerating...", "Onering::Reporter") + if _cache_expired?(cache, options[:maxage]) + Onering::Logger.debug("Cache expired, regenerating", "Onering::Reporter") throw :retry if _update_cache_file(cachefile) + end + + if options[:cacheregen] == true + Onering::Logger.debug("Forcing cache regeneration", "Onering::Reporter") + _update_cache_file(cachefile) + return _generated_report() end # remove cached_at key Onering::Logger.debug("Using cached data (#{Time.now.to_i - Time.parse(cache.get('cached_at')).to_i} seconds old)", "Onering::Reporter") cache.delete('cached_at')