Democratic queue in Sidekiq

流过昼夜 提交于 2019-12-08 07:34:10

问题


Users can create a Project object that contain multiple tasks. When user tells the project to execute all those tasks are put on the default queue from Sidekiq.

The problem is: if a user creates a project with 1000 tasks and hit the execute buttom, all those 1000 tasks are put in queue. If just after that, another user creates a project with only one task, when he hits executes this project, its only task is put on the end of the line.

I'd like that background process to work on a more democratic way. If each project has its own queue, chances are that the one task from the second project is executed before the end of the 1000 tasks queue.

To do so, I will need separe queues for each project, and it should be dynamically created. I'm trying to accomplish that using the following instead of perform_async:

Sidekiq::Client.push('queue' => "project_#{self.project.id}", 'class' => TaskWorker, 'args' => [self.id])

However, when I try to execute a project, all the tasks are placed in a queue called "project_somenumber" but none of them is executed. It seems that when I use Sidekiq::Client.push, I need to tell the queue to start executing. Is it correct? How can I start that queue?


回答1:


Sidekiq doesn't support dynamic queue names. When you start Sidekiq, you tell it which queues to process with "sidekiq -q queue1 -q queue2 ..." Please see Sidekiq's Advanced Options wiki page for more detail.




回答2:


I have the same issue and solved it by creating helper method like this:

def sidekiq_queue(name)
  [name.to_s, Settings.sidekiq_ns].join('_')
end

...

class MyWorker
  sidekiq_options quque: sidekiq_queue(:sync)
end

I use dynamic settings.yml file where set sidekiq namespace.

PS: And of cause you should set this dymanic queue in sidekiq.yml.



来源:https://stackoverflow.com/questions/16530865/democratic-queue-in-sidekiq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!