Class: Justlogging::Agent

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

Constant Summary collapse

ACTION =
'log_entries'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/justlogging/agent.rb', line 7

def initialize
  @sleep_time = 5
  @queue = []
  @active = true
  @thread = Thread.new do
    while true do
      if (@queue.any? && @active == true)
        send_queue
      end
      sleep @sleep_time
    end
  end
  @transmitter = Transmitter.new(
    Justlogging.config[:endpoint],
    ACTION,
    Justlogging.config[:api_key]
  )
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



4
5
6
# File 'lib/justlogging/agent.rb', line 4

def active
  @active
end

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/justlogging/agent.rb', line 4

def queue
  @queue
end

#sleep_timeObject (readonly)

Returns the value of attribute sleep_time.



4
5
6
# File 'lib/justlogging/agent.rb', line 4

def sleep_time
  @sleep_time
end

#transmitterObject (readonly)

Returns the value of attribute transmitter.



4
5
6
# File 'lib/justlogging/agent.rb', line 4

def transmitter
  @transmitter
end

Instance Method Details

#add_to_queue(transaction) ⇒ Object



26
27
28
# File 'lib/justlogging/agent.rb', line 26

def add_to_queue(transaction)
  @queue << transaction
end

#handle_result(code) ⇒ Object

Empty queue is result is 200 ok. Throttle connection when result is 403 Unsubscribe and stop sending all together on any other code.



37
38
39
40
41
42
43
44
45
46
# File 'lib/justlogging/agent.rb', line 37

def handle_result(code)
  if code == '200'
    @queue = []
  elsif code == '403'
    @sleep_time = @sleep_time * 1.5
  else
    ActiveSupport::Notifications.unsubscribe(Justlogging.subscriber)
    @active = false
  end
end

#send_queueObject



30
31
32
# File 'lib/justlogging/agent.rb', line 30

def send_queue
  handle_result transmitter.transmit(:log_entries => queue)
end