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