Rails 3.2: Gem Dependencies Only From Sidekiq Worker
I have a Rails application that contains a lot of code and dependencies. It has a web server component, a worker component (based on Sidekiq) and a database component.
I have some gems that the workers should have, but I don't need them on web servers. I also don't want my controller or view code to load into the Sidekiq desktop.
Is there a way to tell Bundler (via Gemfile, I suppose) to only include certain gems or classes in workers, but not in the webserver?
source to share
The cleanest solution is to create a workgroup in your Gemfile:
group :worker do
# These gems are only needed by the workers
gem 'foo'
gem 'bar'
end
These gems will not be loaded by default.
Then when initializing your workers calls Bundler.require(:worker)
EDIT (in response to your comment):
Two perfectly acceptable options here,
a) Don't load the rails environment into your workers.
I believe the correct way to do this is to call sidekiq with sidekiq -r worker_environment.rb
where work_environment.rb requires your dependencies and your work files.
b) Configure the rails environment to not load the default gem group for your workflows.
I'm not sure with Rails 3.2, but in Rails 4.x you will find the line in your config / application.rb Bundler.require(*Rails.groups)
. Rails.groups will usually be something like [:default, :development]
or [:default, :production]
. So you can add some logic to only be required :worker
if you are in a workflow instead (maybe you can set the env var just for your workflows to differentiate here).
source to share