Imagine: you have 10 sidekiq job servers and you want to enque some job on specific server. With this strategy you can easily do this.
Sponsored by Evil Martians.
Thanks to Yaroslav Markin and Alexey Gaziev for advice about making this project.
Add this line to your application's Gemfile:
gem 'sidekiq-hostname_fetch', github: "kirs/sidekiq-hostname_fetch"
And then execute:
$ bundle
Define the hostname strategy:
Sidekiq.configure_server do |config|
config.options[:fetch] = Sidekiq::HostnameFetch::Strategy
end
Now Sidekiq server will fetch tasks also from hostname-specific queues.
To send jobs to specific server, you can use:
MyWorker.perform_async_on_host "app-03.beta.myproject.com", "arg_to_worker", "another_arg"
As well as perform_in_on_host
and perform_at_on_host
.
Another way is to declate sidekiq_options host_specific: true
inside the worker.
With this option, task will be always performed on the same server it was enqueed.
To perform only one job on current host, use perform_async_on_current_host
.
(you can pick up demo.rb
from this repo)
Start up sidekiq via
bundle exec sidekiq -r ./demo.rb
and then you can open up an IRB session like so:
bundle exec irb -r ./demo.rb
where you can then say
HardWorker.perform_async # <-- without plugin
HardWorker.perform_async_on_host "myhost" # <-- won't work
HardWorker.perform_async_on_host `hostname`.strip # <-- gonna work on your machine
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request