Commit d845d80e authored by Maxime Orefice's avatar Maxime Orefice Committed by Grzegorz Bizon

Denormalize shared runners to CI::PendingBuild

parent 7883a37d
...@@ -11,11 +11,48 @@ module Ci ...@@ -11,11 +11,48 @@ module Ci
scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) } scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) }
def self.upsert_from_build!(build) def self.upsert_from_build!(build)
entry = self.new(build: build, project: build.project, protected: build.protected?) entry = self.new(args_from_build(build))
entry.validate! entry.validate!
self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id) self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
end end
def self.args_from_build(build)
args = {
build: build,
project: build.project,
protected: build.protected?
}
if Feature.enabled?(:ci_pending_builds_maintain_shared_runners_data, type: :development, default_enabled: :yaml)
args.merge(instance_runners_enabled: shareable?(build))
else
args
end
end
private_class_method :args_from_build
def self.shareable?(build)
shared_runner_enabled?(build) &&
builds_access_level?(build) &&
project_not_removed?(build)
end
private_class_method :shareable?
def self.shared_runner_enabled?(build)
build.project.shared_runners.exists?
end
private_class_method :shared_runner_enabled?
def self.project_not_removed?(build)
!build.project.pending_delete?
end
private_class_method :project_not_removed?
def self.builds_access_level?(build)
build.project.project_feature.builds_access_level.nil? || build.project.project_feature.builds_access_level > 0
end
private_class_method :builds_access_level?
end end
end end
---
name: ci_pending_builds_maintain_shared_runners_data
introduced_by_url:
rollout_issue_url:
milestone: '14.1'
type: development
group: group::pipeline execution
default_enabled: false
# frozen_string_literal: true
class AddInstanceRunnersEnabledToCiPendingBuild < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
def up
with_lock_retries do
add_column :ci_pending_builds, :instance_runners_enabled, :boolean, null: false, default: false
end
end
def down
with_lock_retries do
remove_column :ci_pending_builds, :instance_runners_enabled
end
end
end
9ba27b5e2599262846a06736db72fb0d31dc904e2ef4d167c1ee9530feb6367f
\ No newline at end of file
...@@ -10833,7 +10833,8 @@ CREATE TABLE ci_pending_builds ( ...@@ -10833,7 +10833,8 @@ CREATE TABLE ci_pending_builds (
build_id bigint NOT NULL, build_id bigint NOT NULL,
project_id bigint NOT NULL, project_id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL,
protected boolean DEFAULT false NOT NULL protected boolean DEFAULT false NOT NULL,
instance_runners_enabled boolean DEFAULT false NOT NULL
); );
CREATE SEQUENCE ci_pending_builds_id_seq CREATE SEQUENCE ci_pending_builds_id_seq
...@@ -5,5 +5,6 @@ FactoryBot.define do ...@@ -5,5 +5,6 @@ FactoryBot.define do
build factory: :ci_build build factory: :ci_build
project project
protected { build.protected } protected { build.protected }
instance_runners_enabled { true }
end end
end end
...@@ -29,5 +29,61 @@ RSpec.describe Ci::PendingBuild do ...@@ -29,5 +29,61 @@ RSpec.describe Ci::PendingBuild do
expect(result.rows.dig(0, 0)).to eq build.id expect(result.rows.dig(0, 0)).to eq build.id
end end
end end
context 'when project does not have shared runner' do
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
context 'when project has shared runner' do
let_it_be(:runner) { create(:ci_runner, :instance) }
context 'when ci_pending_builds_maintain_shared_runners_data is enabled' do
it 'sets instance_runners_enabled to true' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_truthy
end
context 'when project is about to be deleted' do
before do
build.project.update!(pending_delete: true)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
context 'when builds are disabled' do
before do
build.project.project_feature.update!(builds_access_level: false)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
end
context 'when ci_pending_builds_maintain_shared_runners_data is disabled' do
before do
stub_feature_flags(ci_pending_builds_maintain_shared_runners_data: false)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
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