Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

The easiest way to use sidekiq without Rails

Posted on 2018-08-30 by Rodrigo Urubatan

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.

Related

4 thoughts on “The easiest way to use sidekiq without Rails”

  1. madhan ayyasamy says:
    2019-04-05 at 13:49

    If your worker interacts with any Model which needed some database via ActiveRecord, then what is the workaround? Can you please suggest anything?

    Reply
    1. urubatan says:
      2019-05-17 at 00:28

      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

      Reply
  2. Doraemon says:
    2019-04-13 at 12:18

    Thanks for taking the time to do this , very useful!

    Reply
  3. Pingback: Simple and easy way to use ActiveRecord without Rails | Rodrigo Urubatan - About Code

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme