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

59 lines
1.7 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
class GitGarbageCollectWorker
include Sidekiq::Worker
2016-11-03 12:29:30 +05:30
include DedicatedSidekiqQueue
2017-08-17 22:00:37 +05:30
include Gitlab::CurrentSettings
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
sidekiq_options retry: false
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
def perform(project_id, task = :gc, lease_key = nil, lease_uuid = nil)
2016-08-24 12:49:21 +05:30
project = Project.find(project_id)
2017-08-17 22:00:37 +05:30
task = task.to_sym
cmd = command(task)
repo_path = project.repository.path_to_repo
description = "'#{cmd.join(' ')}' in #{repo_path}"
Gitlab::GitLogger.info(description)
output, status = Gitlab::Popen.popen(cmd, repo_path)
Gitlab::GitLogger.error("#{description} failed:\n#{output}") unless status.zero?
2016-08-24 12:49:21 +05:30
# Refresh the branch cache in case garbage collection caused a ref lookup to fail
2017-08-17 22:00:37 +05:30
flush_ref_caches(project) if task == :gc
ensure
Gitlab::ExclusiveLease.cancel(lease_key, lease_uuid) if lease_key.present? && lease_uuid.present?
end
private
def command(task)
case task
when :gc
git(write_bitmaps: bitmaps_enabled?) + %w[gc]
when :full_repack
git(write_bitmaps: bitmaps_enabled?) + %w[repack -A -d --pack-kept-objects]
when :incremental_repack
# Normal git repack fails when bitmaps are enabled. It is impossible to
# create a bitmap here anyway.
git(write_bitmaps: false) + %w[repack -d]
else
raise "Invalid gc task: #{task.inspect}"
end
end
def flush_ref_caches(project)
2016-08-24 12:49:21 +05:30
project.repository.after_create_branch
project.repository.branch_names
project.repository.has_visible_content?
end
2017-08-17 22:00:37 +05:30
def bitmaps_enabled?
current_application_settings.housekeeping_bitmaps_enabled
end
def git(write_bitmaps:)
config_value = write_bitmaps ? 'true' : 'false'
%W[git -c repack.writeBitmaps=#{config_value}]
end
2016-08-24 12:49:21 +05:30
end