module Ekylibre::PluginSystem::Middleware::SidekiqMiddleware

Sidekiq client middleware adding the provided container to the job context so that all other middlewares can access it for configuring newly enqueued jobs.

Sidekiq server middleware adding the provided container to the job context. It also makes the container accessible through RequestStore for the legacy code to be able to access it globally.

Public Class Methods

setup(container:) click to toggle source

This method sets up sidekiq middlewares allowing to access the container natively inside jobs.

In addition, it also stores the job's container inside RequestStore so that the legacy code of Ekylibre can globally access the container when needed.

The client middleware is called when a job is enqueued, the server middleware when the job is executed.

# File lib/ekylibre/plugin_system/middleware/sidekiq_middleware.rb, line 15
def setup(container:)
  ::Sidekiq.configure_client do |config|
    configure_client(config)
  end

  ::Sidekiq.configure_server do |config|
    configure_client(config)

    configure_server(config, container: container)
  end
end

Private Class Methods

configure_client(config) click to toggle source
# File lib/ekylibre/plugin_system/middleware/sidekiq_middleware.rb, line 29
def configure_client(config)
  config.client_middleware do |chain|
    chain.add ClientMiddleware
  end
end
configure_server(config, container:) click to toggle source
# File lib/ekylibre/plugin_system/middleware/sidekiq_middleware.rb, line 35
def configure_server(config, container:)
  config.server_middleware do |chain|
    if defined?(::Sidekiq::Batch::Server)
      chain.insert_before ::Sidekiq::Batch::Server, ServerMiddleware, container
    else
      chain.add ServerMiddleware, container
    end
  end
end