Learn Ruby on Rails Book

Configure Sidekiq in Heroku

In previous chapter, we have deployed application to Heroku. However, we also need to setup Sidekiq on Heroku to process background jobs.

  • First we need to have the heroku-redis addon for our project. In order to use an addon, we need to verify our account using a credit card. Ultimately the credit card is used to verify that the user is not a bogus user. Since it's a free addon, the credit card won't be charged for usage. If we don't have a verified account, then we can't add the redis addon and thus won't be able to directly make sidekiq work in heroku!

  • Assuming we have a verified account, the next step is to add heroku-redis to our project. Two ways to set it up:

    • From the heroku website, goto resources section, and choose heroku-redis from the addons section.
    • Or run the following from your projects root: heroku addons:create heroku-redis:hobby-dev
  • After setting up the addon, add the following to your Procfile. If you don't understand what a Procfile is, then refer the detailed docs from heroku:

1web: bundle exec rails s -p $PORT
2worker: bundle exec sidekiq -e production -C config/sidekiq.yml

Then push this file to heroku using git push heroku <master or main>.

  • Next step is to scale our dyno to use both the web and worker:
1heroku ps:scale web=1
2heroku ps:scale worker+1
  • Sometimes our active jobs gets enqueued and not get processed in heroku. To avoid that, make sure you have successfully completed the above steps and also add the following under your production key in config/database.yml:
1url: <%= ENV['DATABASE_URL'] %>
  • [Optional][rare case] Sometimes the jobs even after doing the above steps, gets enqueued in heroku without processing. Weird right? Then we can invoke the job from our Task model as follows, which hopefully should fix it:
1  after_commit :log_task_details, on: :create
  • You can view the sidekiq logs, or your project logs to be more accurate, using heroku logs -t from your projects root, once deployed.

Viewing Sidekiq dashboard in browser

This is an optional step, which you can use, if you need to view the sidekiq dashboard via browser. Add the following to your config/routes.rb:

1require 'sidekiq/web'
2
3Rails.application.routes.draw do
4  Sidekiq::Web.use Rack::Auth::Basic do |username, password|
5    ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username),
6                                                ::Digest::SHA256.hexdigest(ENV['SIDEKIQ_USERNAME'])) &
7      ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password),
8                                                  ::Digest::SHA256.hexdigest(ENV['SIDEKIQ_PASSWORD']))
9  end
10  mount Sidekiq::Web, at: '/sidekiq'
11  # rest of the routes would reside below this

We have set our app to allow viewing sidekiq logs via /sidekiq path. But we authenticate the users who can view it, using two env variables(ideally it should be rails credentials). So we need to set up those to config env variables in heroku website under Settings -> Config Vars tab.

1SIDEKIQ_USERNAME : some_user_name
2SIDEKIQ_PASSWORD : some_pass_word

That's it. Now we can view the dashboard using the /sidekiq path by providing the credentials you have set.

⌘F
    to navigateEnterto select Escto close
    Older
    Newer