Ruby-on-rails 如何清除卡住/陈旧的 Resque 工人?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7416318/
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
How do I clear stuck/stale Resque workers?
提问by Shpigford
As you can see from the attached image, I've got a couple of workers that seem to be stuck. Those processes shouldn't take longer than a couple of seconds.
正如您从附加图片中看到的,我有几个工人似乎被卡住了。这些过程不应超过几秒钟。


I'm not sure why they won't clear or how to manually remove them.
我不确定为什么他们不会清除或如何手动删除它们。
I'm on Heroku using Resque with Redis-to-Go and HireFire to automatically scale workers.
我在 Heroku 上使用带有 Redis-to-Go 和 HireFire 的 Resque 来自动扩展工作人员。
回答by hagope
None of these solutions worked for me, I would still see this in redis-web:
这些解决方案都不适合我,我仍然会在 redis-web 中看到这一点:
0 out of 10 Workers Working
Finally, this worked for me to clear all the workers:
最后,这对我有用以清除所有工人:
Resque.workers.each {|w| w.unregister_worker}
回答by Simpleton
In your console:
在您的控制台中:
queue_name = "process_numbers"
Resque.redis.del "queue:#{queue_name}"
Otherwise you can try to fake them as being done to remove them, with:
否则,您可以尝试将它们伪装为已完成删除它们,使用:
Resque::Worker.working.each {|w| w.done_working}
EDIT
编辑
A lot of people have been upvoting this answer and I feel that it's important that people try hagope's solution which unregisters workers off a queue, whereas the above code deletes queues. If you're happy to fake them, then cool.
很多人一直在支持这个答案,我觉得人们尝试 hagope 的解决方案很重要,该解决方案将工作人员从队列中注销,而上述代码删除队列。如果你乐于伪造它们,那就太酷了。
回答by Shairon Toledo
You probably have the resque gem installed, so you can open the console and get current workers
您可能安装了 resque gem,因此您可以打开控制台并获取当前工作人员
Resque.workers
It returns a list of workers
它返回一个工人列表
#=> [#<Worker infusion.local:40194-0:JAVA_DYNAMIC_QUEUES,index_migrator,converter,extractor>]
pick the worker and prune_dead_workers, for example the first one
选择工人prune_dead_workers,例如第一个
Resque.workers.first.prune_dead_workers
回答by ewH
Adding to answer by hagope, I wanted to be able to only unregister workers that had been running for a certain amount of time. The code below will only unregister workers running for over 300 seconds (5 minutes).
添加到 hagope 的回答中,我希望能够只取消注册已经运行了一定时间的工人。下面的代码只会注销运行超过 300 秒(5 分钟)的工人。
Resque.workers.each {|w| w.unregister_worker if w.processing['run_at'] && Time.now - w.processing['run_at'].to_time > 300}
I have an ongoing collection of Resque related Rake tasks that I have also added this to: https://gist.github.com/ewherrmann/8809350
我有一个与 Resque 相关的 Rake 任务的持续收集,我也将其添加到:https: //gist.github.com/ewherrmann/8809350
回答by jBeas
Run this command wherever you ran the command to start the server
在您运行该命令以启动服务器的任何位置运行此命令
$ ps -e -o pid,command | grep [r]esque
you should see something like this:
你应该看到这样的东西:
92102 resque: Processing ProcessNumbers since 1253142769
Make note of the PID (process id) in my example it is 92102
记下我示例中的 PID(进程 ID),它是92102
Then you can quit the process 1 of 2 ways.
然后您可以通过两种方式中的一种退出进程。
Gracefully use
QUIT 92102Forcefully use
TERM 92102
优雅地使用
QUIT 92102强行使用
TERM 92102
*I'm not sure of the syntax it's either QUIT 92102or QUIT -92102
*我不确定它是QUIT 92102或的语法QUIT -92102
Let me know if you have any trouble.
如果您有任何问题,请告诉我。
回答by user2811637
I just did:
我已经做了:
% rails c production
irb(main):001:0>Resque.workers
Got the list of workers.
得到工人名单。
irb(main):002:0>Resque.remove_worker(Resque.workers[n].id)
... where n is the zero based index of the unwanted worker.
... 其中 n 是不需要的工人的从零开始的索引。
回答by joost
I had a similar problem that Redis saved the DB to disk that included invalid (non running) workers. Each time Redis/resque was started they appeared.
我有一个类似的问题,Redis 将数据库保存到包含无效(非运行)工作人员的磁盘。每次 Redis/resque 启动时,它们都会出现。
Fix this using:
使用以下方法修复此问题:
Resque::Worker.working.each {|w| w.done_working}
Resque.redis.save # Save the DB to disk without ANY workers
Make sure you restart Redis and your Resque workers.
确保您重新启动 Redis 和您的 Resque 工作线程。
回答by Rich Sutton
Here's how you can purge them from Redis by hostname. This happens to me when I decommission a server and workers do not exit gracefully.
以下是通过主机名从 Redis 中清除它们的方法。当我停用服务器并且工作人员没有正常退出时,就会发生这种情况。
Resque.workers.each { |w| w.unregister_worker if w.id.start_with?(hostname) }
回答by Will Bryant
I ran into this issue and started down the path of implementing a lot of the suggestions here. However, I discovered the root cause that was creating this issue was that I was using the gem redis-rb 3.3.0. Downgrading to redis-rb 3.2.2 prevented these workers from getting stuck in the first place.
我遇到了这个问题,并开始着手实施这里的许多建议。但是,我发现造成此问题的根本原因是我使用的是 gem redis-rb 3.3.0。降级到 redis-rb 3.2.2 可以防止这些工人首先陷入困境。
回答by Shai
Started working on https://github.com/shaiguitar/resque_stuck_queue/recently. It's not a solution to how to fix stuck workers but it addresses the issue of resque hanging/being stuck, so I figured it could be helpful for people on this thread. From README:
最近开始在https://github.com/shaiguitar/resque_stuck_queue/ 上工作。这不是如何解决卡住的工人的解决方案,但它解决了重新挂起/卡住的问题,所以我认为它可能对这个线程上的人有所帮助。从自述文件:
"If resque doesn't run jobs within a certain timeframe, it will trigger a pre-defined handler of your choice. You can use this to send an email, pager duty, add more resque workers, restart resque, send you a txt...whatever suits you."
“如果 resque 在特定时间范围内没有运行作业,它将触发您选择的预定义处理程序。您可以使用它来发送电子邮件、寻呼任务、添加更多 resque 工作人员、重新启动 resque、向您发送 txt。 ..适合你的。”
Been used in production and works pretty well for me thus far.
已在生产中使用,到目前为止对我来说效果很好。

