103

我习惯于使用delayed_jobs 方法进入控制台查看队列中的内容,以及在需要时轻松清除队列。Sidekiq 中是否有类似的命令?谢谢!

4

9 回答 9

151

有一个符合人体工程学的API 用于查看和管理队列

默认情况下不需要。

require 'sidekiq/api'

这是摘录:

# get a handle to the default queue
default_queue = Sidekiq::Queue.new 

# get a handle to the mailer queue
mailer_queue = Sidekiq::Queue.new("mailer") 

# How many jobs are in the default queue?
default_queue.size # => 1001

# How many jobs are in the mailer queue?
mailer_queue.size # => 50

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

您还可以获得一些汇总统计信息。

stats = Sidekiq::Stats.new

# Get the number of jobs that have been processed.
stats.processed # => 100

# Get the number of jobs that have failed.    
stats.failed # => 3

# Get the queues with name and number enqueued.
stats.queues # => { "default" => 1001, "email" => 50 }

#Gets the number of jobs enqueued in all queues (does NOT include retries and scheduled jobs).
stats.enqueued # => 1051 
于 2013-09-21T00:21:21.307 回答
93

我从未使用过 Sidekiq,因此可能有一些方法仅用于查看排队的作业,但它们实际上只是 Redis 命令的包装器,因为这基本上是 Sidekiq(和 Resque)的全部内容:

# See workers
Sidekiq::Client.registered_workers

# See queues
Sidekiq::Client.registered_queues

# See all jobs for one queue
Sidekiq.redis { |r| r.lrange "queue:app_queue", 0, -1 }

# See all jobs in all queues
Sidekiq::Client.registered_queues.each do |q|
  Sidekiq.redis { |r| r.lrange "queue:#{q}", 0, -1 }
end

# Remove a queue and all of its jobs
Sidekiq.redis do |r| 
  r.srem "queues", "app_queue"
  r.del  "queue:app_queue"
end

不幸的是,删除一个特定的工作有点困难,因为你必须复制它的确切值:

# Remove a specific job from a queue
Sidekiq.redis { |r| r.lrem "queue:app_queue", -1, "the payload string stored in Redis" }

您可以通过以下方式更轻松地完成所有这些操作redis-cli

$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queues:app_queue 0 -1
> lrem queues:app_queue -1 "payload"
于 2012-10-02T05:25:32.680 回答
10

如果有任何预定的工作。您可以使用以下命令删除所有作业:

Sidekiq::ScheduledSet.new.clear

如果有任何队列要删除所有作业,可以使用以下命令:

  Sidekiq::Queue.new.clear

重试作业也可以通过以下命令删除:

Sidekiq::RetrySet.new.clear

以下链接有更多信息,您可以查看: https ://github.com/mperham/sidekiq/wiki/API

于 2014-10-18T03:19:33.370 回答
8

有一个 API 用于访问有关工作人员、队列和作业的实时信息。
访问这里https://github.com/mperham/sidekiq/wiki/API

于 2013-10-24T07:51:36.100 回答
2

一种解决方法是使用测试模块(需要'sidekiq/testing')并耗尽工作人员(MyWorker.drain)。

于 2012-12-13T20:16:12.083 回答
2

默认队列中有被绞死的“工人”,我可以通过网络界面看到他们。但是如果我使用 Sidekiq::Queue.new.size,它们就不能从控制台获得

irb(main):002:0> Sidekiq::Queue.new.size
2014-03-04T14:37:43Z 17256 TID-oujb9c974 INFO: Sidekiq client with redis options {:namespace=>"sidekiq_staging"}
=> 0

使用 redis-cli 我能够找到它们

redis 127.0.0.1:6379> keys *
    1) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:7635c39a29d7b255b564970bea51c026-69853672483440:default"
    2) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:0cf585f5e93e1850eee1ae4613a08e45-70328697677500:default:started"
    3) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:7635c39a29d7b255b564970bea51c026-69853672320140:default:started"
    ...

解决方案是:

irb(main):003:0>  Sidekiq.redis { |r| r.del "workers", 0, -1 }
=> 1

在 Sidekiq v3 中也有一个命令

Sidekiq::Workers.new.prune

但由于某种原因,那天它对我不起作用

于 2014-03-04T14:52:40.123 回答
2

如果你想清除 sidekiq 重试队列,是这样的:Sidekiq::RetrySet.new.clear

于 2014-05-10T04:02:18.640 回答
2
$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queue:queue_name 0 -1 # (queue_name must be your relevant queue)
> lrem queue:queue_name -1 "payload"
于 2016-06-24T06:35:33.560 回答
1

清除所有 sidekiq 队列的 Rake 任务:

namespace :sidekiq do
  desc 'Clear sidekiq queue'
  task clear: :environment do
    require 'sidekiq/api'
    Sidekiq::Queue.all.each(&:clear)
  end
end

用法:

rake sidekiq:clear
于 2015-08-26T13:36:33.277 回答