Search code examples
multithreadingrabbitmqunicornsidekiqbunny

Singleton for sidekiq threads possible?


So I need to be able to create a single RabbitMQ connection and channel per Sidekiq thread because I run out of RabbitMQ connections if I don't and because the docs suggest it. The docs show how to do it with Unicorn:

before_fork do |server, worker|
  $rabbitmq_connection.close if $rabbitmq_connection
end

after_fork do |server, worker|
  # the following is *required* for Rails + "preload_app true",
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

    $rabbitmq_connection = Bunny.new
    $rabbitmq_connection.start

    $rabbitmq_channel    = $rabbitmq_connection.create_channel
  end
end

Is it possible to do something similar for Sidekiq threads? Is there something I can do in Sidekiq.server_configure? It looks like this is where Sidekiq starts a thread but I don't see anyway to hook into the start/stop?


Solution

  • Create your own pool of connections to RabbitMQ, as detailed here:

    https://github.com/mperham/sidekiq/wiki/Advanced-Options#connection-pooling