[Awesome Ruby Gem] Use request_store-sidekiq gem to integrate between RequestStore and Sidekiq

request_store-sidekiq

request_store-sidekiq provide an easy integration between RequestStore and Sidekiq.

RequestStore allows you to easily create threadsafe code, and this middleware for Sidekiq brings that functionality to Sidekiq workers, or even ActiveJob backed Sidekiq.

Installation

You can install it as a gem:

1
$ gem install request_store-sidekiq

or add it into a Gemfile (Bundler):

1
2
3
# madebylotus/request_store-sidekiq: Provides an easy integration between RequestStore and Sidekiq
# https://github.com/madebylotus/request_store-sidekiq
gem 'request_store-sidekiq', '0.1.0'

Then, run bundle install.

1
$ bundle install

Usages

If using Rails, the Sidekiq middleware is already inserted into the Sidekiq middleware stack for you automatically and there is nothing to do.

And try the code to this:

1
2
3
4
5
6
def index
RequestStore.store[:foo] ||= 0
RequestStore.store[:foo] += 1

render :text => RequestStore.store[:foo]
end

Now no matter Sidekiq workers, or even ActiveJob backed Sidekiq you use, you’ll get 1 every time: the storage is local to that worker.

References

[1] madebylotus/request_store-sidekiq: Provides an easy integration between RequestStore and Sidekiq - https://github.com/madebylotus/request_store-sidekiq

[2] request_store-sidekiq | RubyGems.org | your community gem host - https://rubygems.org/gems/request_store-sidekiq

[3] steveklabnik/request_store: Per-request global storage for Rack. - https://github.com/steveklabnik/request_store

[4] request_store | RubyGems.org | your community gem host - https://rubygems.org/gems/request_store/

[5] madebylotus/request_store-sidekiq: Provides an easy integration between RequestStore and Sidekiq - https://github.com/madebylotus/request_store-sidekiq