Class: SnowAgent::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/snowagent/sender.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, conf) ⇒ Sender

Returns a new instance of Sender.



3
4
5
6
7
8
9
# File 'lib/snowagent/sender.rb', line 3

def initialize(queue, conf)
  @queue      = queue
  @service    = Service.new(conf)
  @conf       = conf
  @metrics    = []
  @send_at    = Time.now.to_i + @conf.send_interval
end

Instance Method Details

#processObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/snowagent/sender.rb', line 15

def process
  # Pop metrics from the queue
  while !@queue.empty?
    @metrics << @queue.pop
  end

  if @send_at <= Time.now.to_i
    @send_at = Time.now.to_i + @conf.send_interval
    send_data if @metrics.any?
  end

  sleep 1
end

#runObject



11
12
13
# File 'lib/snowagent/sender.rb', line 11

def run
  loop { process }
end

#send_dataObject



29
30
31
32
33
34
35
# File 'lib/snowagent/sender.rb', line 29

def send_data
  # TODO:
  # * requeue data on failure
  @service.post_data({ metrics: @metrics.map(&:to_h) })

  @metrics.clear
end