Commit 93148873 authored by Mike Kozono's avatar Mike Kozono

Fix cleanup rake task limit

Methods defined in rake tasks are defined globally.
parent 7916beef
---
title: Fix gitlab:cleanup:orphan_job_artifact_files rake task limit
merge_request: 52056
author:
type: fixed
...@@ -12,10 +12,9 @@ module Gitlab ...@@ -12,10 +12,9 @@ module Gitlab
VALID_NICENESS_LEVELS = %w{none realtime best-effort idle}.freeze VALID_NICENESS_LEVELS = %w{none realtime best-effort idle}.freeze
attr_accessor :batch, :total_found, :total_cleaned attr_accessor :batch, :total_found, :total_cleaned
attr_reader :limit, :dry_run, :niceness, :logger attr_reader :dry_run, :niceness, :logger
def initialize(limit: nil, dry_run: true, niceness: nil, logger: nil) def initialize(dry_run: true, niceness: nil, logger: nil)
@limit = limit
@dry_run = dry_run @dry_run = dry_run
@niceness = (niceness || DEFAULT_NICENESS).downcase @niceness = (niceness || DEFAULT_NICENESS).downcase
@logger = logger || Gitlab::AppLogger @logger = logger || Gitlab::AppLogger
...@@ -31,7 +30,11 @@ module Gitlab ...@@ -31,7 +30,11 @@ module Gitlab
batch << artifact_file batch << artifact_file
clean_batch! if batch.full? clean_batch! if batch.full?
break if limit_reached?
if limit_reached?
log_info("Exiting due to reaching limit of #{limit}.")
break
end
end end
clean_batch! clean_batch!
...@@ -128,6 +131,10 @@ module Gitlab ...@@ -128,6 +131,10 @@ module Gitlab
def log_error(msg, params = {}) def log_error(msg, params = {})
logger.error(msg) logger.error(msg)
end end
def limit
ENV['LIMIT']&.to_i
end
end end
end end
end end
......
...@@ -5,15 +5,14 @@ module Gitlab ...@@ -5,15 +5,14 @@ module Gitlab
class OrphanLfsFileReferences class OrphanLfsFileReferences
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
attr_reader :project, :dry_run, :logger, :limit attr_reader :project, :dry_run, :logger
DEFAULT_REMOVAL_LIMIT = 1000 DEFAULT_REMOVAL_LIMIT = 1000
def initialize(project, dry_run: true, logger: nil, limit: nil) def initialize(project, dry_run: true, logger: nil)
@project = project @project = project
@dry_run = dry_run @dry_run = dry_run
@logger = logger || Gitlab::AppLogger @logger = logger || Gitlab::AppLogger
@limit = limit
end end
def run! def run!
...@@ -67,6 +66,10 @@ module Gitlab ...@@ -67,6 +66,10 @@ module Gitlab
def log_info(msg) def log_info(msg)
logger.info("#{'[DRY RUN] ' if dry_run}#{msg}") logger.info("#{'[DRY RUN] ' if dry_run}#{msg}")
end end
def limit
ENV['LIMIT']&.to_i
end
end end
end end
end end
...@@ -56,7 +56,7 @@ namespace :gitlab do ...@@ -56,7 +56,7 @@ namespace :gitlab do
task orphan_job_artifact_files: :gitlab_environment do task orphan_job_artifact_files: :gitlab_environment do
warn_user_is_not_gitlab warn_user_is_not_gitlab
cleaner = Gitlab::Cleanup::OrphanJobArtifactFiles.new(limit: limit, dry_run: dry_run?, niceness: niceness, logger: logger) cleaner = Gitlab::Cleanup::OrphanJobArtifactFiles.new(dry_run: dry_run?, niceness: niceness, logger: logger)
cleaner.run! cleaner.run!
if dry_run? if dry_run?
...@@ -78,8 +78,7 @@ namespace :gitlab do ...@@ -78,8 +78,7 @@ namespace :gitlab do
cleaner = Gitlab::Cleanup::OrphanLfsFileReferences.new( cleaner = Gitlab::Cleanup::OrphanLfsFileReferences.new(
project, project,
dry_run: dry_run?, dry_run: dry_run?,
logger: logger, logger: logger
limit: limit
) )
cleaner.run! cleaner.run!
...@@ -162,10 +161,6 @@ namespace :gitlab do ...@@ -162,10 +161,6 @@ namespace :gitlab do
ENV['DEBUG'].present? ENV['DEBUG'].present?
end end
def limit
ENV['LIMIT']&.to_i
end
def niceness def niceness
ENV['NICENESS'].presence ENV['NICENESS'].presence
end end
......
...@@ -13,7 +13,7 @@ namespace :gitlab do ...@@ -13,7 +13,7 @@ namespace :gitlab do
raise "Please supply the list of ids through the SNIPPET_IDS env var" raise "Please supply the list of ids through the SNIPPET_IDS env var"
end end
raise "Invalid limit value" if limit == 0 raise "Invalid limit value" if snippet_task_limit == 0
if migration_running? if migration_running?
raise "There are already snippet migrations running. Please wait until they are finished." raise "There are already snippet migrations running. Please wait until they are finished."
...@@ -41,8 +41,8 @@ namespace :gitlab do ...@@ -41,8 +41,8 @@ namespace :gitlab do
end end
end end
if ids.size > limit if ids.size > snippet_task_limit
raise "The number of ids provided is higher than #{limit}. You can update this limit by using the env var `LIMIT`" raise "The number of ids provided is higher than #{snippet_task_limit}. You can update this limit by using the env var `LIMIT`"
end end
ids ids
...@@ -68,14 +68,14 @@ namespace :gitlab do ...@@ -68,14 +68,14 @@ namespace :gitlab do
# bundle exec rake gitlab:snippets:list_non_migrated LIMIT=50 # bundle exec rake gitlab:snippets:list_non_migrated LIMIT=50
desc 'GitLab | Show non migrated snippets' desc 'GitLab | Show non migrated snippets'
task list_non_migrated: :environment do task list_non_migrated: :environment do
raise "Invalid limit value" if limit == 0 raise "Invalid limit value" if snippet_task_limit == 0
non_migrated_count = non_migrated_snippets.count non_migrated_count = non_migrated_snippets.count
if non_migrated_count == 0 if non_migrated_count == 0
puts "All snippets have been successfully migrated" puts "All snippets have been successfully migrated"
else else
puts "There are #{non_migrated_count} snippets that haven't been migrated. Showing a batch of ids of those snippets:\n" puts "There are #{non_migrated_count} snippets that haven't been migrated. Showing a batch of ids of those snippets:\n"
puts non_migrated_snippets.limit(limit).pluck(:id).join(',') puts non_migrated_snippets.limit(snippet_task_limit).pluck(:id).join(',')
end end
end end
...@@ -84,7 +84,7 @@ namespace :gitlab do ...@@ -84,7 +84,7 @@ namespace :gitlab do
end end
# There are problems with the specs if we memoize this value # There are problems with the specs if we memoize this value
def limit def snippet_task_limit
ENV['LIMIT'] ? ENV['LIMIT'].to_i : DEFAULT_LIMIT ENV['LIMIT'] ? ENV['LIMIT'].to_i : DEFAULT_LIMIT
end end
end end
......
...@@ -42,7 +42,8 @@ RSpec.describe Gitlab::Cleanup::OrphanJobArtifactFiles do ...@@ -42,7 +42,8 @@ RSpec.describe Gitlab::Cleanup::OrphanJobArtifactFiles do
end end
it 'stops when limit is reached' do it 'stops when limit is reached' do
cleanup = described_class.new(limit: 1) stub_env('LIMIT', 1)
cleanup = described_class.new
mock_artifacts_found(cleanup, 'tmp/foo/bar/1', 'tmp/foo/bar/2') mock_artifacts_found(cleanup, 'tmp/foo/bar/1', 'tmp/foo/bar/2')
......
...@@ -109,8 +109,7 @@ RSpec.describe 'gitlab:cleanup rake tasks' do ...@@ -109,8 +109,7 @@ RSpec.describe 'gitlab:cleanup rake tasks' do
it 'passes dry_run correctly' do it 'passes dry_run correctly' do
expect(Gitlab::Cleanup::OrphanJobArtifactFiles) expect(Gitlab::Cleanup::OrphanJobArtifactFiles)
.to receive(:new) .to receive(:new)
.with(limit: anything, .with(dry_run: false,
dry_run: false,
niceness: anything, niceness: anything,
logger: anything) logger: anything)
.and_call_original .and_call_original
...@@ -145,7 +144,6 @@ RSpec.describe 'gitlab:cleanup rake tasks' do ...@@ -145,7 +144,6 @@ RSpec.describe 'gitlab:cleanup rake tasks' do
expect(Gitlab::Cleanup::OrphanLfsFileReferences) expect(Gitlab::Cleanup::OrphanLfsFileReferences)
.to receive(:new) .to receive(:new)
.with(project, .with(project,
limit: anything,
dry_run: false, dry_run: false,
logger: anything) logger: anything)
.and_call_original .and_call_original
...@@ -153,24 +151,6 @@ RSpec.describe 'gitlab:cleanup rake tasks' do ...@@ -153,24 +151,6 @@ RSpec.describe 'gitlab:cleanup rake tasks' do
rake_task rake_task
end end
end end
context 'with LIMIT set to 100' do
before do
stub_env('LIMIT', '100')
end
it 'passes limit as integer' do
expect(Gitlab::Cleanup::OrphanLfsFileReferences)
.to receive(:new)
.with(project,
limit: 100,
dry_run: true,
logger: anything)
.and_call_original
rake_task
end
end
end end
describe 'gitlab:cleanup:orphan_lfs_files' do describe 'gitlab:cleanup:orphan_lfs_files' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment