do not log postgres-listener pings
authorJustin Wind <justin.wind+git@gmail.com>
Fri, 25 Jul 2025 18:05:22 +0000 (11:05 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Fri, 25 Jul 2025 18:05:22 +0000 (11:05 -0700)
lib/postgres-listener.js

index 9810eede1dfa324d0ac3ff8f5e0556fcf102b457..bc3478faeda4b0cd062d7a8f9bedc83a2b629302 100644 (file)
@@ -29,10 +29,21 @@ const defaultOptions = {
   reconnectTimes: -1, // infinite retries by default
 };
 
+/**
+ * @typedef {import('pg-promise')} PGPromise
+ */
+
 /**
  * Create a robust connection which listens to a notification channel.
  */
 class PostgresListener {
+  static PingPayload = 'ping';
+
+  /**
+   * @param {*} logger logger
+   * @param {PGPromise.Database} db db
+   * @param {*} options options
+   */
   constructor(logger, db, options) {
     this.logger = logger;
     this.db = db;
@@ -92,7 +103,7 @@ class PostgresListener {
     this.nextPingTimeout = setTimeout(async () => {
       try {
         if (this.connection) {
-          await this.connection.none('NOTIFY $(channel:name), $(payload)', { channel: this.options.channel, payload: 'ping' });
+          await this.connection.none('NOTIFY $(channel:name), $(payload)', { channel: this.options.channel, payload: this.constructor.PingPayload });
         }
       } catch (e) {
         this.logger.error(_scope, 'failed', { error: e });
@@ -109,13 +120,13 @@ class PostgresListener {
    */
   async _onNotification(data) {
     const _scope = _fileScope('_onNotification');
-    this.logger.debug(_scope, 'called', data);
 
     const { channel, payload } = data;
     // Ignore our own messages
-    if (payload === 'ping') {
+    if (payload === this.constructor.PingPayload) {
       return;
     }
+    this.logger.debug(_scope, 'called', data);
     await this.options.dataCallback(payload, channel);
   }