Galet
Galet

Reputation: 6279

Redis configuration for sidekiq with different database

My application is running on same server for different environments with sidekiq. I am facing mismatching of sidekiq process in all environments.

So i need to use different database for development/staging/testing environment of same server. How to configure redis to run different database for each environment.

Upvotes: 0

Views: 1892

Answers (3)

jphortense
jphortense

Reputation: 131

I tried /2, /3, /4, etc. but sidekiq does not start. If I rollback to /0, it works perfectly.

2022-11-12T02:37:02.601Z pid=8 tid=gx4 INFO: Booting Sidekiq 6.4.2 with redis options {:url=>"redis://xxxxxxxxxxxxxxxxxxxxxxxx.cache.amazonaws.com:6379/2"}
{"severity":"WARN","time":"2022-11-12T02:37:02.608+00:00","message":"Redis::CannotConnectError: Redis client could not connect to any cluster nodes"}
{"severity":"WARN","time":"2022-11-12T02:37:02.608+00:00","message":"/usr/local/bundle/gems/redis-4.6.0/lib/redis/cluster/slot_loader.rb:22:in `load'\n/usr/local/bundle/gems/redis-4.6.0/lib/redis/cluster.rb:116:in `fetch_cluster_info!'\n/usr/local/bundle/gems/redis-4.6.0/lib/redis/cluster.rb:26:in `initialize'\n/usr/local/bundle/gems/redis-4.6.0/lib/redis.rb:84:in `new'\n/usr/local/bundle/gems/redis-4.6.0/lib/redis.rb:84:in `initialize'\n/App/config/initializers/redis_cache.rb:8:in `new'\n/App/config/initializers/redis_cache.rb:8:in `\u003cmain\u003e'\n/usr/local/bundle/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'\n/usr/local/bundle/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/engine.rb:667:in `block in load_config_initializer'\n/usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/notifications.rb:208:in `instrument'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/engine.rb:666:in `load_config_initializer'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/engine.rb:620:in `block (2 levels) in \u003cclass:Engine\u003e'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/engine.rb:619:in `each'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/engine.rb:619:in `block in \u003cclass:Engine\u003e'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:32:in `instance_exec'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:32:in `run'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:61:in `block in run_initializers'\n/usr/local/lib/ruby/3.1.0/tsort.rb:228:in `block in tsort_each'\n/usr/local/lib/ruby/3.1.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'\n/usr/local/lib/ruby/3.1.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'\n/usr/local/lib/ruby/3.1.0/tsort.rb:431:in `each_strongly_connected_component_from'\n/usr/local/lib/ruby/3.1.0/tsort.rb:421:in `block in each_strongly_connected_component_from'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:50:in `each'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:50:in `tsort_each_child'\n/usr/local/lib/ruby/3.1.0/tsort.rb:415:in `call'\n/usr/local/lib/ruby/3.1.0/tsort.rb:415:in `each_strongly_connected_component_from'\n/usr/local/lib/ruby/3.1.0/tsort.rb:349:in `block in each_strongly_connected_component'\n/usr/local/lib/ruby/3.1.0/tsort.rb:347:in `each'\n/usr/local/lib/ruby/3.1.0/tsort.rb:347:in `call'\n/usr/local/lib/ruby/3.1.0/tsort.rb:347:in `each_strongly_connected_component'\n/usr/local/lib/ruby/3.1.0/tsort.rb:226:in `tsort_each'\n/usr/local/lib/ruby/3.1.0/tsort.rb:205:in `tsort_each'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/initializable.rb:60:in `run_initializers'\n/usr/local/bundle/gems/railties-7.0.3/lib/rails/application.rb:372:in `initialize!'\n/App/config/environment.rb:5:in `\u003ctop (required)\u003e'\n/usr/local/bundle/gems/sidekiq-6.4.2/lib/sidekiq/cli.rb:273:in `require'\n/usr/local/bundle/gems/sidekiq-6.4.2/lib/sidekiq/cli.rb:273:in `boot_application'\n/usr/local/bundle/gems/sidekiq-6.4.2/lib/sidekiq/cli.rb:37:in `run'\n/usr/local/bundle/gems/sidekiq-6.4.2/bin/sidekiq:31:in `\u003ctop (required)\u003e'\n/usr/local/bundle/bin/sidekiq:25:in `load'\n/usr/local/bundle/bin/sidekiq:25:in `\u003ctop (required)\u003e'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli/exec.rb:58:in `load'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli/exec.rb:58:in `kernel_load'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli/exec.rb:23:in `run'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli.rb:483:in `exec'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli.rb:31:in `dispatch'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/cli.rb:25:in `start'\n/usr/local/bundle/gems/bundler-2.3.10/exe/bundle:48:in `block in \u003ctop (required)\u003e'\n/usr/local/bundle/gems/bundler-2.3.10/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors'\n/usr/local/bundle/gems/bundler-2.3.10/exe/bundle:36:in `\u003ctop (required)\u003e'\n/usr/local/bundle/bin/bundle:25:in `load'\n/usr/local/bundle/bin/bundle:25:in `\u003cmain\u003e'"}

My use case: I want to share the same redis instance between multiple test applications.

Upvotes: 0

Dheena
Dheena

Reputation: 117

For multiple Redis Database for each environment in sidekiq

add the below line in config/environment/env.rb as

 require 'sidekiq'

Sidekiq.configure_client do |config|
config.redis = { :namespace => 'xxx', :url => 'redis://127.0.0.1:6379/2' }
end

Sidekiq.configure_server do |config|
config.redis = { :namespace => 'xxx', :url => 'redis://127.0.0.1:6379/2' }
end

So the /2 will define the redis database. create seperate database for each environment by changing the value /3, /4 and so on

Upvotes: 1

Tom Wilson
Tom Wilson

Reputation: 837

We've used separate sidekiq queues for each environment. It was a bit tricky to get it set up right, but it works.

Then of course you have to run separate workers for each environment, each accessing it's own environment's queue (and it's own environment's database.)

We started doing this because of time we wasted on "integration" tests failing because we had development workers accessing the queue instead of test workers. There may be other ways but this seemed simplest

Upvotes: 0

Related Questions