Class: Justlogging::Agent
- Inherits:
-
Object
- Object
- Justlogging::Agent
- Defined in:
- lib/justlogging/agent.rb
Constant Summary collapse
- ACTION =
'log_entries'
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#sleep_time ⇒ Object
readonly
Returns the value of attribute sleep_time.
-
#transmitter ⇒ Object
readonly
Returns the value of attribute transmitter.
Instance Method Summary collapse
- #add_to_queue(transaction) ⇒ Object
-
#handle_result(code) ⇒ Object
Empty queue is result is 200 ok.
-
#initialize ⇒ Agent
constructor
A new instance of Agent.
- #send_queue ⇒ Object
Constructor Details
#initialize ⇒ Agent
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
#active ⇒ Object (readonly)
Returns the value of attribute active.
4 5 6 |
# File 'lib/justlogging/agent.rb', line 4 def active @active end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
4 5 6 |
# File 'lib/justlogging/agent.rb', line 4 def queue @queue end |
#sleep_time ⇒ Object (readonly)
Returns the value of attribute sleep_time.
4 5 6 |
# File 'lib/justlogging/agent.rb', line 4 def sleep_time @sleep_time end |
#transmitter ⇒ Object (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_queue ⇒ Object
30 31 32 |
# File 'lib/justlogging/agent.rb', line 30 def send_queue handle_result transmitter.transmit(:log_entries => queue) end |