Resque:每个队列一名工作人员

发布于 2025-01-05 07:46:36 字数 437 浏览 2 评论 0原文

我目前有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2025-01-12 07:46:36

我终于找到了一个非常简单的解决方案,使用 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

尐偏执 2025-01-12 07:46:36

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

兮颜 2025-01-12 07:46:36

我想到的第一个解决方案是检查当有另一个工作人员轮询同一队列时是否有任何工作人员在给定队列中工作。这可以通过重新实现 Resque::Job.reserve(queue) 来完成:

module Resque
  class Job

    def self.reserve(queue)

      Resque::Worker.working.each do |worker|
        # if already working in the same queue
        if worker.job['queue'] == queue
          return
        end
      end

      return unless payload = Resque.pop(queue)
      new(queue, payload)
    end

  end
end

一个可能的问题是竞争条件。想法?

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):

module Resque
  class Job

    def self.reserve(queue)

      Resque::Worker.working.each do |worker|
        # if already working in the same queue
        if worker.job['queue'] == queue
          return
        end
      end

      return unless payload = Resque.pop(queue)
      new(queue, payload)
    end

  end
end

A possible issue would be the race condition. Thoughts?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文