Class: Ci::RegisterJobService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ci/register_job_service.rb

Overview

This class responsible for assigning proper pending build to runner on runner API request

Defined Under Namespace

Classes: Result

Constant Summary collapse

TEMPORARY_LOCK_TIMEOUT =
3.seconds
MAX_QUEUE_DEPTH =

The queue depth limit number has been determined by observing 95 percentile of effective queue depth on gitlab.com. This is only likely to affect 5% of the worst case scenarios.

45

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ RegisterJobService

Returns a new instance of RegisterJobService.


19
20
21
22
# File 'app/services/ci/register_job_service.rb', line 19

def initialize(runner)
  @runner = runner
  @metrics = ::Gitlab::Ci::Queue::Metrics.new(runner)
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.


7
8
9
# File 'app/services/ci/register_job_service.rb', line 7

def metrics
  @metrics
end

#runnerObject (readonly)

Returns the value of attribute runner.


7
8
9
# File 'app/services/ci/register_job_service.rb', line 7

def runner
  @runner
end

Instance Method Details

#execute(params = {}) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/ci/register_job_service.rb', line 24

def execute(params = {})
  db_all_caught_up =
    ::Ci::Runner.sticking.all_caught_up?(:runner, runner.id)

  @metrics.increment_queue_operation(:queue_attempt)

  result = @metrics.observe_queue_time(:process, @runner.runner_type) do
    process_queue(params)
  end

  # Since we execute this query against replica it might lead to false-positive
  # We might receive the positive response: "hi, we don't have any more builds for you".
  # This might not be true. If our DB replica is not up-to date with when runner event was generated
  # we might still have some CI builds to be picked. Instead we should say to runner:
  # "Hi, we don't have any more builds now,  but not everything is right anyway, so try again".
  # Runner will retry, but again, against replica, and again will check if replication lag did catch-up.
  if !db_all_caught_up && !result.build
    metrics.increment_queue_operation(:queue_replication_lag)

    ::Ci::RegisterJobService::Result.new(nil, false) # rubocop:disable Cop/AvoidReturnFromBlocks
  else
    result
  end
end