Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 巨大なリポジトリのバックアップを作ろうとするとBackup::Repositoryでエラーになるのでコネクションを明示的に取得するためのモンキーパッチ
# (lib/tasks/gitlab/backup.rake よりも後にloadされてほしいのでわざとz_を付与)
# https://gitlab.com/gitlab-org/gitlab-ce/blob/v12.2.4/lib/backup/repository.rb
# FIXME: gitlab-ceにMRを投げてマージされたら削除したい
task :backup_repository_monkey_patch do
module BackupRepositoryMonkeyPatch
MAX_RETRY_COUNT = 1
def backup_custom_hooks(project)
FileUtils.mkdir_p(project_backup_path(project))
custom_hooks_path = custom_hooks_tar(project)
# monkey patched
with_reconnect_and_retry do
Gitlab::GitalyClient::RepositoryService.new(project.repository)
.backup_custom_hooks(custom_hooks_path)
end
end
def display_repo_path(project)
with_reconnect_and_retry do
super project
end
end
def with_reconnect_and_retry
ActiveRecord::Base.connection_pool.with_connection do |conn|
yield
rescue ActiveRecord::StatementInvalid => e
case e.cause
when PG::UnableToSend, PG::ConnectionBad
retry_count ||= 0
retry_count += 1
raise if retry_count > MAX_RETRY_COUNT
progress.puts "[WARNING] Error occurred #{e}. So reconnect and retry (#{retry_count}/#{MAX_RETRY_COUNT})".color(:orange)
conn.reconnect!
retry
else
raise
end
end
end
end
require "backup/repository"
Backup::Repository.prepend BackupRepositoryMonkeyPatch
end
# gitlab:backup:repo:create の実行直前にBackup::Repositoryにモンキーパッチを仕込む
Rake::Task["gitlab:backup:repo:create"].enhance([:backup_repository_monkey_patch])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment