diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 30f5a2ee092..4f96f4f3645 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -124,19 +124,30 @@ module Ci ] end - def tick_update + def tick_runner_queue new_update = Time.new.inspect - Gitlab::Redis.with { |redis| redis.set(redis_key, new_update, ex: 60.minutes) } + Gitlab::Redis.with { |redis| redis.set(runner_queue_key, new_update, ex: 60.minutes) } new_update end - def last_build_queue_update - Gitlab::Redis.with { |redis| redis.get(redis_key) } + def ensure_runner_queue_value + Gitlab::Redis.with do |redis| + value = redis.get(runner_queue_key) + if value == "" + value = Time.new.inspect + redis.set(runner_queue_key, value, ex: 60.minutes) + end + value + end + end + + def is_runner_queue_value_latest?(value) + ensure_runner_queue_value == value if value.present? end private - def redis_key + def runner_queue_key "runner:build_queue:#{self.id}" end diff --git a/app/services/ci/update_build_queue_service.rb b/app/services/ci/update_build_queue_service.rb index 348ba6f5cc4..dd1097f5dbe 100644 --- a/app/services/ci/update_build_queue_service.rb +++ b/app/services/ci/update_build_queue_service.rb @@ -1,6 +1,5 @@ module Ci class UpdateBuildQueueService < BaseService - def execute(build) build.project.runners.select do |runner| if runner.can_pick?(build) diff --git a/app/workers/build_queue_worker.rb b/app/workers/build_queue_worker.rb index 8cc99eabdfe..c0ceb8d423a 100644 --- a/app/workers/build_queue_worker.rb +++ b/app/workers/build_queue_worker.rb @@ -4,8 +4,7 @@ class BuildQueueWorker def perform(build_id) Ci::Build.find_by(id: build_id).try do |build| - UpdateBuildQueueService.new(build) + UpdateBuildQueueService.execute(build) end end - end diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb index fb14f043a18..b1b66313092 100644 --- a/lib/ci/api/builds.rb +++ b/lib/ci/api/builds.rb @@ -16,13 +16,9 @@ module Ci not_found! unless current_runner.active? update_runner_info - last_update = current_runner.last_build_queue_update - - if params[:last_update].present? - if params[:last_update] == last_update - headers 'X-GitLab-Last-Update', last_update - return build_not_found! - end + if current_runner.is_runner_queue_value_latest?(params[:last_update]) + headers 'X-GitLab-Last-Update', params[:last_update] + return build_not_found! end build = Ci::RegisterBuildService.new.execute(current_runner) @@ -35,7 +31,7 @@ module Ci else Gitlab::Metrics.add_event(:build_not_found) - new_update = current_runner.tick_update + new_update = current_runner.ensure_runner_queue_value headers 'X-GitLab-Last-Update', new_update build_not_found!