debian-mirror-gitlab/app/services/ci/runners/stale_managers_cleanup_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 lines
956 B
Ruby
Raw Normal View History

2023-04-23 21:23:45 +05:30
# frozen_string_literal: true
module Ci
module Runners
2023-06-20 00:43:36 +05:30
class StaleManagersCleanupService
2023-04-23 21:23:45 +05:30
MAX_DELETIONS = 1000
def execute
ServiceResponse.success(payload: {
# the `stale` relationship can return duplicates, so we don't try to return a precise count here
2023-06-20 00:43:36 +05:30
deleted_managers: delete_stale_runner_managers > 0
2023-04-23 21:23:45 +05:30
})
end
private
2023-06-20 00:43:36 +05:30
def delete_stale_runner_managers
2023-04-23 21:23:45 +05:30
total_deleted_count = 0
loop do
sub_batch_limit = [100, MAX_DELETIONS].min
# delete_all discards part of the `stale` scope query, so we expliclitly wrap it with a SELECT as a workaround
2023-06-20 00:43:36 +05:30
deleted_count = Ci::RunnerManager.id_in(Ci::RunnerManager.stale.limit(sub_batch_limit)).delete_all
2023-04-23 21:23:45 +05:30
total_deleted_count += deleted_count
break if deleted_count == 0 || total_deleted_count >= MAX_DELETIONS
end
total_deleted_count
end
end
end
end