Commit bca2a229 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '327078-refactor-migration-rakes' into 'master'

Refactor Pages deployment and  artifact migrations

See merge request gitlab-org/gitlab!60597
parents 7e940d09 964943f6
# frozen_string_literal: true
module Gitlab
module Ci
module Artifacts
class MigrationHelper
def migrate_to_remote_storage(&block)
artifacts = ::Ci::JobArtifact.with_files_stored_locally
migrate(artifacts, ObjectStorage::Store::REMOTE, &block)
end
def migrate_to_local_storage(&block)
artifacts = ::Ci::JobArtifact.with_files_stored_remotely
migrate(artifacts, ObjectStorage::Store::LOCAL, &block)
end
private
def batch_size
ENV.fetch('MIGRATION_BATCH_SIZE', 10).to_i
end
def migrate(artifacts, store, &block)
artifacts.find_each(batch_size: batch_size) do |artifact| # rubocop:disable CodeReuse/ActiveRecord
artifact.file.migrate!(store)
yield artifact if block
rescue StandardError => e
raise StandardError.new("Failed to transfer artifact of type #{artifact.file_type} and ID #{artifact.id} with error: #{e.message}")
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module LocalAndRemoteStorageMigration
class ArtifactMigrater < Gitlab::LocalAndRemoteStorageMigration::BaseMigrater
private
def items_with_files_stored_locally
::Ci::JobArtifact.with_files_stored_locally
end
def items_with_files_stored_remotely
::Ci::JobArtifact.with_files_stored_remotely
end
end
end
end
# frozen_string_literal: true # frozen_string_literal: true
module Gitlab module Gitlab
module Pages module LocalAndRemoteStorageMigration
class MigrationHelper class BaseMigrater
def initialize(logger = nil) def initialize(logger = nil)
@logger = logger @logger = logger
end end
def migrate_to_remote_storage def migrate_to_remote_storage
deployments = ::PagesDeployment.with_files_stored_locally logger.info('Starting transfer to remote storage')
migrate(deployments, ObjectStorage::Store::REMOTE)
migrate(items_with_files_stored_locally, ObjectStorage::Store::REMOTE)
end end
def migrate_to_local_storage def migrate_to_local_storage
deployments = ::PagesDeployment.with_files_stored_remotely logger.info('Starting transfer to local storage')
migrate(deployments, ObjectStorage::Store::LOCAL)
migrate(items_with_files_stored_remotely, ObjectStorage::Store::LOCAL)
end end
private private
attr_reader :logger
def batch_size def batch_size
ENV.fetch('MIGRATION_BATCH_SIZE', 10).to_i ENV.fetch('MIGRATION_BATCH_SIZE', 10).to_i
end end
def migrate(deployments, store) def migrate(items, store)
deployments.find_each(batch_size: batch_size) do |deployment| # rubocop:disable CodeReuse/ActiveRecord items.find_each(batch_size: batch_size) do |item| # rubocop:disable CodeReuse/ActiveRecord
deployment.file.migrate!(store) item.file.migrate!(store)
log_success(deployment, store) log_success(item, store)
rescue StandardError => e rescue StandardError => e
log_error(e, deployment) log_error(e, item)
end end
end end
def log_success(deployment, store) def log_success(item, store)
logger.info("Transferred deployment ID #{deployment.id} of type #{deployment.file_type} with size #{deployment.size} to #{storage_label(store)} storage") logger.info("Transferred #{item.class.name} ID #{item.id} of type #{item.file_type} with size #{item.size} to #{storage_label(store)} storage")
end end
def log_error(err, deployment) def log_error(err, item)
logger.warn("Failed to transfer deployment of type #{deployment.file_type} and ID #{deployment.id} with error: #{err.message}") logger.warn("Failed to transfer #{item.class.name} of type #{item.file_type} and ID #{item.id} with error: #{err.message}")
end end
def storage_label(store) def storage_label(store)
......
# frozen_string_literal: true
module Gitlab
module LocalAndRemoteStorageMigration
class PagesDeploymentMigrater < Gitlab::LocalAndRemoteStorageMigration::BaseMigrater
private
def items_with_files_stored_locally
::PagesDeployment.with_files_stored_locally
end
def items_with_files_stored_remotely
::PagesDeployment.with_files_stored_remotely
end
end
end
end
...@@ -8,14 +8,11 @@ namespace :gitlab do ...@@ -8,14 +8,11 @@ namespace :gitlab do
namespace :artifacts do namespace :artifacts do
task migrate: :environment do task migrate: :environment do
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
logger.info('Starting transfer of artifacts to remote storage')
helper = Gitlab::Ci::Artifacts::MigrationHelper.new helper = Gitlab::LocalAndRemoteStorageMigration::ArtifactMigrater.new(logger)
begin begin
helper.migrate_to_remote_storage do |artifact| helper.migrate_to_remote_storage
logger.info("Transferred artifact ID #{artifact.id} of type #{artifact.file_type} with size #{artifact.size} to object storage")
end
rescue StandardError => e rescue StandardError => e
logger.error(e.message) logger.error(e.message)
end end
...@@ -23,14 +20,11 @@ namespace :gitlab do ...@@ -23,14 +20,11 @@ namespace :gitlab do
task migrate_to_local: :environment do task migrate_to_local: :environment do
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
logger.info('Starting transfer of artifacts to local storage')
helper = Gitlab::Ci::Artifacts::MigrationHelper.new helper = Gitlab::LocalAndRemoteStorageMigration::ArtifactMigrater.new(logger)
begin begin
helper.migrate_to_local_storage do |artifact| helper.migrate_to_local_storage
logger.info("Transferred artifact ID #{artifact.id} of type #{artifact.file_type} with size #{artifact.size} to local storage")
end
rescue StandardError => e rescue StandardError => e
logger.error(e.message) logger.error(e.message)
end end
......
...@@ -61,9 +61,8 @@ namespace :gitlab do ...@@ -61,9 +61,8 @@ namespace :gitlab do
namespace :deployments do namespace :deployments do
task migrate_to_object_storage: :gitlab_environment do task migrate_to_object_storage: :gitlab_environment do
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
logger.info('Starting transfer of pages deployments to remote storage')
helper = Gitlab::Pages::MigrationHelper.new(logger) helper = Gitlab::LocalAndRemoteStorageMigration::PagesDeploymentMigrater.new(logger)
begin begin
helper.migrate_to_remote_storage helper.migrate_to_remote_storage
...@@ -74,9 +73,8 @@ namespace :gitlab do ...@@ -74,9 +73,8 @@ namespace :gitlab do
task migrate_to_local: :gitlab_environment do task migrate_to_local: :gitlab_environment do
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
logger.info('Starting transfer of Pages deployments to local storage')
helper = Gitlab::Pages::MigrationHelper.new(logger) helper = Gitlab::LocalAndRemoteStorageMigration::PagesDeploymentMigrater.new(logger)
begin begin
helper.migrate_to_local_storage helper.migrate_to_local_storage
......
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