Commit 05f868b9 authored by Mark Chao's avatar Mark Chao

Process all projects synchronously

This simplifies things.
And amount of projects are quite small.
parent 0c3092e9
......@@ -6,24 +6,25 @@ class MigrateProjectApprovers < ActiveRecord::Migration[5.0]
DOWNTIME = false
BATCH_SIZE = 1000
class Project < ActiveRecord::Base
class MergeRequest < ActiveRecord::Base
include ::EachBatch
self.table_name = 'projects'
self.table_name = 'merge_requests'
end
def up
jobs = []
Project.each_batch(of: BATCH_SIZE) do |scope|
project_ids = scope.pluck(:id)
class Approver < ActiveRecord::Base
self.table_name = 'approvers'
end
if jobs.length >= BACKGROUND_MIGRATION_JOB_BUFFER_SIZE
BackgroundMigrationWorker.bulk_perform_async(jobs)
jobs.clear
end
class ApproverGroup < ActiveRecord::Base
self.table_name = 'approver_groups'
end
jobs << ['MigrateApproverToApprovalRulesInBatch', ['Project', project_ids]]
def up
get_project_ids.each do |project_id|
Gitlab::BackgroundMigration::MigrateApproverToApprovalRules.new.perform('Project', project_id)
end
BackgroundMigrationWorker.bulk_perform_async(jobs)
bulk_queue_background_migration_jobs_by_range(MergeRequest, 'MigrateApproverToApprovalRulesInBatch')
check_time = Gitlab::BackgroundMigration::MigrateApproverToApprovalRulesCheckProgress::RESCHEDULE_DELAY
BackgroundMigrationWorker.bulk_perform_in(check_time, [['MigrateApproverToApprovalRulesCheckProgress']])
......@@ -31,4 +32,13 @@ class MigrateProjectApprovers < ActiveRecord::Migration[5.0]
def down
end
private
def get_project_ids
project_ids = Approver.where('target_type = ?', 'Project').pluck(:target_id)
project_ids += ApproverGroup.where('target_type = ?', 'Project').pluck(:target_id)
project_ids.uniq!
project_ids
end
end
......@@ -5,25 +5,13 @@ module Gitlab
class MigrateApproverToApprovalRulesInBatch
class MergeRequest < ActiveRecord::Base
self.table_name = 'merge_requests'
include ::EachBatch
end
def perform(target_type, target_ids)
target_ids.each do |target_id|
MigrateApproverToApprovalRules.new.perform(target_type, target_id)
def perform(start_id, end_id)
merge_request_ids = MergeRequest.where('id >= ? AND id <= ?', start_id, end_id).pluck(:id)
merge_request_ids.each do |merge_request_id|
MigrateApproverToApprovalRules.new.perform('MergeRequest', merge_request_id)
end
schedule_to_migrate_merge_requests(target_ids) if target_type == 'Project'
end
private
def schedule_to_migrate_merge_requests(project_ids)
jobs = []
MergeRequest.where(target_project_id: project_ids).each_batch do |scope, _|
jobs << [self.class.name, ['MergeRequest', scope.pluck(:id)]]
end
BackgroundMigrationWorker.bulk_perform_async(jobs) unless jobs.empty?
end
end
end
......
......@@ -5,37 +5,16 @@ require 'spec_helper'
describe Gitlab::BackgroundMigration::MigrateApproverToApprovalRulesInBatch do
context 'when there is no more MigrateApproverToApprovalRules jobs' do
let(:job) { double(:job) }
let(:project) { create(:project) }
it 'migrates individual target' do
allow(Gitlab::BackgroundMigration::MigrateApproverToApprovalRules).to receive(:new).and_return(job)
expect(job).to receive(:perform).exactly(3).times
described_class.new.perform('Foo', [1, 2, 3])
end
context 'when targets are projects' do
let(:projects) { create_list(:project, 3) }
merge_requests = create_list(:merge_request, 3)
context 'when projects contain merge requests' do
it 'schedules migrations for merge requests' do
merge_requests = projects.flat_map do |project|
create(:merge_request, source_project: project, target_project: project)
end
expect(BackgroundMigrationWorker).to receive(:bulk_perform_async).with([[described_class.name, ["MergeRequest", merge_requests.map(&:id)]]])
described_class.new.perform('Project', projects.map(&:id))
end
end
context 'when merge request do not exist' do
it 'does nothing' do
expect(BackgroundMigrationWorker).not_to receive(:bulk_perform_async)
expect(job).to receive(:perform).exactly(3).times
described_class.new.perform('Project', projects.map(&:id))
end
end
described_class.new.perform(merge_requests.first.id, merge_requests.last.id)
end
end
end
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