Module: Infield::DeprecationWarning::Runner

Defined in:
lib/infield/deprecation_warning.rb

Overview

Handles spinning up a thread to process work

Constant Summary collapse

HTTP_ERRORS =
[
  Timeout::Error, EOFError, SocketError, Errno::ENETDOWN, Errno::ENETUNREACH,
  Errno::EINVAL, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EAGAIN,
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
  Zlib::BufError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.queueObject (readonly)

Returns the value of attribute queue.



21
22
23
# File 'lib/infield/deprecation_warning.rb', line 21

def queue
  @queue
end

.threadObject (readonly)

Returns the value of attribute thread.



21
22
23
# File 'lib/infield/deprecation_warning.rb', line 21

def thread
  @thread
end

Class Method Details

.enqueue(message) ⇒ Object



23
24
25
26
27
# File 'lib/infield/deprecation_warning.rb', line 23

def enqueue(message)
  @queue ||= Queue.new
  return if @queue.size >= @queue_limit
  @queue << message
end

.run(sleep_interval: 5, batch_size: 10, queue_limit: 30) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/infield/deprecation_warning.rb', line 29

def run(sleep_interval: 5, batch_size: 10, queue_limit: 30)
  @queue ||= Queue.new
  @sleep_interval = sleep_interval
  # Queue cannot be larger than this. If more than this number of messages come in
  # before the next wake interval any extra are dropped
  @queue_limit = queue_limit
  @batch_size = batch_size # send up to 20 messages to API at once

  @thread = Thread.new do
    loop do
      sleep(@sleep_interval)
      next if @queue.empty?

      process_queue
    end
  end
end