samgranieri / sidekiq-hostname_fetch

Sidekiq::HostnameFetch Strategy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sidekiq::HostnameFetch Strategy

Build Status

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.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-hostname_fetch', github: "kirs/sidekiq-hostname_fetch"

And then execute:

$ bundle

Usage

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.

Simple demo

(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

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Sidekiq::HostnameFetch Strategy

License:MIT License


Languages

Language:Ruby 100.0%