Ruby-on-rails 清除 sidekiq 队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23143766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:33:45  来源:igfitidea点击:

Clear sidekiq queue

ruby-on-railssidekiq

提问by Linus Oleander

I've this worker that runs for ever.

我有这个永远运行的工人。

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

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

The problem is that I load workers on start up, like this. config/initializers/load_workers.rb

问题是我在启动时加载工作人员,就像这样。 config/initializers/load_workers.rb

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

Using this to start sidekiq SIDEKIQ=1 sidekiq --verbose --environment production -C config/sidekiq.yml.

使用它来启动 sidekiq SIDEKIQ=1 sidekiq --verbose --environment production -C config/sidekiq.yml

This means that old workers as to stop, both those currently running but also the ones being rescheduled.

这意味着旧工人将停止,包括当前正在运行的工人以及被重新安排的工人。

I tried running this on start up (just before loading new works), but that didn't work.

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

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)

After 5-ish deploys there are bunch of duplicate workers in the queue scheduled for later. So, is there a way to clear everyting in one queue and prevent already running jobs from rescheduling?

在 5-ish 部署之后,队列中会有一堆重复的工作人员安排稍后使用。那么,有没有办法清除一个队列中的所有内容并防止重新安排已经运行的作业?

I'm using sidekiq 3.0.

我正在使用 sidekiq 3.0。

回答by Ranjithkumar Ravi

Deletes all Jobs in a Queue, by removing the queue.

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

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

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

回答by iGEL

This did the trick for me:

这对我有用:

Sidekiq::Queue.all.each(&:clear)
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
Sidekiq::DeadSet.new.clear

回答by rusllonrails

Works for me for most sidekiq versions:

适用于大多数 sidekiq 版本:

Sidekiq::RetrySet.new.clear

Sidekiq::ScheduledSet.new.clear

Clear statistics (Optional)

清除统计信息(可选)

Sidekiq::Stats.new.reset

回答by Asad Hameed

You can clear your queue by running this code although there would be built-in methods.

尽管会有内置方法,但您可以通过运行此代码来清除队列。

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