debian-mirror-gitlab/app/workers/project_migrate_hashed_storage_worker.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
class ProjectMigrateHashedStorageWorker
include ApplicationWorker
LEASE_TIMEOUT = 30.seconds.to_i
2019-03-02 22:35:43 +05:30
LEASE_KEY_SEGMENT = 'project_migrate_hashed_storage_worker'.freeze
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def perform(project_id, old_disk_path = nil)
2018-03-17 18:26:18 +05:30
uuid = lease_for(project_id).try_obtain
2019-03-02 22:35:43 +05:30
2018-03-17 18:26:18 +05:30
if uuid
2019-03-02 22:35:43 +05:30
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
2018-03-17 18:26:18 +05:30
else
2019-03-02 22:35:43 +05:30
return false
2018-03-17 18:26:18 +05:30
end
2019-03-02 22:35:43 +05:30
ensure
2018-03-17 18:26:18 +05:30
cancel_lease_for(project_id, uuid) if uuid
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT)
end
private
def lease_key(project_id)
2019-03-02 22:35:43 +05:30
# we share the same lease key for both migration and rollback so they don't run simultaneously
"#{LEASE_KEY_SEGMENT}:#{project_id}"
2018-03-17 18:26:18 +05:30
end
def cancel_lease_for(project_id, uuid)
Gitlab::ExclusiveLease.cancel(lease_key(project_id), uuid)
end
end