Postgres connection not closing after sidekiq Ruby script
This is a small Ruby script that runs under Sidekiq. It opens a connection with
db_connect = Sequel.connect(@db_credential, search_path: @namespace)
It never explicitly closes the connection; I think it shouldn't be necessary?
After the script has been run many times and they have all finished and the Sidekiq web dashboard does not show any running or pending tasks, Postgres shows 60 Sidekiq connections:
postgres=# select count(*) from pg_stat_activity where application_name like '%sidekiq%';
count
-------
60
(1 row)
The database is on localhost, so nothing else creates these connections.
psql 9.3.6, Sidekiq 3.3.3, Rails 4.0.0, ruby 2.1.1p76, continued 4.19.0, Ubuntu 14.04.2 LTS.
source to share
You can:
- Or use Sequel pool, connecting only once and maintaining the value
db_connect
between Sidekiq tasks - Or you can connect every time, but then you need to disconnect manually by calling the method
disconnect
( http://sequel.jeremyevans.net/rdoc/classes/Sequel/Database.html#method-i-disconnect ).
I believe the problem with your current approach is that you are creating a new connection pool each time you perform Sidekiq tasks by calling Sequel.connect
and those connections keep vibrating. It can take a long time before they collect the trash, if ever.
source to share