Sidekiq is usually my “goto” backend processing engine when I’m writing a Rails application.
Recently I had to write a small app that was not a web app, so it didn’t needed Rails, but I’m very comfortable with Ruby, and decided to write the background processing app, or a small “daemon” as we called it in the dark age of internet in Ruby.
And as I’m really used to the Sidekiq API and management, I used it too, but that started a small and ans easy to fix problem for me.
I wanted to use Sidekiq, and I was not using Rails, usually both are really well integrated;
After some research, I found out that you just need to use the ‘-r’ parameter from sidekiq to point it to the file it needs to require to load the workers and any other file you need it to use.
I have one project that uses only Sidekiq and ActiveRecord for example, if you think it is useful to see how to use ActiveRecord without rails, just leave a comment and I’ll write a quick tutorial.
To start our quick sample, lets create a Gemfile:
source 'https://rubygems.org' gem 'sidekiq' gem 'redis-namespace' gem 'pry'
then we’ll create an “environment.rb” file to load the application, I’ll use an “app” directory, and make the “environment.rb” load everything that is inside the app directory, and for this sample, we’ll have an ‘workers’ directory and one ‘initializers’ to configure Sidekiq.
require 'rubygems' require 'bundler/setup' Bundler.require(:default) $LOAD_PATH.unshift File.join(File.dirname(__FILE__),'..') Dir.glob(File.join(File.dirname(__FILE__), 'app/**/*.rb')).each do |fname| load fname end
with this ‘environment.rb’ file, you just need to add your code inside the app folder, and we’ll do exactly this now, creating an ‘app/initializers/sidekiq.rb’ to configure how sidekiq will connect to Redis.
Sidekiq.configure_server do |config| config.redis = {:namespace => 'SidekiqSample', :url => "redis:localhost:6379" } end Sidekiq.configure_client do |config| config.redis = {:namespace => 'SidekiqSample', :url => "redis:localhost:6379" } end
And now we can add our workers to the app/workers directory, or any other directory we want inside app, for this sample I’ve created two workers called respectively ‘SampleWorker’ and ‘OtherWorker’, I’m posting their code bellow:
class SampleWorker include Sidekiq::Worker def perform(number) (1..number).each do |idx| OtherWorker.perform_async(idx) end logger.warn("Started #{number} processes") end end
class OtherWorker include Sidekiq::Worker def perform(idx) logger.warn("Processing job #{idx}") end end
These workers do exactly nothing, but they are good enough for this sample.
To start sidekiq, I’ve created a simple starter.sh script with the command: bundle exec sidekiq -c 2 -r ./environment.rb
And to make it easy to interact with the app, I’ve used pry to create the application console, in a ‘console’ script as you can see bellow:
#!/usr/bin/env ruby load './environment.rb' pry
With this, you can start your “Sidekiq only” application, and add just the libraries you need for the queue processing.
If you wanna take a look at the sample code in a more user friendly way, you can check this github repository: https://github.com/urubatan/sidekiqsample
If you think this quick tutorial was useful, please leave a comment, and if you think this can help others, please share the link to this post to your social network.
If your worker interacts with any Model which needed some database via ActiveRecord, then what is the workaround? Can you please suggest anything?
Sorry for the delay answering this, this blog was kinda forgotten by me for some time here 😀
to use activerecord you just need to load that gem, and configure it with the same config file as you use for rails 😀
I can post a sample if you are still interested
Thanks for taking the time to do this , very useful!