diff options
Diffstat (limited to 'app/workers/loose_foreign_keys/cleanup_worker.rb')
-rw-r--r-- | app/workers/loose_foreign_keys/cleanup_worker.rb | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/app/workers/loose_foreign_keys/cleanup_worker.rb b/app/workers/loose_foreign_keys/cleanup_worker.rb index b4565dbf624..c3492fed77b 100644 --- a/app/workers/loose_foreign_keys/cleanup_worker.rb +++ b/app/workers/loose_foreign_keys/cleanup_worker.rb @@ -6,6 +6,7 @@ module LooseForeignKeys include Gitlab::ExclusiveLeaseHelpers include CronjobQueue # rubocop: disable Scalability/CronWorkerContext + sidekiq_options retry: false feature_category :sharding data_consistency :always idempotent! @@ -13,13 +14,30 @@ module LooseForeignKeys def perform return if Feature.disabled?(:loose_foreign_key_cleanup, default_enabled: :yaml) - ttl = ModificationTracker::MAX_RUNTIME + 1.minute - in_lock(self.class.name.underscore, ttl: ttl, retries: 0) do - # TODO: Iterate over the connections - # https://gitlab.com/gitlab-org/gitlab/-/issues/341513 - stats = ProcessDeletedRecordsService.new(connection: ApplicationRecord.connection).execute + in_lock(self.class.name.underscore, ttl: ModificationTracker::MAX_RUNTIME, retries: 0) do + stats = {} + + connection_name, base_model = current_connection_name_and_base_model + + Gitlab::Database::SharedModel.using_connection(base_model.connection) do + stats = ProcessDeletedRecordsService.new(connection: base_model.connection).execute + stats[:connection] = connection_name + end + log_extra_metadata_on_done(:stats, stats) end end + + private + + # Rotate the databases every minute + # + # If one DB is configured: every minute use the configured DB + # If two DBs are configured (Main, CI): minute 1 -> Main, minute 2 -> CI + def current_connection_name_and_base_model + minutes_since_epoch = Time.current.to_i / 60 + connections_with_name = Gitlab::Database.database_base_models.to_a # this will never be empty + connections_with_name[minutes_since_epoch % connections_with_name.count] + end end end |