web-dev-qa-db-ja.com

キュー全体をクリアせずに、Resqueキューから特定のジョブを削除するにはどうすればよいですか?

Resqueワーカーを使用してキュー内のジョブを処理しています。キュー内に100万を超える多数のジョブがあり、削除する必要のあるジョブがいくつかあります(エラーによって追加されました)。ジョブを使用してキューを作成するのは簡単な作業ではなかったため、resque-webを使用してキューをクリアし、正しいジョブを再度追加することは、私にとって選択肢ではありません。

アドバイスに感謝します。ありがとう!

27
Rami

Resqueのソース(ジョブクラス)にはそのようなメソッドがあります、それがあなたが必要としているものだと思います:)

# Removes a job from a queue. Expects a string queue name, a
# string class name, and, optionally, args.
#
# Returns the number of jobs destroyed.
#
# If no args are provided, it will remove all jobs of the class
# provided.
#
# That is, for these two jobs:
#
# { 'class' => 'UpdateGraph', 'args' => ['defunkt'] }
# { 'class' => 'UpdateGraph', 'args' => ['mojombo'] }
#
# The following call will remove both:
#
#   Resque::Job.destroy(queue, 'UpdateGraph')
#
# Whereas specifying args will only remove the 2nd job:
#
#   Resque::Job.destroy(queue, 'UpdateGraph', 'mojombo')
#
# This method can be potentially very slow and memory intensive,
# depending on the size of your queue, as it loads all jobs into
# a Ruby array before processing.
def self.destroy(queue, klass, *args)
22
DeTeam

キューから特定のジョブを削除するには、destroyメソッドを使用できます。使い方はとても簡単です。たとえば、queue1という名前のキューにあるPostクラスとIDxのジョブを削除する場合は次のようにできます。

Resque::Job.destroy(queue1, Post, 'x')

特定のタイプのすべてのジョブをキューから削除する場合は、次を使用できます。

Resque::Job.destroy(QueueName, ClassName) 

あなたはそれのドキュメントを見つけることができます

http://www.rubydoc.info/gems/resque/Resque%2FJob.destroy

21
Puneeth

上記の解決策は、ジョブに渡されるすべての引数を知っている場合にうまく機能します。ジョブに渡された引数のsomeがわかっている状況がある場合は、次のスクリプトが機能します。

queue_name = 'a_queue'
jobs = Resque.data_store.peek_in_queue(queue_name, 0, 500_000);
deleted_count = 0

jobs.each do |job|
  decoded_job = Resque.decode(job)
  if decoded_job['class'] == 'CoolJob' && decoded_job['args'].include?('a_job_argument')
    Resque.data_store.remove_from_queue(queue_name, job)
    deleted_count += 1
    puts "Deleted!"
  end
end

puts deleted_count
4
iloveitaly