Sidekiq configuration for multiple environments

ruby, ruby-on-rails, sidekiq

Solution

Use -e option

bundle exec sidekiq -e beta -C config/sidekiq.yml

If all environments(development, staging and production) are on same server then use namespace. In your initializers/sidekiq.rb file,

Sidekiq.configure_server do |config|
    config.redis = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }
end

Sidekiq.configure_client do |config|
    config.redis = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }
end     

Problem

I have looked at multiple sources and tried various scenarios but couldn't resolve this hence the issue. Please point me in the right direction. Like everybody I have 3 env (development, staging and production). I have the following in my sidekiq.yml file ``` # Options here can still be overridden by cmd line args. # sidekiq -C config.yml --- :verbose: false :namespace: xyz :logfile: log/sidekiq.log :concurrency: 25 :strict: false :pidfile: tmp/pids/sidekiq.pid :queues: - [stg_xyz_tests_queue, 10] - [stg_default_xyz_queue, 2] - [stg_xyz_default_queue, 3] development: :verbose: true :concurrency: 15 :queues: - [dev_xyz_queue, 10] - [dev_default_xyz_queue, 2] - [dev_xyz_default_queue, 3] staging: :queues: - [stg_xyz_queue, 10] - [stg_default_xyz_queue, 2] - [stg_xyz_default_queue, 3] production: :queues: - [prod_xyz_queue, 10] - [prod_default_xyz_queue, 2] - [prod_xyz_default_queue, 3] ``` With this I was hoping that when I start sidekiq with the command ``` RAILS_ENV=#{rails_env} bundle exec sidekiq -C config/sidekiq.yml ``` that it would pickup all the values from the configuration file and start sidekiq with the appropriate queues and log file at log/sidekiq.log but that doesn't work. Sidekiq starts but it only creates the stg_xyz_tests_queue, stg_default_xyz_queue and stg_xyz_default_queue no matter what environment we use. The other approach I tried was using the following code in the config/environments/development.rb ``` #configure Sidekiq for dev environment Sidekiq.configure_server do |config| config.options[:namespace] = "xyz" config.options[:concurrency] = 25 config.options[:verbose] = true config.options[:strict] = false config.options[:logfile] = "log/sidekiq.log" config.options[:pidfile] = "tmp/pids/sidekiq.pid" queues = Array.new 10.times do queues.push "dev_xyz_queue" end 2.times do queues.push "dev_default_xyz_queue" end 3.times do queues.push "dev_xyz_default_queue" end config.options[:queues] = queues puts "Sidekiq server config options for development => #{config.options.to_yaml}" end ``` With this the queues are created ok but the logfile is not created or written and I need to duplicate this code for all the 3 environments. What is the best way to get sidekiq working seamlessly for my setup Thanks for your help in advance !!!

Original source