2020-03-23 08:09:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Cleanup
|
|
|
|
class OrphanLfsFileReferences
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
attr_reader :project, :dry_run, :logger, :limit
|
|
|
|
|
|
|
|
DEFAULT_REMOVAL_LIMIT = 1000
|
|
|
|
|
|
|
|
def initialize(project, dry_run: true, logger: nil, limit: nil)
|
|
|
|
@project = project
|
|
|
|
@dry_run = dry_run
|
|
|
|
@logger = logger || Rails.logger # rubocop:disable Gitlab/RailsLogger
|
|
|
|
@limit = limit
|
|
|
|
end
|
|
|
|
|
|
|
|
def run!
|
|
|
|
log_info("Looking for orphan LFS files for project #{project.name_with_namespace}")
|
|
|
|
|
|
|
|
remove_orphan_references
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def remove_orphan_references
|
2020-09-07 08:08:27 -04:00
|
|
|
invalid_references = project.lfs_objects_projects.lfs_object_in(orphan_objects)
|
2020-03-23 08:09:47 -04:00
|
|
|
|
|
|
|
if dry_run
|
|
|
|
log_info("Found invalid references: #{invalid_references.count}")
|
|
|
|
else
|
|
|
|
count = 0
|
|
|
|
invalid_references.each_batch(of: limit || DEFAULT_REMOVAL_LIMIT) do |relation|
|
|
|
|
count += relation.delete_all
|
|
|
|
end
|
|
|
|
|
2020-05-19 11:08:04 -04:00
|
|
|
ProjectCacheWorker.perform_async(project.id, [], [:lfs_objects_size])
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
log_info("Removed invalid references: #{count}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
def orphan_objects
|
|
|
|
# Get these first so racing with a git push can't remove any LFS objects
|
|
|
|
oids = project.lfs_objects_oids
|
2020-03-23 08:09:47 -04:00
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
repos = [
|
|
|
|
project.repository,
|
|
|
|
project.design_repository,
|
|
|
|
project.wiki.repository
|
|
|
|
].select(&:exists?)
|
2020-03-23 08:09:47 -04:00
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
repos.flat_map do |repo|
|
|
|
|
oids -= repo.gitaly_blob_client.get_all_lfs_pointers.map(&:lfs_oid)
|
2020-03-23 08:09:47 -04:00
|
|
|
end
|
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
# The remaining OIDs are not used by any repository, so are orphans
|
|
|
|
LfsObject.for_oids(oids)
|
2020-03-23 08:09:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_info(msg)
|
|
|
|
logger.info("#{'[DRY RUN] ' if dry_run}#{msg}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|