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

process_sync_events_service.rb « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 11ce6e8eeaf5e93511ab198ca1ab94c6ac44a491 (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
# frozen_string_literal: true

module Ci
  class ProcessSyncEventsService
    include Gitlab::Utils::StrongMemoize
    include ExclusiveLeaseGuard

    BATCH_SIZE = 1000

    def initialize(sync_event_class, sync_class)
      @sync_event_class = sync_event_class
      @sync_class = sync_class
    end

    def execute
      return unless ::Feature.enabled?(:ci_namespace_project_mirrors, default_enabled: :yaml)

      # preventing parallel processing over the same event table
      try_obtain_lease { process_events }

      enqueue_worker_if_there_still_event
    end

    private

    def process_events
      events = @sync_event_class.preload_synced_relation.first(BATCH_SIZE)

      return if events.empty?

      processed_events = []

      begin
        events.each do |event|
          @sync_class.sync!(event)

          processed_events << event
        end
      ensure
        @sync_event_class.id_in(processed_events).delete_all
      end
    end

    def enqueue_worker_if_there_still_event
      @sync_event_class.enqueue_worker if @sync_event_class.exists?
    end

    def lease_key
      "#{super}::#{@sync_event_class}"
    end

    def lease_timeout
      1.minute
    end
  end
end