onering-client 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,7 +43,14 @@ EOS
43
43
  end
44
44
 
45
45
  def self.run(args)
46
- report = _report()
46
+ # saving, by default, should not use the cache (but should update it to keep it fresh)
47
+ if @opts[:save] === true or args[0] == 'save'
48
+ report = _report({
49
+ :cacheregen => true
50
+ })
51
+ else
52
+ report = _report()
53
+ end
47
54
 
48
55
  # pull overrides from CLI arguments
49
56
  @opts[:fields].each do |field|
@@ -98,10 +105,10 @@ EOS
98
105
  end
99
106
  end
100
107
 
101
- def self._report()
108
+ def self._report(options={})
102
109
  begin
103
110
  Onering::Logger.debug("Gathering local data for report", "Onering::CLI::Report")
104
- report = Onering::Reporter.report().stringify_keys()
111
+ report = Onering::Reporter.report(options).stringify_keys()
105
112
 
106
113
  # pull report overrides from the config file
107
114
  Onering::Config.get('reporter.fields',{}).each do |key, value|
@@ -142,14 +142,15 @@ module Onering
142
142
  end
143
143
  end
144
144
 
145
- def report()
145
+ def report(options={})
146
+ options = @options.merge(options)
146
147
  @id = (@options[:id] || Onering::Util.fact('hardwareid', nil))
147
148
 
148
149
  if not @id.nil?
149
- if @options[:nocache]
150
+ if options[:nocache]
150
151
  return _generated_report()
151
152
  else
152
- rv = _cached_report()
153
+ rv = _cached_report(options)
153
154
  return _generated_report() if rv.nil? or rv.empty?
154
155
  return rv
155
156
  end
@@ -184,20 +185,35 @@ module Onering
184
185
  return {}
185
186
  end
186
187
 
187
- def _cached_report()
188
- cachefile = (@options[:cachefile] || DEFAULT_CACHE_FILE)
188
+ def _cached_report(options={})
189
+ options = @options.merge(options)
190
+ cachefile = (options[:cachefile] || DEFAULT_CACHE_FILE)
191
+ tries = 0
189
192
 
190
193
  catch(:retry) do
194
+ tries += 1
195
+
196
+ if tries > 10
197
+ Onering::Logger.error("Too many retries reading cache #{cachefile}, generating report", "Onering::Reporter")
198
+ return _generated_report()
199
+ end
200
+
191
201
  if File.readable?(cachefile)
192
202
  Onering::Logger.debug("Loading cache file at #{cachefile}", "Onering::Reporter")
193
203
  cache = File.read(cachefile)
194
204
  cache = (MultiJson.load(cache) rescue {})
195
205
 
196
- if _cache_expired?(cache, @options[:maxage])
197
- Onering::Logger.debug("Cache expired, regenerating...", "Onering::Reporter")
206
+ if _cache_expired?(cache, options[:maxage])
207
+ Onering::Logger.debug("Cache expired, regenerating", "Onering::Reporter")
198
208
  throw :retry if _update_cache_file(cachefile)
199
209
  end
200
210
 
211
+ if options[:cacheregen] == true
212
+ Onering::Logger.debug("Forcing cache regeneration", "Onering::Reporter")
213
+ _update_cache_file(cachefile)
214
+ return _generated_report()
215
+ end
216
+
201
217
  # remove cached_at key
202
218
  Onering::Logger.debug("Using cached data (#{Time.now.to_i - Time.parse(cache.get('cached_at')).to_i} seconds old)", "Onering::Reporter")
203
219
  cache.delete('cached_at')
data/lib/onering.rb CHANGED
@@ -2,7 +2,7 @@ $: << File.expand_path(File.dirname(__FILE__))
2
2
 
3
3
  module Onering
4
4
  module Client
5
- VERSION = "0.1.4"
5
+ VERSION = "0.1.5"
6
6
 
7
7
  class Error < Exception; end
8
8
  class FatalError < Error; end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: onering-client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.4
5
+ version: 0.1.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gary Hetzel