If you’ve been having trouble with the following error
could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)
in your sidekiq or sneakers worker, there are 2 ways I came accross to mitigate it.
- Your DB pool is probably lesser than the worker threads that are being spawned. Fix this by specifying the pool size to be equal to the number of threads in
production: url: <%= ENV["DATABASE_URL"] %> pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %>
- Open and close the connection explicitly as the worker process may not be releasing the connection on its own after it finishes. I’ve seen this happen too many times with sneakers (a rabbitmq adapter)
ActiveRecord::Base.connection_pool.with_connection do # your code end
Hope that helped.