Hanami with Sidekiq
I’ve been working on a small Hanami app as a side project recently and wanted to use Sidekiq to implement some of the functionality.
Adding Sidekiq to a Rails project is pretty straightforward and adding it to a Hanami application is not any harder.
There is a good guide written by David Strauß on his blog which I mostly followed. His guide is a couple of years old now and there are some small changes which I had to discover by searching github issues and the Hanami Gitter so I will detail here what I did to get everything working.
Sidekiq Setup
This part is identical to the process details in David’s blog.
Add sidekiq to your gemfile and bundle install
.
Create a config/sidekiq.rb
file:
Require Sidekiq in your config/environment.rb
Then we need to add the REDIS_URL
to our .env
files and the setup is complete.
Adding Workers
With the setup complete we can now add our workers. I went back and forth over whether they should go in apps/web
or lib
and eventually decided to go with lib/<app name>/workers
.
If you do put your workers in apps/web/workers
you will need to add the workers directory to your load paths in apps/web/application.rb
Lets create a simple worker that prints whatever is passed to it.
Running Sidekiq
There is a small change to David’s instructions on how to run Sidekiq. We need to pass the config/boot.rb
file to Sidekiq and not the config/environment.rb
file.
To run the Sidekiq server we need to use bundle exec sidekiq -e development -r ./config/boot.rb
. This boots up the Hanami app and gives us access to our Entities, repositories etc.
If we start a Hanami console with bundle exec hanami c
we can now call our worker:
Once called we should see output like this in the Sidekiq log:
Mounting the Sidekiq Dashboard
I couldn’t get David’s instructions to work for this part so here is what I did instead.
Add Sinatra to your Gemfile and bundle install
. You do not need to require it as Sidekiq will require what it needs.
Require Sidekiq from the routes file and pass it your session secret environment variable.
I’ve left the default comments in place to illustrate that I placed this right at the top of the file.
The session secret environment variable needs to be set or requests to cancel or retry jobs etc. you make in the Sidekiq dashboard will error.
If your Hanami app is running on the default port you should now be able to visit localhost:2300/sidekiq
and see the Sidekiq dashboard.
There it is, Sidekiq running in a Hanami application. You can now go wild with your required background processing workflows.
Final Note
As it stands the Sidekiq dashboard is not secured in any way. It really should be behind a login of some kind. Whether this is basic auth or the authorisation your app already uses is up to you.
My little side project hasn’t got any kind of authentication/authorisation yet so I haven’t included any instruction on how to do that, perhaps I’ll write a future blog to do that when I get to it.