debian-mirror-gitlab/lib/gitlab/sidekiq_queue.rb

73 lines
1.6 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module Gitlab
class SidekiqQueue
include Gitlab::Utils::StrongMemoize
NoMetadataError = Class.new(StandardError)
InvalidQueueError = Class.new(StandardError)
attr_reader :queue_name
def initialize(queue_name)
@queue_name = queue_name
end
def drop_jobs!(search_metadata, timeout:)
2021-09-30 23:02:18 +05:30
start_time = monotonic_time
2020-04-08 14:13:33 +05:30
completed = true
deleted_jobs = 0
job_search_metadata =
search_metadata
.stringify_keys
2021-04-29 21:17:54 +05:30
.slice(*Gitlab::ApplicationContext::KNOWN_KEYS)
2020-04-08 14:13:33 +05:30
.transform_keys { |key| "meta.#{key}" }
.compact
raise NoMetadataError if job_search_metadata.empty?
raise InvalidQueueError unless queue
queue.each do |job|
if timeout_exceeded?(start_time, timeout)
completed = false
break
end
next unless job_matches?(job, job_search_metadata)
job.delete
deleted_jobs += 1
end
{
completed: completed,
deleted_jobs: deleted_jobs,
queue_size: queue.size
}
end
private
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
def timeout_exceeded?(start_time, timeout)
2021-09-30 23:02:18 +05:30
(monotonic_time - start_time) > timeout
end
def monotonic_time
Gitlab::Metrics::System.monotonic_time
2020-04-08 14:13:33 +05:30
end
end
end