fix minor issue with postgres listener dealing with connection errors and shutdown
authorJustin Wind <justin.wind+git@gmail.com>
Mon, 7 Feb 2022 22:33:41 +0000 (14:33 -0800)
committerJustin Wind <justin.wind+git@gmail.com>
Mon, 7 Feb 2022 22:33:41 +0000 (14:33 -0800)
server.js
src/db/postgres/listener.js

index cef54a918fbce09797f290bd3a3f8fe49176e577..49ce8f6ea6971169ae958ef0c4d4d9227133a8d2 100644 (file)
--- a/server.js
+++ b/server.js
@@ -26,12 +26,13 @@ const ADDR = process.env.LISTEN_ADDR || '127.0.0.1';
       service.dispatch(req, res);
     }).listen(PORT, ADDR, (err) => {
       if (err) {
-        logger.error(_scope, 'error starting server', err);
+        logger.error(_scope, 'error creating server', err);
         throw err;
       }
       logger.info(_scope, 'server started', { version, listenAddress: ADDR, listenPort: PORT });
     });
   } catch (e) {
     (logger || console).error(_scope, 'error starting server', e);
+    db && db.listener && await db.listener.stop();
   }
 })();
\ No newline at end of file
index 6ad387aa49ea2e11a91e29a6de60094eea4024aa..4baf5d082de3bc1d507ef441ddc795af014e8470 100644 (file)
@@ -54,7 +54,7 @@ class PostgresListener {
       delete this.reconnectPending;
     }
     if (this.connection) {
-      this.connection.client.removeListener(this.notificationEventName, this.onNotificationBound);
+      this.connection.client.removeListener(this.notificationEventName, this._onNotificationBound);
       this.connection.done();
       this.connection = null;
       await this.options.connectionLostCallback();
@@ -106,7 +106,7 @@ class PostgresListener {
     this.logger.error(_scope, 'listener connection lost', { error, event });
     this.connection = null;
     try {
-      event.client.removeListener(this.notificationEventName, this.onNotificationBound);
+      event.client.removeListener(this.notificationEventName, this._onNotificationBound);
     } catch (e) {
       this.logger.error(_scope, 'failed to remove listener', { error: e });
       // That's okay, it was probably just gone anyhow.