2020-03-03 10:08:08 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class SidekiqQueue
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
NoMetadataError = Class.new(StandardError)
|
|
|
|
InvalidQueueError = Class.new(StandardError)
|
|
|
|
|
2021-09-14 05:11:32 -04:00
|
|
|
WORKER_KEY = 'worker_class'
|
2022-02-07 10:15:53 -05:00
|
|
|
ALLOWED_KEYS = Gitlab::ApplicationContext.known_keys.map(&:to_s) + [WORKER_KEY]
|
2021-09-14 05:11:32 -04:00
|
|
|
|
2020-03-03 10:08:08 -05:00
|
|
|
attr_reader :queue_name
|
|
|
|
|
|
|
|
def initialize(queue_name)
|
|
|
|
@queue_name = queue_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def drop_jobs!(search_metadata, timeout:)
|
2021-06-23 20:07:35 -04:00
|
|
|
start_time = monotonic_time
|
2020-03-04 07:07:52 -05:00
|
|
|
completed = true
|
2020-03-03 10:08:08 -05:00
|
|
|
deleted_jobs = 0
|
|
|
|
|
|
|
|
job_search_metadata =
|
|
|
|
search_metadata
|
|
|
|
.stringify_keys
|
2021-09-14 05:11:32 -04:00
|
|
|
.slice(*ALLOWED_KEYS)
|
|
|
|
.transform_keys(&method(:transform_key))
|
2020-03-03 10:08:08 -05:00
|
|
|
.compact
|
|
|
|
|
|
|
|
raise NoMetadataError if job_search_metadata.empty?
|
|
|
|
raise InvalidQueueError unless queue
|
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
queue.each do |job|
|
|
|
|
if timeout_exceeded?(start_time, timeout)
|
|
|
|
completed = false
|
|
|
|
break
|
|
|
|
end
|
2020-03-03 10:08:08 -05:00
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
next unless job_matches?(job, job_search_metadata)
|
2020-03-03 10:08:08 -05:00
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
job.delete
|
|
|
|
deleted_jobs += 1
|
2020-03-03 10:08:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
completed: completed,
|
|
|
|
deleted_jobs: deleted_jobs,
|
|
|
|
queue_size: queue.size
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
private
|
|
|
|
|
2021-09-14 05:11:32 -04:00
|
|
|
def transform_key(key)
|
2022-02-07 10:15:53 -05:00
|
|
|
if Gitlab::ApplicationContext.known_keys.include?(key.to_sym)
|
2021-09-14 05:11:32 -04:00
|
|
|
"meta.#{key}"
|
|
|
|
elsif key == WORKER_KEY
|
|
|
|
'class'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-03 10:08:08 -05:00
|
|
|
def queue
|
|
|
|
strong_memoize(:queue) do
|
|
|
|
# Sidekiq::Queue.new always returns a queue, even if it doesn't
|
|
|
|
# exist.
|
|
|
|
Sidekiq::Queue.all.find { |queue| queue.name == queue_name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_matches?(job, job_search_metadata)
|
|
|
|
job_search_metadata.all? { |key, value| job[key] == value }
|
|
|
|
end
|
2020-03-04 07:07:52 -05:00
|
|
|
|
|
|
|
def timeout_exceeded?(start_time, timeout)
|
2021-06-23 20:07:35 -04:00
|
|
|
(monotonic_time - start_time) > timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
def monotonic_time
|
|
|
|
Gitlab::Metrics::System.monotonic_time
|
2020-03-04 07:07:52 -05:00
|
|
|
end
|
2020-03-03 10:08:08 -05:00
|
|
|
end
|
|
|
|
end
|