summaryrefslogtreecommitdiff
path: root/app/workers/project_migrate_hashed_storage_worker.rb
blob: 1c8f313e6e94ea658cb96c843c01faa83adc059f (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
# frozen_string_literal: true

class ProjectMigrateHashedStorageWorker
  include ApplicationWorker

  LEASE_TIMEOUT = 30.seconds.to_i
  LEASE_KEY_SEGMENT = 'project_migrate_hashed_storage_worker'.freeze

  # rubocop: disable CodeReuse/ActiveRecord
  def perform(project_id, old_disk_path = nil)
    uuid = lease_for(project_id).try_obtain

    if uuid
      project = Project.find_by(id: project_id)
      return if project.nil? || project.pending_delete?

      old_disk_path ||= project.disk_path

      ::Projects::HashedStorage::MigrationService.new(project, old_disk_path, logger: logger).execute
    else
      return false
    end

  ensure
    cancel_lease_for(project_id, uuid) if uuid
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def lease_for(project_id)
    Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT)
  end

  private

  def lease_key(project_id)
    # we share the same lease key for both migration and rollback so they don't run simultaneously
    "#{LEASE_KEY_SEGMENT}:#{project_id}"
  end

  def cancel_lease_for(project_id, uuid)
    Gitlab::ExclusiveLease.cancel(lease_key(project_id), uuid)
  end
end