diff --git a/src/job.rb b/src/job.rb index de2bd2b..568d0d9 100644 --- a/src/job.rb +++ b/src/job.rb @@ -7,9 +7,9 @@ # Information representing a CI job. class Job NAME_REGEX = - /\A(?\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?\d+)(?:-(?\d+))?(?:-(?[a-z]+))?\z/ + /\A(?\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?\d+)(?:-(?\d+))?(?:-(?[-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 @@ -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 @@ -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 diff --git a/src/job_queue.rb b/src/job_queue.rb new file mode 100644 index 0000000..f5040bd --- /dev/null +++ b/src/job_queue.rb @@ -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 diff --git a/src/shared_state.rb b/src/shared_state.rb index 6ef7f21..d0e0870 100644 --- a/src/shared_state.rb +++ b/src/shared_state.rb @@ -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)