Class: DynamoAutoscale::CWPoller

Inherits:
Poller
  • Object
show all
Includes:
Logger
Defined in:
lib/dynamo-autoscale/cw_poller.rb

Constant Summary collapse

INTERVAL =
5.minutes

Instance Attribute Summary

Attributes inherited from Poller

#filters, #tables

Instance Method Summary collapse

Methods included from Logger

included, logger, #logger, logger=

Methods inherited from Poller

#dispatch, #initialize, #run

Constructor Details

This class inherits a constructor from DynamoAutoscale::Poller

Instance Method Details

#backdateObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dynamo-autoscale/cw_poller.rb', line 6

def backdate
  now = Time.now.utc

  @tables.each do |table_name|
    table = DynamoAutoscale.tables[table_name]
    dispatch(table, Metrics.all_metrics(table_name, {
      period:     5.minutes,
      start_time: now - 6.hours,
      end_time:   now,
    }))
  end
end

#do_poll(tables, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dynamo-autoscale/cw_poller.rb', line 34

def do_poll tables, &block
  logger.debug "[cw_poller] Beginning CloudWatch poll..."
  now = Time.now

  tables.each do |table_name|
    # This code will dispatch a message to the listening table that looks
    # like this:
    #
    #   {
    #     :consumed_reads=>{
    #       2013-06-19 12:22:00 UTC=>2.343117697349672
    #     },
    #     :consumed_writes=>{
    #       2013-06-19 12:22:00 UTC=>3.0288461538461537
    #     }
    #   }
    #
    # There may also be :provisioned_reads and :provisioned_writes
    # depending on how the CloudWatch API feels.
    block.call(table_name, Metrics.all_metrics(table_name, {
      period:     5.minutes,
      start_time: now - 20.minutes,
      end_time:   now,
    }))
  end
end

#poll(tables, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dynamo-autoscale/cw_poller.rb', line 19

def poll tables, &block
  tables = AWS::DynamoDB.new.tables.to_a.map(&:name) if tables.nil?

  loop do
    # Sleep until the next interval occurrs. This calculation ensures that
    # polling always happens on interval boundaries regardless of how long
    # polling takes.
    sleep_duration = INTERVAL - ((Time.now.to_i + INTERVAL) % INTERVAL)
    logger.debug "[cw_poller] Sleeping for #{sleep_duration} seconds..."
    sleep(sleep_duration)

    do_poll(tables, &block)
  end
end