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

.post_deprecation_warnings(tasks) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/infield/deprecation_warning.rb', line 47

def post_deprecation_warnings(tasks)
  messages = tasks.map { |w| { message: w.message, callstack: w.callstack.map(&:to_s) } }

  uri = infield_api_uri
  Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
    http.post('/api/raw_deprecation_warnings',
              { raw_deprecation_warnings: {
                  repo_environment_id: Infield.repo_environment_id,
                  environment: Infield.environment,
                  messages: messages
                }
              }.to_json,
              { 'Content-Type' => 'application/json', 'Authorization' => "bearer #{Infield.api_key}" })
  end
rescue *HTTP_ERRORS => e
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