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
our project. Two ways to set it up:
resourcessection, and choose
heroku-redisfrom the addons section.
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>.
1heroku ps:scale web=1 2heroku ps:scale worker+1
1url: <%= ENV['DATABASE_URL'] %>
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
Taskmodel as follows, which hopefully should fix it:
1 after_commit :log_task_details, on: :create
sidekiqlogs, or your project logs to be more accurate, using
heroku logs -tfrom your projects root, once deployed.
This is an optional step, which you can use, if you need to view the sidekiq
dashboard via browser. Add the following to your
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.