title: Sidekiq deploy to multiple environments
body: <p>(See below for my detailed config, which is the result of Henley Chiu's answer).</p>
  
  <p>I've been trying to wrap my brain around Sidekiq deploys, and I am not really getting it. I have an app with a staging environment, and a production environment, on the same server. Everything I see about sidekiq deploys basically say "just add sidekiq/capistrano to your deploy file", so I did that. And then the instructions are "here's a yml file with options" but nothing seems to be explained. Do I need namespaces? I see that in an initialize file, but that seems to be to point outside the server.</p>
  
  <p>I deployed earlier, and each stage seems to boot sidekiq up with the proper environment, but they both process from the same queues. My emails from production were trying to be processed by the stage sidekiq, and failing. I stopped my stage for now, but eventually I will need to use it again. I hope I'm not being dense, I've really tried to understand this and am just having a hard time with finding a definitive "here's how it's done".</p>
  
  <p>For what it's worth, here is config/sidekiq.yml (which is loaded fine during the deploy):</p>
  
  <pre><code>:concurrency: 5
  :verbose: false
  :pidfile: ./tmp/pids/sidekiq.pid
  :logfile: ./log/sidekiq.log
  :queues:
    - [carrierwave, 7]
    - [client_emails, 5]
    - [default, 3]
  staging:
    :concurrency: 10
  production:
    :concurrency: 25
  </code></pre>
  
  <p>Log files, and pids seem to be in the right spot, but the queues are just merged. Any help would be GREAT!</p>
  
  <p>Also, if it matters:</p>
  
  <pre><code>Rails 3.2.11, passenger, nginx, rvm, Ubuntu 12.10, and Ruby 1.9.3
  </code></pre>
  
  <h2>Detailed Configuration (answer):</h2>
  
  <p>First I set up a new redis server at port 7777 (or whatever port you please besides the default 6379). Pretty much followed the <a href="http://redis.io/topics/quickstart" rel="nofollow noreferrer">redis quickstart guide</a> that I used the first time around.</p>
  
  <p>Then I made the initilizer file; this has both the client and the server config. Both are required to make sidekiq work multistage.</p>
  
  <p>Note that I am using an external YAML file for the settings. I am using <a href="https://github.com/binarylogic/settingslogic" rel="nofollow noreferrer">SettingsLogic</a> for this to make things easier, but you can just as easily <a href="https://stackoverflow.com/questions/592554/best-way-to-create-custom-config-options-for-my-rails-app">do this yourself by including the file</a>. By using a yaml file, we don't have to touch our environments/staging or production files.</p>
  
  <pre><code># config/initializers/sidekiq.rb
  server = Settings.redis.server
  port = Settings.redis.port
  db_num = Settings.redis.db_num
  namespace = Settings.redis.namespace
  
  Sidekiq.configure_server do |config|  
    config.redis = { url: "redis://#{server}:#{port}/#{db_num}", namespace: namespace  }
  end
  </code></pre>
  
  <p>I am using passenger - the <a href="https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting" rel="nofollow noreferrer">troubleshooting page of the sidekiq wiki</a> recommends a change for the setup when using unicorn or passenger, so I added the code there for the client setup:</p>
  
  <pre><code># config/initializers/sidekiq.rb (still)
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      Sidekiq.configure_client do |config|
        config.redis = { url: "redis://#{server}:#{port}/#{db_num}", namespace: namespace }
      end if forked
    end
  end
  </code></pre>
  
  <p>This is my Settings file (obviously values changed):</p>
  
  <pre><code>#config/settings.yml
  defaults: &amp;defaults
    redis: &amp;redis_defaults
      server: 'localhost'
      port: 6379
      db_num: 0
      namespace: 'sidekiq_development'
  
  development:
    &lt;&lt;: *defaults
  
  test:
    &lt;&lt;: *defaults
  
  staging:
    &lt;&lt;: *defaults
    redis:
      &lt;&lt;: *redis_defaults
      port: 8888
      namespace: 'sidekiq_staging'
  
  production:
    &lt;&lt;: *defaults
    redis:
      &lt;&lt;: *redis_defaults
      port: 7777
      namespace: 'sidekiq_production'
  </code></pre>
  
  <p>I found that adding the namespace to the config/sidekiq.yml file didn't seem to work - sidekiq would boot on deploy using the right port, but wouldn't actually process anything. But since the wiki recommends using a namespace, I ended up just adding it to the init file.</p>
  
  <p>I hope this helpful for others, because this was really hard for me to understand, having not done a lot of this kind of setup before.</p>