2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-08 15:32:58 -04:00
|
|
|
class GitGarbageCollectWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2016-07-08 15:32:58 -04:00
|
|
|
|
2016-10-21 12:13:41 -04:00
|
|
|
sidekiq_options retry: false
|
2016-07-08 15:32:58 -04:00
|
|
|
|
2017-09-04 13:55:04 -04:00
|
|
|
# Timeout set to 24h
|
|
|
|
LEASE_TIMEOUT = 86400
|
|
|
|
|
2016-10-27 08:59:52 -04:00
|
|
|
def perform(project_id, task = :gc, lease_key = nil, lease_uuid = nil)
|
2016-07-08 15:32:58 -04:00
|
|
|
project = Project.find(project_id)
|
2017-09-04 13:55:04 -04:00
|
|
|
active_uuid = get_lease_uuid(lease_key)
|
|
|
|
|
|
|
|
if active_uuid
|
|
|
|
return unless active_uuid == lease_uuid
|
|
|
|
|
|
|
|
renew_lease(lease_key, active_uuid)
|
|
|
|
else
|
|
|
|
lease_uuid = try_obtain_lease(lease_key)
|
2016-10-27 08:59:52 -04:00
|
|
|
|
2017-09-04 13:55:04 -04:00
|
|
|
return unless lease_uuid
|
|
|
|
end
|
|
|
|
|
|
|
|
task = task.to_sym
|
2018-06-13 10:36:43 -04:00
|
|
|
gitaly_call(task, project.repository.raw_repository)
|
2016-07-08 15:32:58 -04:00
|
|
|
|
2016-07-13 09:41:06 -04:00
|
|
|
# Refresh the branch cache in case garbage collection caused a ref lookup to fail
|
2016-10-27 08:59:52 -04:00
|
|
|
flush_ref_caches(project) if task == :gc
|
2018-03-04 01:18:32 -05:00
|
|
|
|
|
|
|
# In case pack files are deleted, release libgit2 cache and open file
|
|
|
|
# descriptors ASAP instead of waiting for Ruby garbage collection
|
|
|
|
project.cleanup
|
2016-10-27 08:59:52 -04:00
|
|
|
ensure
|
2017-09-04 13:55:04 -04:00
|
|
|
cancel_lease(lease_key, lease_uuid) if lease_key.present? && lease_uuid.present?
|
2016-10-27 08:59:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-04 13:55:04 -04:00
|
|
|
def try_obtain_lease(key)
|
|
|
|
::Gitlab::ExclusiveLease.new(key, timeout: LEASE_TIMEOUT).try_obtain
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew_lease(key, uuid)
|
|
|
|
::Gitlab::ExclusiveLease.new(key, uuid: uuid, timeout: LEASE_TIMEOUT).renew
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancel_lease(key, uuid)
|
|
|
|
::Gitlab::ExclusiveLease.cancel(key, uuid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_lease_uuid(key)
|
|
|
|
::Gitlab::ExclusiveLease.get_uuid(key)
|
|
|
|
end
|
|
|
|
|
2017-07-19 11:34:14 -04:00
|
|
|
## `repository` has to be a Gitlab::Git::Repository
|
|
|
|
def gitaly_call(task, repository)
|
|
|
|
client = Gitlab::GitalyClient::RepositoryService.new(repository)
|
|
|
|
case task
|
|
|
|
when :gc
|
|
|
|
client.garbage_collect(bitmaps_enabled?)
|
|
|
|
when :full_repack
|
|
|
|
client.repack_full(bitmaps_enabled?)
|
|
|
|
when :incremental_repack
|
|
|
|
client.repack_incremental
|
|
|
|
end
|
2018-06-13 10:36:43 -04:00
|
|
|
rescue GRPC::NotFound => e
|
2018-07-10 18:08:54 -04:00
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\nRepository not found")
|
2018-06-13 10:36:43 -04:00
|
|
|
raise Gitlab::Git::Repository::NoRepository.new(e)
|
|
|
|
rescue GRPC::BadStatus => e
|
2018-07-10 18:08:54 -04:00
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\n#{e}")
|
2018-06-13 10:36:43 -04:00
|
|
|
raise Gitlab::Git::CommandError.new(e)
|
2016-10-27 08:59:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_ref_caches(project)
|
2016-07-08 15:32:58 -04:00
|
|
|
project.repository.after_create_branch
|
2016-07-13 09:41:06 -04:00
|
|
|
project.repository.branch_names
|
|
|
|
project.repository.has_visible_content?
|
2016-07-08 15:32:58 -04:00
|
|
|
end
|
2016-10-27 08:59:52 -04:00
|
|
|
|
|
|
|
def bitmaps_enabled?
|
2018-02-02 13:39:55 -05:00
|
|
|
Gitlab::CurrentSettings.housekeeping_bitmaps_enabled
|
2016-10-27 08:59:52 -04:00
|
|
|
end
|
2016-07-08 15:32:58 -04:00
|
|
|
end
|