Class: Autoscale::Agent::WebDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscale/agent/web_dispatcher.rb

Constant Summary collapse

DISPATCH_INTERVAL =
15
TTL =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ WebDispatcher

Returns a new instance of WebDispatcher.



9
10
11
12
13
14
15
16
# File 'lib/autoscale/agent/web_dispatcher.rb', line 9

def initialize(token)
  @id = token.each_char.take(7).join
  @token = token
  @buffer = {}
  @mutex = Mutex.new
  @running = false
  @running_lock = Mutex.new
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/autoscale/agent/web_dispatcher.rb', line 7

def token
  @token
end

Instance Method Details

#add(value, timestamp: Time.now.to_i) ⇒ Object



18
19
20
21
22
# File 'lib/autoscale/agent/web_dispatcher.rb', line 18

def add(value, timestamp: Time.now.to_i)
  @mutex.synchronize do
    add_unsafe(value, timestamp: timestamp)
  end
end

#dispatchObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/autoscale/agent/web_dispatcher.rb', line 24

def dispatch
  payload = flush

  return if payload.empty?

  body = MultiJson.dump(payload)
  response = Request.dispatch(body, token: token)

  unless response.is_a?(Net::HTTPOK)
    revert(payload)
    error "Failed to dispatch (#{response.code}) #{response.body}"
  end
rescue => err
  puts "Autoscale::Agent/WebDispatcher: #{err}\n#{err.backtrace.join("\n")}"
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/autoscale/agent/web_dispatcher.rb', line 40

def run
  @running_lock.synchronize do
    return if @running
    @running = true
  end

  Thread.new do
    loop do
      dispatch
      sleep DISPATCH_INTERVAL
    end
  end
end