0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sidekiqのジョブステータスをチェックする

Posted at

概要

Sidekiqにキューされていたらperformしたくないときに使った

コード

class User < ApplicationRecord
  def scheduled?
    Sidekiq::ScheduledSet.new.select{|q| q.args.first['queue_name'] == "hogehoge"}.select{|q| q.args.first['arguments'].first == self.id}.any?
  end

  def queued?
    Sidekiq::Queue.new('hogehoge').select{|q| q.args.first['arguments'].first == self.id}.any?
  end

  def running?
    Sidekiq::Workers.new.select{|process_id, thread_id, job|
      JSON.parse(job['payload'])['args'].first['queue_name'] == "hogehoge"
    }.select{|process_id, thread_id, job|
      JSON.parse(job['payload'])['args'].first['arguments'].first == self.id
    }.any?
  end
end

Tips

  • queue_nameと引数で判定している
  • Jobの第一引数にモデルのidを指定しているので、それで判別
  • 同じようにdeadやretryのものも取得できるはず
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?