davids
davids

Reputation: 6371

Sidekiq - Prevent worker from being executed in specific machine

I'm working on a Rails project that uses Sidekiq. Our Sidekiq implementation has two workers (WorkerA, that reads queue_a, and WorkerB, which reads queue_b). One of them has to be executed in the same server the Rails app is and the other one in a different server(s). How can I prevent WorkerB from being executed in the first server, and vice versa? Can a Sidekiq process be configured to run just specific workers?

EDIT: The Redis server is in the same machine the Rails app is.

Upvotes: 3

Views: 1336

Answers (2)

Mike Perham
Mike Perham

Reputation: 22228

Use a hostname-specific queue. config/sidekiq.yml:

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

In your worker:

class ImageUploadProcessor
  include Sidekiq::Worker
  sidekiq_options queue: `hostname`.strip

  def perform(filename)
    # process image
  end
end

More detail on my blog:

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

Upvotes: 7

Zoker
Zoker

Reputation: 3959

well, here is the way to start sidekiq with options

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

you can start sidekiq with specific workers

you can run nohup bundle exec sidekiq -q queue_a -c 5 -e #{Rails.env} -P #{pidfile} 2>&1 & to execute only WorkA

to distinguish different workers on different servers, just do like below:

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

Upvotes: 2

Related Questions