1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/docs/middleware.md
Mike Perham d985aa35f0 Adjust middleware, fixes #5371
Middleware now has access to the `config` object so we can provide local helper methods.
2022-06-08 12:31:25 -07:00

1.6 KiB

Middleware Changes in Sidekiq 7.0

With the internal refactoring coming in Sidekiq 7.0 it is necessary to make minor changes to the Middleware API.

tl;dr - middleware should now include Sidekiq::ClientMiddleware or Sidekiq::ServerMiddleware.

Currently the middleware API looks like this:

Existing Client API

Client middleware is run when pushing a job to Redis.

class Client
  def initialize(optional_args)
    @args = optional_args
  end
  def call(worker, job, queue, redis_pool)
    yield
  end
end

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add Client, optional_args
  end
end

Server

Server middleware is run around job execution.

class Server
  def initialize(optional_args)
    @args = optional_args
  end
  def call(worker, job, queue)
    Sidekiq.redis {|c| c.do_something }
    Sidekiq.logger.info { "Some message" }
    yield
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Server, optional_args
  end
end

Updated API

The updated middleware API requires the middleware class to include a helper module.

class Client
  include Sidekiq::ClientMiddleware

  def initialize(optional_args)
    @args = optional_args
  end
  def call(worker, job, queue, redis_pool)
    yield
  end
end
class Server
  include Sidekiq::ServerMiddleware

  def initialize(optional_args)
    @args = optional_args
  end
  def call(worker, job, queue)
    # note we no longer need to use the global Sidekiq module
    # to access Redis and the logger
    redis {|c| c.do_something }
    logger.info { "Some message" }
    yield
  end
end