Ruby-on-rails 以编程方式获取 Resque 队列中的作业数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11235318/
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
Programmatically get the number of jobs in a Resque queue
提问by randombits
I am interested in setting up a monitoring service that will page me whenever there are too many jobs in the Resque queue (I have about 6 queues, I'll have different numbers for each queue). I also want to setup a very similar monitoring service that will alert me when I exceed a certain amount of failed jobs in my queue.
我有兴趣设置一个监控服务,只要 Resque 队列中有太多作业,它就会向我传呼(我有大约 6 个队列,每个队列都有不同的编号)。我还想设置一个非常相似的监控服务,当我的队列中的失败作业超过一定数量时,它会提醒我。
My question is, there is a lot of keys and confusion that I see affiliated with Resque on my redis server. I don't necessarily see a straight forward way to get a count of jobs per queue or the number of failed jobs. Is there currently a trivial way to grab this data from redis?
我的问题是,在我的 redis 服务器上,我看到有很多与 Resque 相关的密钥和混乱。我不一定看到获得每个队列的作业数或失败作业数的直接方法。目前是否有一种简单的方法可以从 redis 中获取这些数据?
回答by gef
yes it's quite easy, given you're using the Resque gem:
是的,这很容易,因为您使用的是Resque gem:
require 'resque'
Resque.info
will return a hash
将返回一个哈希
e.g/ =>
例如/ =>
{
:pending => 54338,
:processed => 12772,
:queues => 2,
:workers => 0,
:working => 0,
:failed => 8761,
:servers => [
[0] "redis://192.168.1.10:6379/0"
],
:environment => "development"
}
So to get the failed job count, simply use:
因此,要获得失败的作业计数,只需使用:
Resque.info[:failed]
which would give => 8761 #in my example
这会给 => 8761 #in 我的例子
To get the queues use:
要获取队列,请使用:
Resque.queues
this returns a array
这将返回一个数组
e.g./ =>
例如/ =>
[
[0] "superQ",
[1] "anotherQ"
]
You may then find the number of jobs per queue:
然后您可以找到每个队列的作业数:
Resque.size(queue_name)
e.g/ Resque.size("superQ")or Resque.size(Resque.queues[0]).....
例如/Resque.size("superQ")或 Resque.size(Resque.queues[0]).....
回答by The Who
Here is a bash script which will monitor the total number of jobs queued and the number of failed jobs.
这是一个 bash 脚本,它将监视排队的作业总数和失败的作业数。
while :
do
let sum=0
let errors=$(redis-cli llen resque:failed)
for s in $(redis-cli keys resque:queue:*)
do
let sum=$sum+$(redis-cli llen $s)
done
echo $sum jobs queued, with $errors errors
sleep 1 # sleep 1 second, probably want to increase this
done
This is for Resque 1.X, 2.0 might have different key names.
这是针对 Resque 1.X,2.0 可能有不同的键名。

