timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
1.95k stars 153 forks source link

Feature Request: Close connection pool #318

Closed MadhuVK closed 1 year ago

MadhuVK commented 2 years ago

Hey there! Really grateful that you created and are continuing to support this lib.

I noticed that PgBoss.stop does not seem to close the pg.Pool instance, even when using the internal construction (i.e. not BYODB). I believe any socket handles will eventually be cleaned up, but it does create some noise in our tests which report "leaked handles."

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        4.727 s
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Furthermore, it also creates slowdown when shutting down our application as we need to wait for the pool resources to automatically clean up on the pool's idle timeout.

I was playing around, and it looks like the following addition alleviated the problem for me:

diff --git a/src/boss.js b/src/boss.js
index cb85eac..4116c9a 100644
--- a/src/boss.js
+++ b/src/boss.js
@@ -175,6 +175,10 @@ class Boss extends EventEmitter {
         await this.manager.offWork(queues.MONITOR_STATES)
       }

+      if (this.db.isOurs && this.db.opened) {
+        await this.db.close()
+      }
+
       this.stopped = true
     }
   }

Let me know if you would be open to including something like this - I'm unclear if there are other ramifications here to graceful shutdown.

MadhuVK commented 2 years ago

It looks like this may have been the existing behavior until the following recent commit: https://github.com/timgit/pg-boss/commit/2a684185161eade58791c0f568c57d513de86df7

I can see why this is useful, but I think it may also make sense to expose a db shutdown option as a one of the StopOptions.

timgit commented 2 years ago

I agree that an option should be provided in stop() to control the closing of the pool