diff --git a/package-lock.json b/package-lock.json index 6414bd8..8295476 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,13 +37,15 @@ "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", "eslint": "^8.6.0", "eslint-config-airbnb-base": "^15.0.0", - "eslint-plugin-import": "^2.25.4" + "eslint-plugin-import": "^2.25.4", + "systemd-notify": "^1.0.0" }, "optionalDependencies": { "sd-notify": "^2.8.0" @@ -752,6 +754,27 @@ "node": ">= 8" } }, + "node_modules/cross-spawn/node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "dev": true + }, + "node_modules/cross-spawn/node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/crypt": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", @@ -2094,10 +2117,12 @@ } }, "node_modules/isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", - "dev": true + "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", @@ -3261,6 +3286,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/systemd-notify": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/systemd-notify/-/systemd-notify-1.0.0.tgz", + "integrity": "sha512-KP/Qn747dzdOgc7BEk3qd3EZWauwJ+aupqo3Upz134r4WZQJhU+qyJFnGMsMzj/NQf7XfPFq/LQogpqZuZmNXA==", + "dev": true + }, "node_modules/tektrans-logger": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/tektrans-logger/-/tektrans-logger-1.2.5.tgz", @@ -3522,18 +3553,17 @@ } }, "node_modules/which": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", - "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz", + "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==", "dependencies": { - "isexe": "^2.0.0" + "isexe": "^3.1.1" }, "bin": { - "node-which": "bin/node-which" + "node-which": "bin/which.js" }, "engines": { - "node": ">= 8" + "node": "^16.13.0 || >=18.0.0" } }, "node_modules/which-boxed-primitive": { @@ -4190,6 +4220,23 @@ "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" + }, + "dependencies": { + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "dev": true + }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + } } }, "crypt": { @@ -5190,10 +5237,9 @@ } }, "isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", - "dev": true + "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", @@ -6052,6 +6098,12 @@ "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", "dev": true }, + "systemd-notify": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/systemd-notify/-/systemd-notify-1.0.0.tgz", + "integrity": "sha512-KP/Qn747dzdOgc7BEk3qd3EZWauwJ+aupqo3Upz134r4WZQJhU+qyJFnGMsMzj/NQf7XfPFq/LQogpqZuZmNXA==", + "dev": true + }, "tektrans-logger": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/tektrans-logger/-/tektrans-logger-1.2.5.tgz", @@ -6261,12 +6313,11 @@ } }, "which": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", - "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz", + "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==", "requires": { - "isexe": "^2.0.0" + "isexe": "^3.1.1" } }, "which-boxed-primitive": { diff --git a/package.json b/package.json index b304a97..d6fe8c0 100644 --- a/package.json +++ b/package.json @@ -48,13 +48,15 @@ "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", "eslint": "^8.6.0", "eslint-config-airbnb-base": "^15.0.0", - "eslint-plugin-import": "^2.25.4" + "eslint-plugin-import": "^2.25.4", + "systemd-notify": "^1.0.0" }, "optionalDependencies": { "sd-notify": "^2.8.0" diff --git a/sd-notify.js b/sd-notify.js index 829bc9f..e70d24a 100644 --- a/sd-notify.js +++ b/sd-notify.js @@ -6,15 +6,45 @@ const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY'; const logger = require('tektrans-logger'); const matrix = require('./matrix'); -module.exports = () => { +const notifyUseSdNotify = () => { try { - const notify = require('sd-notify'); + const sdNotify = require('sd-notify'); - notify.ready(); + sdNotify.ready(); matrix.systemd_notified = new Date(); - logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent`); + logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify module`); } catch (e) { logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`); } }; + +/** + * + * @param {string} statusMsg + * @returns + */ +module.exports = async (statusMsg) => { + const { ppid } = process; + + if (ppid !== 1) { + logger.verbose(`${MODULE_NAME} 74A5B2AF: No need to notify systemd`, { ppid }); + return; + } + + 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, + }); + } catch (e) { + logger.verbose(`${MODULE_NAME} 488B3245: Trying to notify sd-notify package`); + notifyUseSdNotify(); + } +};