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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

  git_branch, git_sha = git_info

  webmock_needs_re_enabling = false
  if defined?(WebMock) && WebMock.respond_to?(:net_connect_allowed?) &&
     !WebMock.net_connect_allowed?(Infield.infield_api_url)
    webmock_needs_re_enabling = true
    allowed = WebMock::Config.instance.allow
    WebMock.disable_net_connect!(allow: Infield.infield_api_url)
  end

  if defined?(Rails) && Rails.respond_to?(:root)
    rails_root = Rails.root.to_s
  end

  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,
                  root_dir: rails_root,
                  git_sha: git_sha,
                  git_branch: git_branch,
                  messages: messages
                }
              }.to_json,
              { 'Content-Type' => 'application/json', 'Authorization' => "bearer #{Infield.api_key}" })
  end
rescue *HTTP_ERRORS => e
ensure
  if webmock_needs_re_enabling
    WebMock.disable_net_connect!(allow: allowed)
  end
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