Class: Weatherman::Report

Inherits:
Object
  • Object
show all
Includes:
AWS
Defined in:
lib/weatherman/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AWS

aws_access_key_id, aws_access_key_id=, aws_secret_access_key, aws_secret_access_key=, #cloud_watch, ec2_attributes, instance_id, region, region=

Constructor Details

#initialize(name, options = {}, &collector) ⇒ Report

Returns a new instance of Report.



14
15
16
17
18
19
20
21
22
# File 'lib/weatherman/report.rb', line 14

def initialize(name, options = {}, &collector)
  @name       = name
  @collector  = collector
  @namespace  = options[:namespace] || 'Custom/Weatherman'
  @period     = options[:period] || 12
  @dimensions = options[:dimensions] || {}

  @dimensions.merge! 'InstanceId' => AWS.instance_id
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/weatherman/report.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/weatherman/report.rb', line 6

def options
  @options
end

Class Method Details

.run(name, options = {}, &collector) ⇒ Object



8
9
10
11
12
# File 'lib/weatherman/report.rb', line 8

def self.run(name, options = {}, &collector)
  report = new(name, options, &collector)
  report.run
  report
end

Instance Method Details

#reportObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/weatherman/report.rb', line 42

def report
  value = @collector.call

  cloud_watch.put_metric_data @namespace, [{
    'MetricName' => @name,
    'Value'      => value,
    'Dimensions' => @dimensions.map { |k, v| { k.to_s => v } }
  }]

  value
end

#runObject



24
25
26
27
28
29
30
31
# File 'lib/weatherman/report.rb', line 24

def run
  @thread ||= Thread.new do
    while true
      report
      sleep @period
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/weatherman/report.rb', line 38

def running?
  !@thread.nil?
end

#stopObject



33
34
35
36
# File 'lib/weatherman/report.rb', line 33

def stop
  @thread.kill if @thread
  @thread = nil
end