bigpotato
bigpotato

Reputation: 27557

Rails + Sidekiq: Sidekiq running in wrong environment

I have a Rails 3 app that I deploy using Capistrano. I recently added Sidekiq. It works fine in my development. I'm hosting both staging and preview on the same server and it's preview that is not functioning properly. When I trigger the worker on preview it goes to staging and hits the staging database. Am I missing configuration to have 2 sidekiq processes coexist on the same server?

Here's my deploy.rb:

require "bundler/capistrano"
require 'sidekiq/capistrano' #<-- sidekiq

load "lib/deployer/deployer.rb"
set :application, "myapp"
set :scm, :git
set :repository,  ...
set :scm_passphrase, ""
defaults
global_defaults
set :stages, ["staging", "preview"]

task :staging do
  set :rails_env, "staging"
  set :user, "deployer"
  server "myserver.com", :app, :web, :db, :primary => true

  defaults
end

task :preview do
  set :rails_env, "preview"
  set :user, "deployer"
  server "myserver.com", :app, :web, :db, :primary => true

  defaults
end

It's inconsistent. In preview I just did an action that triggers the worker, and 4 times it went to staging (I could see in the log as well as the console), and 1 time it hit preview like it was supposed to.

Am I missing something?

Upvotes: 4

Views: 3623

Answers (2)

engineerDave
engineerDave

Reputation: 3945

I think the solution you're looking for is namespace related.

https://github.com/mperham/sidekiq/wiki/Advanced-Options#wiki-using-sidekiqs-configure-blocks

This is what I use in my initializer.

Sidekiq.configure_server do |config|
  config.redis = { url:       'redis://localhost:6379/0',
                   namespace: "sidekiq_#{Rails.application.class.parent_name}_#{Rails.env}".downcase }
end

Sidekiq.configure_client do |config|
  config.redis = { url:       'redis://localhost:6379/0',
                   namespace: "sidekiq_#{Rails.application.class.parent_name}_#{Rails.env}".downcase }
end

Upvotes: 6

TalkativeTree
TalkativeTree

Reputation: 619

So it would help to see what the action is as well as the worker and any configuration you've set up for Sidekiq. Since you're using Capistrano, have you set up your configuration for Sidekiq

https://github.com/mperham/sidekiq/wiki/Deployment

Also, this is probably a bad solution, but it may at least get you past this roadblock. You can set different redis queues, so you could create a staging queue and a preview queue. For it to actually work, you might have to set the queue you're not going to use frequency of checking to 0.

https://github.com/mperham/sidekiq/wiki/Advanced-Options#wiki-queues

In your worker, try adding

sidekiq_options :queue => QUEUE

and set QUEUE = :staging in your staging's environment and QUEUE = :preview in your preview's environment.

Upvotes: 0

Related Questions