summaryrefslogtreecommitdiff
path: root/app/workers/database
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-11-07 03:07:49 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-11-07 03:07:49 +0000
commita31408ba64f61275813cc3ffd5aa9bc9ce9f3319 (patch)
tree711e3c6f7ea239e0aedc28a815d7067280399314 /app/workers/database
parent175f124d93ba52aeb850b5c032930168612d1e71 (diff)
downloadgitlab-ce-a31408ba64f61275813cc3ffd5aa9bc9ce9f3319.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/workers/database')
-rw-r--r--app/workers/database/batched_background_migration/execution_worker.rb42
-rw-r--r--app/workers/database/batched_background_migration/single_database_worker.rb11
2 files changed, 44 insertions, 9 deletions
diff --git a/app/workers/database/batched_background_migration/execution_worker.rb b/app/workers/database/batched_background_migration/execution_worker.rb
new file mode 100644
index 00000000000..341e509c9c6
--- /dev/null
+++ b/app/workers/database/batched_background_migration/execution_worker.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+module Database
+ module BatchedBackgroundMigration
+ class ExecutionWorker # rubocop:disable Scalability/IdempotentWorker
+ include Gitlab::Utils::StrongMemoize
+
+ INTERVAL_VARIANCE = 5.seconds.freeze
+
+ def perform(database_name, migration_id)
+ self.database_name = database_name
+
+ Gitlab::Database::SharedModel.using_connection(base_model.connection) do
+ migration = find_migration(migration_id)
+
+ break unless migration
+
+ run(migration) if migration.active? && migration.interval_elapsed?(variance: INTERVAL_VARIANCE)
+ end
+ end
+
+ private
+
+ attr_accessor :database_name
+
+ def base_model
+ strong_memoize(:base_model) do
+ Gitlab::Database.database_base_models[database_name]
+ end
+ end
+
+ def find_migration(id)
+ Gitlab::Database::BackgroundMigration::BatchedMigration.find_executable(id, connection: base_model.connection)
+ end
+
+ def run(migration)
+ Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.new(connection: base_model.connection)
+ .run_migration_job(migration)
+ end
+ end
+ end
+end
diff --git a/app/workers/database/batched_background_migration/single_database_worker.rb b/app/workers/database/batched_background_migration/single_database_worker.rb
index cfbd44ba397..0c7c51d5c0a 100644
--- a/app/workers/database/batched_background_migration/single_database_worker.rb
+++ b/app/workers/database/batched_background_migration/single_database_worker.rb
@@ -58,14 +58,7 @@ module Database
break unless self.class.enabled? && active_migration
with_exclusive_lease(active_migration.interval) do
- # Now that we have the exclusive lease, reload migration in case another process has changed it.
- # This is a temporary solution until we have better concurrency handling around job execution
- #
- # We also have to disable this cop, because ApplicationRecord aliases reset to reload, but our database
- # models don't inherit from ApplicationRecord
- active_migration.reload # rubocop:disable Cop/ActiveRecordAssociationReload
-
- run_active_migration if active_migration.active? && active_migration.interval_elapsed?(variance: INTERVAL_VARIANCE)
+ run_active_migration
end
end
end
@@ -77,7 +70,7 @@ module Database
end
def run_active_migration
- Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.new(connection: base_model.connection).run_migration_job(active_migration)
+ Database::BatchedBackgroundMigration::ExecutionWorker.new.perform(self.class.tracking_database, active_migration.id)
end
def base_model