Class: FogTracker::AccountTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/fog_tracker/account_tracker.rb

Overview

Tracks all collections in a single Fog account

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_name, account, options = {}) ⇒ AccountTracker

Creates an object for tracking all collections in a single Fog account

Parameters:

  • account_name (String)

    a human-readable name for the account

  • account (Hash)

    a Hash of account configuration data

  • options (Hash) (defaults to: {})

    optional additional parameters:

    • :delay (Integer) - Default time between polling of accounts

    • :callback (Proc) - A Method or Proc to call each time an account is polled. (should take an Array of resources as its only required parameter)

    • :error_callback (Proc) - A Method or Proc to call if polling errors occur. (should take a single Exception as its only required parameter)

    • :logger - a Ruby Logger-compatible object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog_tracker/account_tracker.rb', line 28

def initialize(, , options={})
  @name     = 
  @account  = 
  @callback = options[:callback]
  @log      = options[:logger] || FogTracker.default_logger
  @delay    = options[:delay]  || [:delay] ||
                          FogTracker::DEFAULT_POLLING_TIME
  @account[:delay] = @delay
  @error_proc = options[:error_callback]
  @log.debug "Creating tracker for account #{@name}."
  create_collection_trackers
end

Instance Attribute Details

#accountObject (readonly)

A Hash of account-specific configuration data



10
11
12
# File 'lib/fog_tracker/account_tracker.rb', line 10

def 
  @account
end

#delayObject (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

#logObject (readonly)

A Logger-compatible object



12
13
14
# File 'lib/fog_tracker/account_tracker.rb', line 12

def log
  @log
end

#nameObject (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_timeObject (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_resourcesObject

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

#connectionObject

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

Returns:

  • (Boolean)


89
# File 'lib/fog_tracker/account_tracker.rb', line 89

def running? ; @timer != nil end

#startObject

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

#stopObject

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_typesObject

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 - ([:exclude_resources] || [])
  types.map {|type| type.to_s}
end

#updateObject

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.message}"
    e.backtrace.each {|line| @log.debug line}
    @error_proc.call(e) if @error_proc
    raise e
  end
end