summaryrefslogtreecommitdiff
path: root/app/services/users/migrate_records_to_ghost_user_in_batches_service.rb
blob: d294312cc3088bb0fa17985456aa943b34faa5e6 (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
# frozen_string_literal: true

module Users
  class MigrateRecordsToGhostUserInBatchesService
    LIMIT_SIZE = 1000

    def initialize
      @execution_tracker = Gitlab::Utils::ExecutionTracker.new
    end

    def execute
      ghost_user_migrations.each do |job|
        break if execution_tracker.over_limit?

        service = Users::MigrateRecordsToGhostUserService.new(job.user,
                                                              job.initiator_user,
                                                              execution_tracker)
        service.execute(hard_delete: job.hard_delete)
      rescue Gitlab::Utils::ExecutionTracker::ExecutionTimeOutError
        # no-op
      rescue StandardError => e
        ::Gitlab::ErrorTracking.track_exception(e)
        reschedule(job)
      end
    end

    private

    attr_reader :execution_tracker

    def ghost_user_migrations
      Users::GhostUserMigration.consume_order.limit(LIMIT_SIZE)
    end

    def reschedule(job)
      job.update(consume_after: 30.minutes.from_now)
    end
  end
end