Class: Ci::RegisterJobService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Ci::Artifacts::Logger
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

Methods included from Gitlab::Ci::Artifacts::Logger

#log_artifacts_context, #log_artifacts_filesize, #log_build_dependencies, log_created, log_deleted

Constructor Details

#initialize(runner, runner_manager) ⇒ RegisterJobService

Returns a new instance of RegisterJobService.



21
22
23
24
25
# File 'app/services/ci/register_job_service.rb', line 21

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

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



9
10
11
# File 'app/services/ci/register_job_service.rb', line 9

def metrics
  @metrics
end

#runnerObject (readonly)

Returns the value of attribute runner.



9
10
11
# File 'app/services/ci/register_job_service.rb', line 9

def runner
  @runner
end

#runner_managerObject (readonly)

Returns the value of attribute runner_manager.



9
10
11
# File 'app/services/ci/register_job_service.rb', line 9

def runner_manager
  @runner_manager
end

Instance Method Details

#execute(params = {}) ⇒ Object



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

def execute(params = {})
  replica_caught_up =
    ::Ci::Runner.sticking.find_caught_up_replica(:runner, runner.id, use_primary_on_failure: false)

  @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 !replica_caught_up && !result.build
    metrics.increment_queue_operation(:queue_replication_lag)

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