From 69b66c6166070aa2cdaf4d09726e4c25f27996bb Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <adhisimon@gmail.com>
Date: Wed, 19 Jun 2024 19:38:08 +0700
Subject: [PATCH] Catch e on using systemd-notify binary

---
 package-lock.json | 38 +-------------------------------------
 package.json      |  3 +--
 sd-notify.js      | 42 ++++++++++++++++++++++++++++--------------
 3 files changed, 30 insertions(+), 53 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 67daccd..84a45cc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -37,8 +37,7 @@
         "tektrans-logger": "^1.2.5",
         "uniqid": "^4.1.1",
         "url-join": "^4.0.1",
-        "uuid": "^9.0.0",
-        "which": "^4.0.0"
+        "uuid": "^9.0.0"
       },
       "devDependencies": {
         "auto-changelog": "^2.4.0",
@@ -2116,14 +2115,6 @@
         "url": "https://github.com/sponsors/ljharb"
       }
     },
-    "node_modules/isexe": {
-      "version": "3.1.1",
-      "resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz",
-      "integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==",
-      "engines": {
-        "node": ">=16"
-      }
-    },
     "node_modules/isstream": {
       "version": "0.1.2",
       "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
@@ -3552,20 +3543,6 @@
         "webidl-conversions": "^3.0.0"
       }
     },
-    "node_modules/which": {
-      "version": "4.0.0",
-      "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz",
-      "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==",
-      "dependencies": {
-        "isexe": "^3.1.1"
-      },
-      "bin": {
-        "node-which": "bin/which.js"
-      },
-      "engines": {
-        "node": "^16.13.0 || >=18.0.0"
-      }
-    },
     "node_modules/which-boxed-primitive": {
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz",
@@ -5236,11 +5213,6 @@
         "call-bind": "^1.0.2"
       }
     },
-    "isexe": {
-      "version": "3.1.1",
-      "resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz",
-      "integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ=="
-    },
     "isstream": {
       "version": "0.1.2",
       "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
@@ -6312,14 +6284,6 @@
         "webidl-conversions": "^3.0.0"
       }
     },
-    "which": {
-      "version": "4.0.0",
-      "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz",
-      "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==",
-      "requires": {
-        "isexe": "^3.1.1"
-      }
-    },
     "which-boxed-primitive": {
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz",
diff --git a/package.json b/package.json
index 297b50c..c0024f8 100644
--- a/package.json
+++ b/package.json
@@ -48,8 +48,7 @@
     "tektrans-logger": "^1.2.5",
     "uniqid": "^4.1.1",
     "url-join": "^4.0.1",
-    "uuid": "^9.0.0",
-    "which": "^4.0.0"
+    "uuid": "^9.0.0"
   },
   "devDependencies": {
     "auto-changelog": "^2.4.0",
diff --git a/sd-notify.js b/sd-notify.js
index 79953b3..f48516c 100644
--- a/sd-notify.js
+++ b/sd-notify.js
@@ -3,15 +3,11 @@
 
 const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY';
 
-const childProcess = require('child_process');
-const which = require('which');
+const util = require('util');
 const logger = require('tektrans-logger');
 const matrix = require('./matrix');
 
-const hasSystemdNotifyBin = async () => {
-    const result = await which('systemd-notify', { nothrow: true });
-    return result;
-};
+const exec = util.promisify(require('node:child_process').exec);
 
 const notifyUseSystemdNotify = async (statusMsg) => {
     try {
@@ -27,6 +23,8 @@ const notifyUseSystemdNotify = async (statusMsg) => {
             pid: process.pid,
         });
 
+        logger.info(`${MODULE_NAME} B905A857: Systemd ready notification has been sent using systemd-notify package`);
+
         return true;
     } catch (e) {
         logger.verbose(`${MODULE_NAME} 488B3245: Failed to notify using systemd-notify package`, {
@@ -47,8 +45,28 @@ const notifyUseSdNotify = () => {
         matrix.systemd_notified = new Date();
 
         logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify package`);
+
+        return true;
     } catch (e) {
         logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`);
+        return false;
+    }
+};
+
+const notifyUseBin = async () => {
+    try {
+        logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`);
+        await exec('systemd-notify --ready');
+        logger.info(`${MODULE_NAME} B58921FF: Systemd ready notification has been sent using systemd-notify bin`);
+
+        return true;
+    } catch (e) {
+        logger.verbose(`${MODULE_NAME} 75237B65: Failed to notify using systemd-notify bin`, {
+            eCode: e.code,
+            eMessage: e.message || e.toString(),
+        });
+
+        return false;
     }
 };
 
@@ -65,19 +83,15 @@ module.exports = async (statusMsg) => {
         return;
     }
 
-    const successOnUseSystemdNotify = await notifyUseSystemdNotify(statusMsg);
-    if (successOnUseSystemdNotify) {
+    const successOnUsingSystemdNotify = await notifyUseSystemdNotify(statusMsg);
+    if (successOnUsingSystemdNotify) {
         return;
     }
 
-    const useExec = await hasSystemdNotifyBin();
-    if (useExec) {
-        logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`);
-        childProcess.exec('systemd-notify --ready');
+    const successOnUsingBin = await notifyUseBin();
+    if (successOnUsingBin) {
         return;
     }
 
-    logger.verbose(`${MODULE_NAME} 9ADD3807: systemd-notify binary not found, fallback to sd-notify package`);
-
     notifyUseSdNotify();
 };
-- 
1.9.0