Class: FogTracker::AccountTracker
- Inherits:
-
Object
- Object
- FogTracker::AccountTracker
- Defined in:
- lib/fog_tracker/account_tracker.rb
Overview
Tracks all collections in a single Fog account
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
A Hash of account-specific configuration data.
-
#delay ⇒ Object
readonly
How long to wait between successive polling of this account (Integer).
-
#log ⇒ Object
readonly
A Logger-compatible object.
-
#name ⇒ Object
readonly
The name (String) of the account this tracker is polling.
-
#preceeding_update_time ⇒ Object
readonly
The time that the second-to-last successful poll finished.
Instance Method Summary collapse
-
#all_resources ⇒ Object
Returns an Array of all this Account’s currently tracked Resources.
-
#connection ⇒ Object
Returns a Fog::Connection object to this account’s Fog service.
-
#initialize(account_name, account, options = {}) ⇒ AccountTracker
constructor
Creates an object for tracking all collections in a single Fog account.
-
#running? ⇒ Boolean
Returns true or false depending on whether this tracker is polling.
-
#start ⇒ Object
Starts a background thread, which periodically polls for all the resource collections for this tracker’s account.
-
#stop ⇒ Object
Stops polling this tracker’s account.
-
#tracked_types ⇒ Object
Returns an Array of resource types (Strings) to track.
-
#update ⇒ Object
Polls once for all the resource collections for this tracker’s account.
Constructor Details
#initialize(account_name, account, options = {}) ⇒ AccountTracker
Creates an object for tracking all collections in a single Fog account
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fog_tracker/account_tracker.rb', line 28 def initialize(account_name, account, ={}) @name = account_name @account = account @callback = [:callback] @log = [:logger] || FogTracker.default_logger @delay = [:delay] || account[:delay] || FogTracker::DEFAULT_POLLING_TIME @account[:delay] = @delay @error_proc = [:error_callback] @log.debug "Creating tracker for account #{@name}." create_collection_trackers end |
Instance Attribute Details
#account ⇒ Object (readonly)
A Hash of account-specific configuration data
10 11 12 |
# File 'lib/fog_tracker/account_tracker.rb', line 10 def account @account end |
#delay ⇒ Object (readonly)
How long to wait between successive polling of this account (Integer)
14 15 16 |
# File 'lib/fog_tracker/account_tracker.rb', line 14 def delay @delay end |
#log ⇒ Object (readonly)
A Logger-compatible object
12 13 14 |
# File 'lib/fog_tracker/account_tracker.rb', line 12 def log @log end |
#name ⇒ Object (readonly)
The name (String) of the account this tracker is polling
8 9 10 |
# File 'lib/fog_tracker/account_tracker.rb', line 8 def name @name end |
#preceeding_update_time ⇒ Object (readonly)
The time that the second-to-last successful poll finished
16 17 18 |
# File 'lib/fog_tracker/account_tracker.rb', line 16 def preceeding_update_time @preceeding_update_time end |
Instance Method Details
#all_resources ⇒ Object
Returns an Array of all this Account’s currently tracked Resources
111 112 113 114 115 |
# File 'lib/fog_tracker/account_tracker.rb', line 111 def all_resources (@collection_trackers.collect do |tracker| tracker.collection end).flatten end |
#connection ⇒ Object
Returns a Fog::Connection object to this account’s Fog service
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fog_tracker/account_tracker.rb', line 92 def connection if ::Fog::const_defined? @account[:service] service_mod = ::Fog::const_get @account[:service] provider_class = service_mod.send(:const_get, @account[:provider]) @fog_service ||= provider_class.new(@account[:credentials]) else provider_mod = ::Fog::const_get @account[:provider] service_class = provider_mod.send(:const_get, @account[:service]) @fog_service ||= service_class.new(@account[:credentials]) end end |
#running? ⇒ Boolean
Returns true or false depending on whether this tracker is polling
89 |
# File 'lib/fog_tracker/account_tracker.rb', line 89 def running? ; @timer != nil end |
#start ⇒ Object
Starts a background thread, which periodically polls for all the resource collections for this tracker’s account
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fog_tracker/account_tracker.rb', line 43 def start if not running? @log.debug "Starting tracking for account #{@name}..." @timer = Thread.new do begin while true update ; sleep @delay end rescue Exception => e sleep @delay ; retry end end else @log.info "Already tracking account #{@name}" end end |
#stop ⇒ Object
Stops polling this tracker’s account
61 62 63 64 65 66 67 68 69 |
# File 'lib/fog_tracker/account_tracker.rb', line 61 def stop if running? @log.info "Stopping tracker for #{@name}..." @timer.kill @timer = nil else @log.info "Tracking already stopped for account #{@name}" end end |
#tracked_types ⇒ Object
Returns an Array of resource types (Strings) to track
105 106 107 108 |
# File 'lib/fog_tracker/account_tracker.rb', line 105 def tracked_types types = connection.collections - (account[:exclude_resources] || []) types.map {|type| type.to_s} end |
#update ⇒ Object
Polls once for all the resource collections for this tracker’s account
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fog_tracker/account_tracker.rb', line 72 def update begin @log.info "Polling account #{@name}..." @collection_trackers.each {|tracker| tracker.update} @preceeding_update_time = @most_recent_update @most_recent_update = Time.now @log.info "Polled account #{@name}" @callback.call(all_resources) if @callback rescue Exception => e @log.error "Exception polling account #{name}: #{e.}" e.backtrace.each {|line| @log.debug line} @error_proc.call(e) if @error_proc raise e end end |