Skip to content

Commit

Permalink
Implement separate long build queues
Browse files Browse the repository at this point in the history
This implements a separate queue for long-running builds that are
identified by a tag `-long` in the runner name.
  • Loading branch information
carlocab committed May 2, 2024
1 parent 42863f2 commit ea7b4f7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# Information representing a CI job.
class Job
NAME_REGEX =
/\A(?<runner>\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?<run_id>\d+)(?:-(?<run_attempt>\d+))?(?:-(?<tag>[a-z]+))?\z/
/\A(?<runner>\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?<run_id>\d+)(?:-(?<run_attempt>\d+))?(?:-(?<tags>[-a-z]+))?\z/

attr_reader :runner_name, :repository, :github_id, :secret
attr_reader :runner_name, :repository, :github_id, :secret, :type
attr_writer :orka_setup_timeout
attr_accessor :github_state, :orka_vm_id, :orka_setup_time, :orka_start_attempts, :runner_completion_time

Expand All @@ -26,6 +26,7 @@ def initialize(runner_name, repository, github_id, secret: nil)
@orka_start_attempts = 0
@secret = secret || SecureRandom.hex(32)
@runner_completion_time = nil
@type = long_build? ? :long : :standard
end

def os
Expand All @@ -44,8 +45,12 @@ def run_attempt
@runner_name[NAME_REGEX, :run_attempt]
end

def tag
@runner_name[NAME_REGEX, :tag]
def tags
@runner_name[NAME_REGEX, :tags]&.split("-")
end

def long_build?
tags&.include?("long")
end

def runner_labels
Expand Down
29 changes: 29 additions & 0 deletions src/job_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative "shared_state"

class JobQueue
def initialize
@mutex = Mutex.new
@queue = Hash.new { |h,k| h[k] = Queue.new }
@state = SharedState.instance
end

def <<(job)
@mutex.synchronize do
@queue[job.type] << job
end
end

def pop
@mutex.synchronize do
running_long_build_count = @state.running_jobs
.count { |job| job.type == :long }
if running_long_build_count < 2
@queue[:long].pop
else
@queue[:standard].pop
end
end
end
end
4 changes: 4 additions & 0 deletions src/shared_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def free_slot?(waiting_job)
@jobs.count { |job| job.queue_type == waiting_job.queue_type && !job.orka_vm_id.nil? } < max_slots
end

def running_jobs
jobs.reject { |j| j.github_state == :queued || j.github_state == :completed }
end

private

def load_pause_data(pause_data)
Expand Down

0 comments on commit ea7b4f7

Please sign in to comment.