Class: Buildkite::TestCollector::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/buildkite/test_collector/session.rb

Constant Summary collapse

UPLOAD_THREAD_TIMEOUT =
60
UPLOAD_SESSION_TIMEOUT =
60
UPLOAD_API_MAX_RESULTS =
5000

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



9
10
11
12
# File 'lib/buildkite/test_collector/session.rb', line 9

def initialize
  @send_queue_ids = []
  @upload_threads = []
end

Instance Method Details

#add_example_to_send_queue(id) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/buildkite/test_collector/session.rb', line 14

def add_example_to_send_queue(id)
  @send_queue_ids << id

  if @send_queue_ids.size >= Buildkite::TestCollector.batch_size
    send_ids = @send_queue_ids.shift(Buildkite::TestCollector.batch_size)
    upload_data(send_ids)
  end
end

#closeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/buildkite/test_collector/session.rb', line 29

def close
  # There are two thread joins here, because the inner join will wait up to
  # UPLOAD_THREAD_TIMEOUT seconds PER thread that is uploading data, i.e.
  # n_threads x UPLOAD_THREAD_TIMEOUT latency if Buildkite happens to be
  # down. By wrapping that in an outer thread join with the
  # UPLOAD_SESSION_TIMEOUT, we ensure that we only wait a max of
  # UPLOAD_SESSION_TIMEOUT seconds before the session exits.
  Thread.new do
    @upload_threads.each { |t| t.join(UPLOAD_THREAD_TIMEOUT) }
  end.join(UPLOAD_SESSION_TIMEOUT)

  @upload_threads.each { |t| t&.kill }
end

#send_remaining_dataObject



23
24
25
26
27
# File 'lib/buildkite/test_collector/session.rb', line 23

def send_remaining_data
  return if @send_queue_ids.empty?

  upload_data(@send_queue_ids)
end