清除 sidkiq 队列

我有一个永远在跑的工人。

class Worker
include Sidekiq::Worker
sidekiq_options queue: "infinity", retry: true


def perform(params)
# ...
self.class.perform_in(30.seconds, params)
end
end

问题是我在启动时加载工人,像这样

Rails.application.config.after_initialize do
if ENV["SIDEKIQ"] == "1"
Worker.perform_async({})
end
end

使用此命令启动 sidekiq SIDEKIQ=1 sidekiq --verbose --environment production -C config/sidekiq.yml

这意味着旧的工人停止,无论是那些目前正在运行,而且那些正在重新安排。

我尝试在启动时运行它(就在加载新作品之前) ,但是不起作用。

q = []
q += Sidekiq::RetrySet.new.select { |job| job.klass.match(/Worker/) }
q += Sidekiq::Queue.new("infinity").select { |job| job.klass.match(/Worker/) }
q += Sidekiq::ScheduledSet.new.select { |job| job.klass.match(/Worker/) }
q.each(&:delete)

在5-ish 部署之后,队列中有一大堆重复的工作者,这些工作者将安排在以后。那么,是否有办法清除一个队列中的所有内容,并防止已经运行的作业重新调度?

I'm using sidekiq 3.0.

105985 次浏览

通过删除队列删除队列中的所有作业。

require 'sidekiq/api' # for the case of rails console


Sidekiq::Queue.new("infinity").clear
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear

对于我来说,大多数 sidekiq 版本都适用:

Sidekiq::RetrySet.new.clear


Sidekiq::ScheduledSet.new.clear

清晰的统计数据(可选)

Sidekiq::Stats.new.reset

This will clear all queues, schedules and retries:

require 'sidekiq/api'
Sidekiq::Queue.all.each(&:clear)
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
Sidekiq::DeadSet.new.clear

可以通过运行此代码来清除队列,尽管可能存在内置方法。

queue = Sidekiq::Queue.new
queue.each do |job|
job.delete
end

还有一种更方便的方法来清除本地 env 的所有 Sidekiq 队列和集: Sidekiq.redis(&:flushdb)

Basically, it just flushes Redis that is configured for Sidekiq, so I would avoid using it in a non-local environment, as it can also remove some data you store in Redis. Anyways, it can be useful for the development or when you have a separate Redis instance for Sidekiq. And it's just one line instead of four.