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

JOB_QUEUE_DURATION_SECONDS_BUCKETS =
[1, 3, 10, 30, 60, 300, 900, 1800, 3600].freeze
JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET =
5.freeze
METRICS_SHARD_TAG_PREFIX =
'metrics_shard::'.freeze
DEFAULT_METRICS_SHARD =
'default'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ RegisterJobService

Returns a new instance of RegisterJobService.


16
17
18
# File 'app/services/ci/register_job_service.rb', line 16

def initialize(runner)
  @runner = runner
end

Instance Attribute Details

#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

rubocop: disable CodeReuse/ActiveRecord


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/ci/register_job_service.rb', line 21

def execute(params = {})
  builds =
    if runner.instance_type?
      builds_for_shared_runner
    elsif runner.group_type?
      builds_for_group_runner
    else
      builds_for_project_runner
    end

  valid = true

  # pick builds that does not have other tags than runner's one
  builds = builds.matches_tag_ids(runner.tags.ids)

  # pick builds that have at least one tag
  unless runner.run_untagged?
    builds = builds.with_any_tags
  end

  # pick builds that older than specified age
  if params.key?(:job_age)
    builds = builds.queued_before(params[:job_age].seconds.ago)
  end

  builds.each do |build|
    result = process_build(build, params)
    next unless result

    if result.valid?
      register_success(result.build)

      return result
    else
      # The usage of valid: is described in
      # handling of ActiveRecord::StaleObjectError
      valid = false
    end
  end

  register_failure
  Result.new(nil, nil, valid)
end