Class: ApplicationInsights::Channel::AsynchronousSender

Inherits:
SenderBase
  • Object
show all
Defined in:
lib/application_insights/channel/asynchronous_sender.rb

Overview

An asynchronous sender that works in conjunction with the AsynchronousQueue. The sender object will start a worker thread that will pull items from the SenderBase#queue. The thread will be created when the client calls #start and will check for queue items every #send_interval seconds. The worker thread can also be forced to check the queue by setting the ApplicationInsights::Channel::AsynchronousQueue#flush_notification event.

  • If no items are found, the thread will go back to sleep.

  • If items are found, the worker thread will send items to the specified service in batches of SenderBase#send_buffer_size.

If no queue items are found for #send_time seconds, the worker thread will shut down (and #start will need to be called again).

Instance Attribute Summary collapse

Attributes inherited from SenderBase

#queue, #send_buffer_size, #service_endpoint_uri

Instance Method Summary collapse

Methods inherited from SenderBase

#send

Constructor Details

#initialize(service_endpoint_uri = 'https://dc.services.visualstudio.com/v2/track') ⇒ AsynchronousSender

Initializes a new instance of the class.



19
20
21
22
23
24
25
26
27
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 19

def initialize(service_endpoint_uri='https://dc.services.visualstudio.com/v2/track')
  @send_interval = 1.0
  @send_remaining_time = 0
  @send_time = 3.0
  @lock_work_thread = Mutex.new
  @work_thread = nil
  @start_notification_processed = true
  super service_endpoint_uri
end

Instance Attribute Details

#send_intervalFixnum

The time span in seconds at which the the worker thread will check the SenderBase#queue for items (defaults to: 1.0).



31
32
33
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 31

def send_interval
  @send_interval
end

#send_timeFixnum

The time span in seconds for which the worker thread will stay alive if no items are found in the SenderBase#queue (defaults to 3.0).



35
36
37
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 35

def send_time
  @send_time
end

#work_threadThread (readonly)

The worker thread which checks queue items and send data every (#send_interval) seconds or upon flush.



39
40
41
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 39

def work_thread
  @work_thread
end

Instance Method Details

#startObject

Calling this method will create a worker thread that checks the SenderBase#queue every #send_interval seconds for a total duration of #send_time seconds for new items. If a worker thread has already been created, calling this method does nothing.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 44

def start
  @start_notification_processed = false
  # Maintain one working thread at one time

  if !@work_thread
    @lock_work_thread.synchronize do
      if !@work_thread
        local_send_interval = (@send_interval < 0.1) ? 0.1 : @send_interval
        @send_remaining_time = (@send_time < local_send_interval) ? local_send_interval : @send_time
        @work_thread = Thread.new do
          run
        end
        @work_thread.abort_on_exception = false
      end
    end
  end
end