Sidekiq - 防止工人在特定机器中执行

时间:2015-06-30 12:00:32

标签: ruby-on-rails distributed sidekiq

我正在开发一个使用Sidekiq的Rails项目。我们的Sidekiq实现有两个worker(WorkerA,读取queue_a,WorkerB,读取queue_b)。其中一个必须在Rails应用程序所在的同一服务器中执行,而另一个服务器在不同的服务器中执行。如何防止WorkerB在第一台服务器中执行,反之亦然?可以将Sidekiq流程配置为仅运行特定的工作人员吗?

编辑: Redis服务器与Rails应用程序位于同一台机器上。

2 个答案:

答案 0 :(得分:4)

使用特定于主机名的队列。配置/ sidekiq.yml:

$string = preg_replace(
    '/url\(("|\')(?!https?)\/?(.+?)(?:\1)/',
    'url(\1http://example.com/$2\1',
    $string
)

在你的工人中:

---
:verbose: false
:concurrency: 25
:queues:
  - default
  - <%= `hostname`.strip %>

我博客上的更多细节:

http://www.mikeperham.com/2013/11/13/advanced-sidekiq-host-specific-queues/

答案 1 :(得分:2)

好吧,这是使用选项启动sidekiq的方法

nohup bundle exec sidekiq -q queue_a queue_b -c 5 -e #{Rails.env} -P #{pidfile} 2>&1 &

您可以与特定工作人员一起启动sidekiq

您可以运行nohup bundle exec sidekiq -q queue_a -c 5 -e #{Rails.env} -P #{pidfile} 2>&1 &仅执行WorkA

区分不同服务器上的不同工作人员,如下所示:

system "nohup bundle exec sidekiq -q #{workers_string} -c 5 -e #{Rails.env} -P #{pidfile} 2>&1 &"

def workers_string
  if <on server A> # using ENV or serverip to distinguish
    "queue_a"
  elsif <on server B>
    "queue_b queue_b ..."
  end
end

#or you can set the workers_strings into config file on different servers