Rails: Starting Sidekiq on Heroku

Ruby on-RailsHerokuSidekiq

Ruby on-Rails Problem Overview


I'm having a problem getting Sidekiq up and running on my Heroku deployed Rails app. I have my app working fine in development (and on Heroku without Sidekiq).

I created a Procfile with:

worker: bundle exec sidekiq

If I run heroku ps, the only process I see is web.1.

Should I see one for Sidekiq?

I do get an error:

Redis::CannotConnectError (Error connecting to Redis on localhost:6379) in my Heroku logs.

UPDATE: Found I probably needed heroku addons:add redistogo. Still not working. I feel I'm missing some basic configuration.

Is there something I need to do to get Redis up and running for my Heroku app?

I've been using Redis/Sidekiq for about a day, so this is new to me.

Thanks!

Greg

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

No you do not need any config with Heroku for Sidekiq, just add the RedisToGo plugin and you're on. Do not forget to attribute at least 1 worker to your app in your Heroku config.

Here is my default Procfile:

web: bundle exec thin start -p $PORT
worker: bundle exec sidekiq -c 5 -v

Solution 2 - Ruby on-Rails

It's worth checking if the sidekiq process is really started with this command:

heroku ps

If there's no worker, then you might need to run this command:

heroku ps:scale worker+1

It turns out that there's a bug in the web UI in that some team members were not allowed to increase the number of workers from 0 to 1, even though the UI seemed to show that!

Solution 3 - Ruby on-Rails

Starting with sidekiq version 3.0 there is an additional step, run heroku config:set REDIS_PROVIDER=REDISTOGO_URL in the console.

Here is the process I used for Rails 4:

In the console:

heroku addons:create redistogo
heroku config:set REDIS_PROVIDER=REDISTOGO_URL

In my Procfile I added:

worker: bundle exec sidekiq

In my gemfile.rb I added:

gem 'redis'

I added the following file, config/initializers/redis.rb:

uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)

Here is the link to the sidekiq docs.

Solution 4 - Ruby on-Rails

Complementing gdurelle answer:

You do need some config with Heroku for Sidekiq:

  1. Have the Sidekiq and Redis gems installed (in gemfile and bundled), in my case:

Gemfile

gem 'redis', '~> 3.1'
gem 'sidekiq', '~> 2.7', '>= 2.7.1'
  1. Add a worker, if you don't have any workers created locally I suggest you create at least one, just in case, use this:
rails g sidekiq:worker Hard # will create app/workers/hard_worker.rb

should create this:

app/workers/hard_worker.rb

class HardWorker
  include Sidekiq::Worker
  def perform(name, count)
    # do something
  end
end
  1. Add the Redis add-on (in my case Heroku Redis):
heroku addons:create heroku-redis:hobby-dev
  1. Add your redis.rb file, in my case:

config/initializers/redis.rb

$redis = Redis.new(url: ENV["REDIS_URL"])
  1. Add Procfile or config/sidekiq.yml or both, here are mine:

Procfile

worker: bundle exec sidekiq -c 1 -q default -q mailers 

which you can create easier by pasting this in your terminal

echo "worker: bundle exec sidekiq -c 1 -q default -q mailers" > Procfile

config/sidekiq.yml

:concurrency: 1
:queues:
  - [mailers, 7]
  - [default, 5]
  1. Most important part go https://dashboard.heroku.com/apps/equitec-app/resources">here</a>;: https://drive.google.com/uc?export=view&id=1Ou221tXDpl2fN_hbGN9KDJ9m__pAiVYz"> and turn on the switch for your worker, click on the pencil and then turn on the missing switch. Things should be working fine now, have a great day!

Solution 5 - Ruby on-Rails

What I found out is that you have to scale process manually like so:

heroku ps:scale worker+1

Makes no sense since my Procfile said:

web: bundle exec....
worker: bundle exec sidekiq

...and one would've expectd Heroku to start the worker automatically. In the end I didn't have to scale the web process...

Also, you might have problems with this line: worker: bundle exec sidekiq

Add flags for concurency:

worker: bundle exec sidekiq -c 5 -v

Solution 6 - Ruby on-Rails

Sounds like you're almost there. It may be that you just need to set REDISTOGO_URL on the heroku config?

heroku config 

should show you the redistogo value?

If you copy that to REDISTOGO_URL then sidekiq should work?

heroku config:add REDISTOGO_URL=<redistogo value>

Edit: Sidekiq will use any of these currently: https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/redis_connection.rb#L29-L33

Edit2: Greg is correct in that you don't need to add the config if you're using RedisToGo. But if you're using OpenRedis, or other Redis providers, then you need to add REDISTOGO_URL for Sidekiq

Solution 7 - Ruby on-Rails

You can of course run sidekiq alongside your current jobs queue.

Our Procfile currently looks like this:

web: bundle exec thin start -R config.ru -e $RAILS_ENV -p $PORT
worker:  bundle exec rake jobs:work
sidekiq: bundle exec sidekiq -c 5 -v

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg RogersView Question on Stackoverflow
Solution 1 - Ruby on-RailsgdurelleView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjustingordonView Answer on Stackoverflow
Solution 3 - Ruby on-RailsSteveView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJose PaezView Answer on Stackoverflow
Solution 5 - Ruby on-Railsluigi7upView Answer on Stackoverflow
Solution 6 - Ruby on-RailsgefView Answer on Stackoverflow
Solution 7 - Ruby on-RailssuperluminaryView Answer on Stackoverflow