Class: ApplicationInsights::Channel::QueueBase

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

Overview

The base class for all types of queues for use in conjunction with an implementation of SenderBase. The queue will notify the sender that it needs to pick up items when it reaches #max_queue_length, or when the consumer calls #flush.

Direct Known Subclasses

AsynchronousQueue, SynchronousQueue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender) ⇒ QueueBase

Initializes a new instance of the class.

Parameters:

  • sender (SenderBase)

    the sender object that will be used in conjunction with this queue.



13
14
15
16
17
# File 'lib/application_insights/channel/queue_base.rb', line 13

def initialize(sender)
  @queue = Queue.new
  @max_queue_length = 500
  self.sender = sender
end

Instance Attribute Details

#max_queue_lengthFixnum

The maximum number of items that will be held by the queue before the queue will call the #flush method.

Returns:

  • (Fixnum)

    the maximum queue size. (defaults to: 500)



22
23
24
# File 'lib/application_insights/channel/queue_base.rb', line 22

def max_queue_length
  @max_queue_length
end

#senderSenderBase

The sender that is associated with this queue that this queue will use to send data to the service.

Returns:



27
28
29
# File 'lib/application_insights/channel/queue_base.rb', line 27

def sender
  @sender
end

Instance Method Details

#empty?Boolean

Indicates whether the queue is empty.

Returns:

  • (Boolean)

    true if the queue is empty



68
69
70
# File 'lib/application_insights/channel/queue_base.rb', line 68

def empty?
  @queue.empty?
end

#flushObject

Flushes the current queue by notifying the #sender. This method needs to be overridden by a concrete implementations of the queue class.



63
64
# File 'lib/application_insights/channel/queue_base.rb', line 63

def flush
end

#popContracts::Envelope

Pops a single item from the queue and returns it. If the queue is empty, this method will return nil.

Returns:



55
56
57
58
59
# File 'lib/application_insights/channel/queue_base.rb', line 55

def pop
  return @queue.pop(true)
rescue ThreadError
  return nil
end

#push(item) ⇒ Object

Adds the passed in item object to the queue and calls #flush if the size of the queue is larger than #max_queue_length. This method does nothing if the passed in item is nil.

Parameters:



43
44
45
46
47
48
49
# File 'lib/application_insights/channel/queue_base.rb', line 43

def push(item)
  return unless item

  @queue.push(item)

  flush if @queue.length >= @max_queue_length
end