Search code examples
ruby-on-railsrubysidekiq

Sidekiq with Rails - Different Sidekiq instances in console vs rake task


Im fairly new to working with asynchronous jobs in Rails and I'm just getting used to sidekiq.

When I write a rake task which connects to sidekiq to clear a queue for example I'm seeing one set of queues including some custom named queues that I had created as part of another application some time ago on my machine. These queues contain none of the jobs which are started by my application (I've been testing this with a mailer)

When I connect to sidekiq from the rails console, I get the sidekiq instance which seems to be getting used by the application when I schedule a job and can see the mailer jobs that I've scheduled.

Inspecting Sidekiq.redis_info from both scenarios gives pretty much teh same information and the redis server being used is the same. when I shut down the redis server, I'm unable to connect to Sidekiq from either the task or the console.

Any ideas why this might be happening and how I might connect to the correct Sidekiq instance from my rake task?

Using Ruby 2.7.8, rails 5.2.6 and this is the initializer for my sidekiq in rails:

rails_root = Rails.root || File.dirname(__FILE__) + '/../..'
rails_env = Rails.env || 'development'
redis_config = YAML.load_file(rails_root.to_s + '/config/redis.yml')
redis_config.merge! redis_config.fetch(Rails.env, {})
redis_config.symbolize_keys!
Sidekiq.configure_server do |config|
  config.redis = { url: "redis://#{redis_config[:host]}:#{redis_config[:port]}/12", namespace: "one-api" }

  config.super_fetch!
end

Sidekiq.configure_client do |config|
  config.redis = { url: "redis://#{redis_config[:host]}:redis_config[:port]}/12", namespace: "one-api" 
}
end

Solution

  • Redis is the place the jobs are persisted, so if both apps were configured to store jobs in the same redis location, then naturally you'll have the old jobs still there regardless of which version of sidekiq you run.

    Ideally you should use a new/different redis DB for a new app. There are many different ways to configure redis for Sidekiq, but what you're interested in is the number, so instead of whatever you're using (12?) you want to use one of the other 15 DBs available in a default redis install.

    If you're no longer using the old app then you could also leave your config and just delete all the keys from the appropriate DB (from your shell command line for the 12 DB):

    > redis-cli -n 12 FLUSHDB