Home > OS >  How to query Sidekiq queues by Job class, queue name and arguments?
How to query Sidekiq queues by Job class, queue name and arguments?

Time:10-19

I have to check if the same job has already been added to the queue before enqueue again.

Currently I use this code but it's very inefficient because it loads all object into memory. Is there a way to query like a normal active record?

Ideally, I would something like

Sidekiq::Queue.where(job_class: "SyncJob", queue_name:"high", arguments: [1,2,3])

Current code:

def list_queued_jobs(job_class, queue_name,  arguments)
  found_jobs = []
  queues = Sidekiq::Queue.all
  queues.each do |queue|
    queue.each do |job|
      job.args.each do |arg|
        if arg['job_class'].to_s == job_class.to_s && arg['queue_name'] == queue_name && ActiveJob::Arguments.deserialize(arg['arguments']) == arguments
          found_jobs << job
        end
      end
    end
  end
  return found_jobs
end

CodePudding user response:

There is no out-of-the-box solution, but there is a simpler way to determine if a job's been already queued (true/false):

def in_queues?(job_class, queue_name,  arguments = [])
  Sidekiq::Queue.new(queue_name).any? do |job|
    job_class == job.klass && job.args == arguments
  end
end

Keep in mind that sidekiq arguments are presented as an array. For example, if you call your job like:

SyncJob.perform_async([1, 2, 3])

then you check that it's been queued as follows:

in_queues?("SyncJob", "high", [[1, 2, 3]])
  • Related