Welcome to mirror list, hosted at ThFree Co, Russian Federation.

record_data_repair_detail_worker.rb « container_registry « workers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 390481f8e011ccd6559a3e3e0339a2ca5e3b7536 (plain)
1
2
3
4
5
6
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

module ContainerRegistry
  class RecordDataRepairDetailWorker
    include ApplicationWorker
    include ExclusiveLeaseGuard
    include LimitedCapacity::Worker
    include Gitlab::Utils::StrongMemoize

    data_consistency :always # rubocop:disable SidekiqLoadBalancing/WorkerDataConsistency
    queue_namespace :container_repository
    feature_category :container_registry
    urgency :low
    worker_resource_boundary :unknown
    idempotent!

    LEASE_TIMEOUT = 1.hour.to_i

    def perform_work
      return unless Gitlab.com?
      return unless next_project
      return if next_project.container_registry_data_repair_detail

      missing_count = 0

      try_obtain_lease do
        detail = create_data_repair_detail

        GitlabApiClient.each_sub_repositories_with_tag_page(path: next_project.full_path,
          page_size: 50) do |repositories|
          next if repositories.empty?

          paths = repositories.map { |repo| ContainerRegistry::Path.new(repo["path"]) }
          paths, invalid_paths = paths.partition(&:valid?)
          unless invalid_paths.empty?
            log_extra_metadata_on_done(
              :invalid_paths_parsed_in_container_repository_repair,
              invalid_paths.join(' ,')
            )
          end

          found_repositories = next_project.container_repositories.where(name: paths.map(&:repository_name)) # rubocop:disable CodeReuse/ActiveRecord

          missing_count += repositories.count - found_repositories.count
        end
        detail.update!(missing_count: missing_count, status: :completed)
      end
    rescue StandardError => exception
      next_project.reset.container_registry_data_repair_detail&.update(status: :failed)
      Gitlab::ErrorTracking.log_exception(exception, class: self.class.name)
    end

    def remaining_work_count
      return 0 unless Gitlab.com?
      return 0 unless Feature.enabled?(:registry_data_repair_worker)
      return 0 unless ContainerRegistry::GitlabApiClient.supports_gitlab_api?

      Project.pending_data_repair_analysis.limit(max_running_jobs + 1).count
    end

    def max_running_jobs
      current_application_settings.container_registry_data_repair_detail_worker_max_concurrency.to_i
    end

    private

    def current_application_settings
      ::Gitlab::CurrentSettings.current_application_settings
    end

    def next_project
      Project.pending_data_repair_analysis.first
    end
    strong_memoize_attr :next_project

    def create_data_repair_detail
      ContainerRegistry::DataRepairDetail.create!(project: next_project, status: :ongoing)
    end

    # Used by ExclusiveLeaseGuard
    def lease_key
      "container_registry_data_repair_detail_worker:#{next_project.id}"
    end

    # Used by ExclusiveLeaseGuard
    def lease_timeout
      LEASE_TIMEOUT
    end
  end
end