Resque:每个队列一名工作人员
我目前有一个 Rails 3.0 项目,使用 Ruby 1.9.2 和 Resque。
我的应用程序有多个工作类和多个队列,它们是动态创建的(在运行时)。此外,还有多个已启动的工作线程可以在任何队列上自由工作,因为在启动时没有任何现有队列,并且无法预测它们:
$ COUNT=3 QUEUE=* rake resque:workers
根据项目
创建的队列id:
@queue = "project_#{project.id}".to_sym
对于给定的队列,它们的作业必须按顺序处理,并且一次处理一个。我的问题是,通过拥有多个工作人员,可以并行处理多个作业。
有没有办法设置每个队列的最大工作人员数(为 1)?有没有办法在作业处理时锁定队列?
谢谢!
I currently have a Rails 3.0 project, with Ruby 1.9.2 and Resque.
My application has multiple worker classes and multiple queues, that are dynamically created (during runtime). Also, there are multiple workers started that are free to work on any queues, because at start time there isn't any existing queues, and they cannot be predicted:
$ COUNT=3 QUEUE=* rake resque:workers
Queues a created based on the project
's id:
@queue = "project_#{project.id}".to_sym
For a given queue, their jobs have to processed in order and one at a time. My problem is that, by having multiple workers, multiple jobs are processed in parallel.
Is there a way to set the maximum number of workers per queue (to 1)? Is there a way to lock a queue while a job is processing?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于找到了一个非常简单的解决方案,使用 resque-retry 和存储在 redis 中的锁(我是为用户这样做,只是为项目这样做): https://stackoverflow.com/a/10933666/745266
I finally came to a quite simple solution using resque-retry and locks stored in redis (I am doing this for users, just do it for projects): https://stackoverflow.com/a/10933666/745266
Resque-pool 可以帮助您指定每个队列的工作人员数量。
https://github.com/nevans/resque-pool
Resque-pool can help you specify number of workers per queue.
https://github.com/nevans/resque-pool
我想到的第一个解决方案是检查当有另一个工作人员轮询同一队列时是否有任何工作人员在给定队列中工作。这可以通过重新实现 Resque::Job.reserve(queue) 来完成:
一个可能的问题是竞争条件。想法?
The first solution I thought of would be to check if there is any worker working in a given queue when there's another worker polling that same queue. This could be done by reimplementing
Resque::Job.reserve(queue)
:A possible issue would be the race condition. Thoughts?