Commit 7017889d authored by Dmitry Chepurovskiy's avatar Dmitry Chepurovskiy Committed by Nick Thomas

Added filtering jobs by age to jobs/request API endpoint

parent 7115bd81
...@@ -140,6 +140,8 @@ module Ci ...@@ -140,6 +140,8 @@ module Ci
where("EXISTS (?)", matcher) where("EXISTS (?)", matcher)
end end
scope :queued_before, ->(time) { where(arel_table[:queued_at].lt(time)) }
## ##
# TODO: Remove these mounters when we remove :ci_enable_legacy_artifacts feature flag # TODO: Remove these mounters when we remove :ci_enable_legacy_artifacts feature flag
mount_uploader :legacy_artifacts_file, LegacyArtifactUploader, mount_on: :artifacts_file mount_uploader :legacy_artifacts_file, LegacyArtifactUploader, mount_on: :artifacts_file
......
...@@ -36,6 +36,11 @@ module Ci ...@@ -36,6 +36,11 @@ module Ci
builds = builds.with_any_tags builds = builds.with_any_tags
end end
# pick builds that older than specified age
if params.key?(:job_age)
builds = builds.queued_before(params[:job_age].seconds.ago)
end
builds.each do |build| builds.each do |build|
next unless runner.can_pick?(build) next unless runner.can_pick?(build)
......
---
title: "Added option to filter jobs by age in the /job/request API endpoint."
merge_request: 1340
author: Dmitry Chepurovskiy
type: added
# frozen_string_literal: true
# This migration make queued_at field indexed to speed up builds filtering by job_age
class AddBuildQueuedAtIndex < ActiveRecord::Migration[5.1]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :ci_builds, :queued_at
end
def down
remove_concurrent_index :ci_builds, :queued_at
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190515125613) do ActiveRecord::Schema.define(version: 20190516011213) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -380,6 +380,7 @@ ActiveRecord::Schema.define(version: 20190515125613) do ...@@ -380,6 +380,7 @@ ActiveRecord::Schema.define(version: 20190515125613) do
t.index ["project_id", "id"], name: "index_ci_builds_on_project_id_and_id", using: :btree t.index ["project_id", "id"], name: "index_ci_builds_on_project_id_and_id", using: :btree
t.index ["project_id", "status"], name: "index_ci_builds_project_id_and_status_for_live_jobs_partial2", where: "(((type)::text = 'Ci::Build'::text) AND ((status)::text = ANY (ARRAY[('running'::character varying)::text, ('pending'::character varying)::text, ('created'::character varying)::text])))", using: :btree t.index ["project_id", "status"], name: "index_ci_builds_project_id_and_status_for_live_jobs_partial2", where: "(((type)::text = 'Ci::Build'::text) AND ((status)::text = ANY (ARRAY[('running'::character varying)::text, ('pending'::character varying)::text, ('created'::character varying)::text])))", using: :btree
t.index ["protected"], name: "index_ci_builds_on_protected", using: :btree t.index ["protected"], name: "index_ci_builds_on_protected", using: :btree
t.index ["queued_at"], name: "index_ci_builds_on_queued_at", using: :btree
t.index ["runner_id"], name: "index_ci_builds_on_runner_id", using: :btree t.index ["runner_id"], name: "index_ci_builds_on_runner_id", using: :btree
t.index ["scheduled_at"], name: "partial_index_ci_builds_on_scheduled_at_with_scheduled_jobs", where: "((scheduled_at IS NOT NULL) AND ((type)::text = 'Ci::Build'::text) AND ((status)::text = 'scheduled'::text))", using: :btree t.index ["scheduled_at"], name: "partial_index_ci_builds_on_scheduled_at_with_scheduled_jobs", where: "((scheduled_at IS NOT NULL) AND ((type)::text = 'Ci::Build'::text) AND ((status)::text = 'scheduled'::text))", using: :btree
t.index ["stage_id", "stage_idx"], name: "tmp_build_stage_position_index", where: "(stage_idx IS NOT NULL)", using: :btree t.index ["stage_id", "stage_idx"], name: "tmp_build_stage_position_index", where: "(stage_idx IS NOT NULL)", using: :btree
......
...@@ -98,6 +98,7 @@ module API ...@@ -98,6 +98,7 @@ module API
optional :certificate, type: String, desc: %q(Session's certificate) optional :certificate, type: String, desc: %q(Session's certificate)
optional :authorization, type: String, desc: %q(Session's authorization) optional :authorization, type: String, desc: %q(Session's authorization)
end end
optional :job_age, type: Integer, desc: %q(Job should be older than passed age in seconds to be ran on runner)
end end
post '/request' do post '/request' do
authenticate_runner! authenticate_runner!
......
...@@ -542,6 +542,30 @@ describe API::Runner, :clean_gitlab_redis_shared_state do ...@@ -542,6 +542,30 @@ describe API::Runner, :clean_gitlab_redis_shared_state do
end end
end end
context 'when job filtered by job_age' do
let!(:job) { create(:ci_build, :tag, pipeline: pipeline, name: 'spinach', stage: 'test', stage_idx: 0, queued_at: 60.seconds.ago) }
context 'job is queued less than job_age parameter' do
let(:job_age) { 120 }
it 'gives 204' do
request_job(job_age: job_age)
expect(response).to have_gitlab_http_status(204)
end
end
context 'job is queued more than job_age parameter' do
let(:job_age) { 30 }
it 'picks a job' do
request_job(job_age: job_age)
expect(response).to have_gitlab_http_status(201)
end
end
end
context 'when job is made for branch' do context 'when job is made for branch' do
it 'sets tag as ref_type' do it 'sets tag as ref_type' do
request_job request_job
......
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