Class: BatchedGitRefUpdates::ProjectCleanupService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::ExclusiveLeaseHelpers
Defined in:
app/services/batched_git_ref_updates/project_cleanup_service.rb

Constant Summary collapse

LOCK_TIMEOUT =
10.minutes
GITALY_BATCH_SIZE =
100
QUERY_BATCH_SIZE =
1000
MAX_DELETES =
10_000

Constants included from Gitlab::ExclusiveLeaseHelpers

Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError

Instance Method Summary collapse

Methods included from Gitlab::ExclusiveLeaseHelpers

#in_lock

Constructor Details

#initialize(project_id) ⇒ ProjectCleanupService

Returns a new instance of ProjectCleanupService.



12
13
14
# File 'app/services/batched_git_ref_updates/project_cleanup_service.rb', line 12

def initialize(project_id)
  @project_id = project_id
end

Instance Method Details

#executeObject



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
# File 'app/services/batched_git_ref_updates/project_cleanup_service.rb', line 16

def execute
  total_deletes = 0

  in_lock("#{self.class}/#{@project_id}", retries: 0, ttl: LOCK_TIMEOUT) do
    project = Project.find_by_id(@project_id)
    break unless project

    Deletion
      .status_pending
      .for_project(@project_id)
      .select_ref_and_identity
      .each_batch(of: QUERY_BATCH_SIZE) do |batch|
      refs = batch.map(&:ref)

      refs.each_slice(GITALY_BATCH_SIZE) do |refs_to_delete|
        project.repository.delete_refs(*refs_to_delete)
      end

      total_deletes += refs.count
      Deletion.mark_records_processed(batch)

      break if total_deletes >= MAX_DELETES
    end
  end

  { total_deletes: total_deletes }
end