Skip to content

Middleware

Sebastian Delmont edited this page Feb 26, 2018 · 63 revisions

Sidekiq has a similar notion of middleware to Rack: these are small bits of code that can implement functionality. Sidekiq breaks middleware into client-side and server-side.

  • Server-side middleware runs 'around' job processing.
  • Client-side middleware runs before the pushing of the job to Redis and allows you to modify/stop the job before it gets pushed.

Writing your own middleware

Server-side middleware

Here is a simple server-side middleware which does something upon any exception from any job:

class Sidekiq::Middleware::Server::ErrorLogger
  def call(worker, job, queue)
    begin
      yield
    rescue => ex
      puts ex.message
    end
  end
end

Your middleware will be called with the worker instance which will process the job along with the full Hash which represents the job to process and the name of the queue it was pulled from.

class AcmeCo::MyMiddleware
  def initialize(options=nil)
    # options == { :foo => 1, :bar => 2 }
  end
  def call(worker, msg, queue)
    yield
  end
end

You then register your middleware as part of the chain:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add AcmeCo::MyMiddleware, :foo => 1, :bar => 2
  end
end

I'd suggest putting this code in config/initializers/sidekiq.rb in your Rails app.

Client-side middleware

Here is an example of some client-side middleware. Client middleware may receive the class argument as a Class object or a String containing the name of the class. You can use client-side middleware to add job metadata to the job before pushing it to Redis. The same job data will be available to the server-side middleware before the job is executed, if you need to set up some global state, e.g. current locale, current tenant in a multi-tenant app, etc.

class AcmeCo::MyClientMiddleware
  def call(worker_class, job, queue, redis_pool)
    # return false/nil to stop the job from going to redis
    return false if queue != 'default'
    job['customer'] = Customer.current_id
    yield
  end
end

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add AcmeCo::MyClientMiddleware
  end
end

Sometimes client-side middleware should be registered in both places

The jobs running in the Sidekiq server can themselves push new jobs to Sidekiq, thus acting as clients. You must configure your client middleware within the configure_server block also in that case:

# you'll need the same Sidekiq.configure_client block as in the definition above, and...

Sidekiq.configure_server do |config|
  config.client_middleware do |chain|
    chain.add AcmeCo::MyClientMiddleware
  end
  config.server_middleware do |chain|
    chain.add AcmeCo::MyMiddleware, :foo => 1, :bar => 2
  end
end

Obscure Scenario: If you share a single redis instance between multiple services, and use client-side middleware on your Sidekiq server, be warned that it might have to deal with workers from unexpected queues. Sidekiq uses a singe redis set to handle scheduled workers, and any server might requeue a worker that's due to be processed, even if it's not for a queue it was configured to process. When it does so, your middleware might be passed a worker name that does not exist in the current ruby context and you might see uninitialized constant exceptions or similar errors.

Notes

Sidekiq 5+ ships with no middleware out of the box. Various gems and services will install middleware to track jobs or provide additional features.

If you need to remove a middleware for some reason, you can do this in your configuration:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.remove Some::Middleware
  end
end

Sidekiq will print out the configured client and server middlewares when you start it with -v.

Previous: API Next: Resque Compatibility