[001.2] Installation and First Job

Installing Sidekiq and writing and executing your first job.

Subscribe now

Installation and First Job [05.13.2016]

Sidekiq is a background processing framework for Ruby. It's very simple to use and extremely fast. It consists of three parts: the Sidekiq Client API, the Sidekiq server, and a Redis server. In today's episode we're going to start a new ruby project, integrate Sidekiq, and write our first worker. Let's get started.

Project

Sidekiq has excellent Rails support, but that's so easy to get going that I thought it would be more worthwhile to demonstrate the basics on a Plain Old Ruby project.

Project Setup

We'll make a new project:

mkdir sidekiq_playground
cd sidekiq_playground
vim Gemfile

We'll just pull in the sidekiq gem:

source "https://rubygems.org"

gem 'sidekiq'

and install it with Bundler:

bundle

Configuring Client and Server, and writing a Worker

So now we'll define a quick ruby script that configures Sidekiq and defines a worker.

vim worker.rb
# We require sidekiq, obviously.
require 'sidekiq'

# We'll configure the Sidekiq client to connect to Redis using a custom
# DB - this way we can run multiple apps on the same Redis without them
# stepping on each other

Sidekiq.configure_client do |config|
  config.redis = { db: 1 }
end

# We'll configure the Sidekiq server as well

Sidekiq.configure_server do |config|
  config.redis = { db: 1 }
end

# We'll make a new Worker class, including Sidekiq::Worker

class OurWorker
  include Sidekiq::Worker

  # To use OurWorker manually, just call the instance method:
  #
  #     OurWorker.new.perform("super_hard")
  #
  # The argument can be one of "super_hard", "hard", or anything else (which will
  # presume easy work)
  #
  # To start the sidekiq worker:
  #
  #     $ bundle exec sidekiq -r ./worker.rb
  #
  # To create a new job for it to perform in the background:
  #
  #     $ bundle exec irb -r ./worker.rb
  #     > OurWorker.perform_async "super_hard"

  # We'll define the perform method on our worker.  This is the entire
  # API you need to concern yourself with when writing Workers in Sidekiq.

  # Our method will take a single argument - a string representing the
  # complexity of the job.
  def perform(complexity)
    case complexity
    when "super_hard"
      # We'll just put sleep statements in to simulate different amounts of work.
      sleep 20
      puts "Really took quite a bit of effort"
    when "hard"
      sleep 10
      puts "That was a bit of work"
    else
      sleep 1
      puts "That wasn't a lot of effort"
    end
  end
end

OK, so there's a basic Sidekiq setup. Let's go ahead and run it. I've already got redis running locally - if you don't, you'll want to do that now.

Running the server and giving it work

First, we start the sidekiq worker process. I'll leave that running and split my screen real quick.

bundle exec sidekiq -r worker.rb

Now in the second terminal I'll launch an IRB session, requiring our ruby file:

bundle exec irb -r ./worker.rb

Now we can create jobs, and see them worked by the worker process:

OurWorker.perform_async("easy")

We can create a few jobs one after the other and see them get interleaved:

OurWorker.perform_async("super_hard")
OurWorker.perform_async("hard")
OurWorker.perform_async("hard")
OurWorker.perform_async("easy")
OurWorker.perform_async("easy")
OurWorker.perform_async("easy")
OurWorker.perform_async("easy")
OurWorker.perform_async("easy")

So as we let some of those finish, you can see that Sidekiq is capable of running multiple bits of ruby code concurrently, so that hard work doesn't block easy work.

Scheduling work

You can also schedule work in Sidekiq. For instance, you might want to send an email out 30 minutes after someone signed up for your service, if they hadn't finished registering. Sidekiq makes it very easy to schedule work. Let's perform an easy task in a few seconds:

OurWorker.perform_in(5, "easy")

Summary

In today's episode, we saw the basics of using Sidekiq from a plain old Ruby project. I hope you enjoyed it. See you soon!

Resources