Class: Ci::UnlockArtifactsService

Inherits:
BaseService show all
Defined in:
app/services/ci/unlock_artifacts_service.rb

Constant Summary collapse

BATCH_SIZE =
100

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#execute(ci_ref, before_pipeline = nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'app/services/ci/unlock_artifacts_service.rb', line 7

def execute(ci_ref, before_pipeline = nil)
  results = {
    unlocked_pipelines: 0,
    unlocked_job_artifacts: 0
  }

  if ::Feature.enabled?(:ci_update_unlocked_job_artifacts, ci_ref.project)
    loop do
      unlocked_pipelines = []
      unlocked_job_artifacts = []

      ::Ci::Pipeline.transaction do
        unlocked_pipelines = unlock_pipelines(ci_ref, before_pipeline)
        unlocked_job_artifacts = unlock_job_artifacts(unlocked_pipelines)
      end

      break if unlocked_pipelines.empty?

      results[:unlocked_pipelines] += unlocked_pipelines.length
      results[:unlocked_job_artifacts] += unlocked_job_artifacts.length
    end
  else
    query = <<~SQL.squish
      UPDATE "ci_pipelines"
      SET    "locked" = #{::Ci::Pipeline.lockeds[:unlocked]}
      WHERE  "ci_pipelines"."id" in (
          #{collect_pipelines(ci_ref, before_pipeline).select(:id).to_sql}
          LIMIT  #{BATCH_SIZE}
          FOR  UPDATE SKIP LOCKED
      )
      RETURNING "ci_pipelines"."id";
    SQL

    loop do
      unlocked_pipelines = Ci::Pipeline.connection.exec_query(query)

      break if unlocked_pipelines.empty?

      results[:unlocked_pipelines] += unlocked_pipelines.length
    end
  end

  results
end

#unlock_job_artifacts_query(pipeline_ids) ⇒ Object

rubocop:disable CodeReuse/ActiveRecord


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/ci/unlock_artifacts_service.rb', line 53

def unlock_job_artifacts_query(pipeline_ids)
  ci_job_artifacts = ::Ci::JobArtifact.arel_table

  build_ids = ::Ci::Build.select(:id).where(commit_id: pipeline_ids)

  returning = Arel::Nodes::Grouping.new(ci_job_artifacts[:id])

  Arel::UpdateManager.new
    .table(ci_job_artifacts)
    .where(ci_job_artifacts[:job_id].in(Arel.sql(build_ids.to_sql)))
    .set([[ci_job_artifacts[:locked], ::Ci::JobArtifact.lockeds[:unlocked]]])
    .to_sql + " RETURNING #{returning.to_sql}"
end

#unlock_pipelines_query(ci_ref, before_pipeline) ⇒ Object

rubocop:disable CodeReuse/ActiveRecord


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/ci/unlock_artifacts_service.rb', line 69

def unlock_pipelines_query(ci_ref, before_pipeline)
  ci_pipelines = ::Ci::Pipeline.arel_table

  pipelines_scope = ci_ref.pipelines.artifacts_locked
  pipelines_scope = pipelines_scope.before_pipeline(before_pipeline) if before_pipeline
  pipelines_scope = pipelines_scope.select(:id).limit(BATCH_SIZE).lock('FOR UPDATE SKIP LOCKED')

  returning = Arel::Nodes::Grouping.new(ci_pipelines[:id])

  Arel::UpdateManager.new
    .table(ci_pipelines)
    .where(ci_pipelines[:id].in(Arel.sql(pipelines_scope.to_sql)))
    .set([[ci_pipelines[:locked], ::Ci::Pipeline.lockeds[:unlocked]]])
    .to_sql + " RETURNING #{returning.to_sql}"
end