From 51291f151035330867e287c720d6544b51312ed2 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <adhisimon@gmail.com> Date: Wed, 19 Jun 2024 19:27:13 +0700 Subject: [PATCH] Another method to notify systemd using systemd-notify binary --- package-lock.json | 38 +++++++++++++++++++++++++++++++++- package.json | 3 ++- sd-notify.js | 62 +++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 85 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 44a055b..99281ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,8 @@ "tektrans-logger": "^1.2.5", "uniqid": "^4.1.1", "url-join": "^4.0.1", - "uuid": "^9.0.0" + "uuid": "^9.0.0", + "which": "^4.0.0" }, "devDependencies": { "auto-changelog": "^2.4.0", @@ -2115,6 +2116,14 @@ "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", @@ -3543,6 +3552,20 @@ "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", @@ -5213,6 +5236,11 @@ "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", @@ -6284,6 +6312,14 @@ "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 9e1210d..dfaebb8 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ "tektrans-logger": "^1.2.5", "uniqid": "^4.1.1", "url-join": "^4.0.1", - "uuid": "^9.0.0" + "uuid": "^9.0.0", + "which": "^4.0.0" }, "devDependencies": { "auto-changelog": "^2.4.0", diff --git a/sd-notify.js b/sd-notify.js index 5f6139e..79953b3 100644 --- a/sd-notify.js +++ b/sd-notify.js @@ -3,17 +3,50 @@ const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY'; +const childProcess = require('child_process'); +const which = require('which'); const logger = require('tektrans-logger'); const matrix = require('./matrix'); +const hasSystemdNotifyBin = async () => { + const result = await which('systemd-notify', { nothrow: true }); + return result; +}; + +const notifyUseSystemdNotify = async (statusMsg) => { + try { + const notify = require('systemd-notify'); + + const status = statusMsg || 'Ready to go'; + + logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); + + await notify({ + ready: true, + status, + pid: process.pid, + }); + + return true; + } catch (e) { + logger.verbose(`${MODULE_NAME} 488B3245: Failed to notify using systemd-notify package`, { + why: e.message || e.toString(), + }); + + return false; + } +}; + const notifyUseSdNotify = () => { try { const sdNotify = require('sd-notify'); + logger.verbose(`${MODULE_NAME} A200BF49: Trying to notify systemd using sd-notify package`); + sdNotify.ready(); matrix.systemd_notified = new Date(); - logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify module`); + logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify package`); } catch (e) { logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`); } @@ -32,22 +65,19 @@ module.exports = async (statusMsg) => { return; } - try { - const notify = require('systemd-notify'); + const successOnUseSystemdNotify = await notifyUseSystemdNotify(statusMsg); + if (successOnUseSystemdNotify) { + return; + } - const status = statusMsg || 'Ready to go'; + const useExec = await hasSystemdNotifyBin(); + if (useExec) { + logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`); + childProcess.exec('systemd-notify --ready'); + return; + } - logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); + logger.verbose(`${MODULE_NAME} 9ADD3807: systemd-notify binary not found, fallback to sd-notify package`); - await notify({ - ready: true, - status, - pid: process.pid, - }); - } catch (e) { - logger.verbose(`${MODULE_NAME} 488B3245: Trying to notify sd-notify package`, { - why: e.message || e.toString(), - }); - notifyUseSdNotify(); - } + notifyUseSdNotify(); }; -- 1.9.0