Commit 371a0c32 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add pending builds model and implement transitions

This commit adds a pending build model that will create a resource in
the database when a build transitions to the pending state. The resource
will be removed when an associated build gets picked by a runner.
parent aec75eab
......@@ -295,11 +295,18 @@ module Ci
end
after_transition any => [:pending] do |build|
Ci::UpdateBuildQueueService.queue_push!(build)
build.run_after_commit do
BuildQueueWorker.perform_async(id)
end
end
after transition pending: any do |build|
# TODO ensure that there is no race condition here, add test for this
Ci::UpdateBuildQueueService.queue_pop!(build)
end
after_transition pending: :running do |build|
build.deployment&.run
......
# frozen_string_literal: true
module Ci
class PendingBuild < ApplicationRecord
extend Gitlab::Ci::Model
belongs_to :project
belongs_to :build, class_name: 'Ci::Build'
end
end
......@@ -2,12 +2,44 @@
module Ci
class UpdateBuildQueueService
##
# Add a build to the pending builds queue
#
def queue_push!(build, metrics = ::Gitlab::Ci::Queue::Metrics)
in_transaction do
::Ci::PendingBuild.create!(build: build, project: project)
# TODO increment pending builds counter
end
end
##
# Remove a build from the pending builds queue
#
def queue_pop!(build, metrics = ::Gitlab::Ci::Queue::Metrics)
in_transaction do
::Ci::PendingBuild.find(build.id).destroy!
# TODO decrement pending builds counter
end
end
##
# Unblock runner associated with given project / build
#
def execute(build, metrics = ::Gitlab::Ci::Queue::Metrics)
tick_for(build, build.project.all_runners, metrics)
end
private
def in_transaction
# TODO ensure that state machine transition transaction is open
#
yield
end
def tick_for(build, runners, metrics)
runners = runners.with_recent_runner_queue
runners = runners.with_tags if Feature.enabled?(:ci_preload_runner_tags, default_enabled: :yaml)
......
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