Commit 51291f151035330867e287c720d6544b51312ed2
1 parent
b8aab1b12d
Exists in
master
Another method to notify systemd using systemd-notify binary
Showing 3 changed files with 85 additions and 18 deletions Side-by-side Diff
package-lock.json
... | ... | @@ -37,7 +37,8 @@ |
37 | 37 | "tektrans-logger": "^1.2.5", |
38 | 38 | "uniqid": "^4.1.1", |
39 | 39 | "url-join": "^4.0.1", |
40 | - "uuid": "^9.0.0" | |
40 | + "uuid": "^9.0.0", | |
41 | + "which": "^4.0.0" | |
41 | 42 | }, |
42 | 43 | "devDependencies": { |
43 | 44 | "auto-changelog": "^2.4.0", |
... | ... | @@ -2115,6 +2116,14 @@ |
2115 | 2116 | "url": "https://github.com/sponsors/ljharb" |
2116 | 2117 | } |
2117 | 2118 | }, |
2119 | + "node_modules/isexe": { | |
2120 | + "version": "3.1.1", | |
2121 | + "resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz", | |
2122 | + "integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==", | |
2123 | + "engines": { | |
2124 | + "node": ">=16" | |
2125 | + } | |
2126 | + }, | |
2118 | 2127 | "node_modules/isstream": { |
2119 | 2128 | "version": "0.1.2", |
2120 | 2129 | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", |
... | ... | @@ -3543,6 +3552,20 @@ |
3543 | 3552 | "webidl-conversions": "^3.0.0" |
3544 | 3553 | } |
3545 | 3554 | }, |
3555 | + "node_modules/which": { | |
3556 | + "version": "4.0.0", | |
3557 | + "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz", | |
3558 | + "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==", | |
3559 | + "dependencies": { | |
3560 | + "isexe": "^3.1.1" | |
3561 | + }, | |
3562 | + "bin": { | |
3563 | + "node-which": "bin/which.js" | |
3564 | + }, | |
3565 | + "engines": { | |
3566 | + "node": "^16.13.0 || >=18.0.0" | |
3567 | + } | |
3568 | + }, | |
3546 | 3569 | "node_modules/which-boxed-primitive": { |
3547 | 3570 | "version": "1.0.2", |
3548 | 3571 | "resolved": "https://registry.npmjs.org/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz", |
... | ... | @@ -5213,6 +5236,11 @@ |
5213 | 5236 | "call-bind": "^1.0.2" |
5214 | 5237 | } |
5215 | 5238 | }, |
5239 | + "isexe": { | |
5240 | + "version": "3.1.1", | |
5241 | + "resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz", | |
5242 | + "integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==" | |
5243 | + }, | |
5216 | 5244 | "isstream": { |
5217 | 5245 | "version": "0.1.2", |
5218 | 5246 | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", |
... | ... | @@ -6284,6 +6312,14 @@ |
6284 | 6312 | "webidl-conversions": "^3.0.0" |
6285 | 6313 | } |
6286 | 6314 | }, |
6315 | + "which": { | |
6316 | + "version": "4.0.0", | |
6317 | + "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz", | |
6318 | + "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==", | |
6319 | + "requires": { | |
6320 | + "isexe": "^3.1.1" | |
6321 | + } | |
6322 | + }, | |
6287 | 6323 | "which-boxed-primitive": { |
6288 | 6324 | "version": "1.0.2", |
6289 | 6325 | "resolved": "https://registry.npmjs.org/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz", |
package.json
sd-notify.js
... | ... | @@ -3,17 +3,50 @@ |
3 | 3 | |
4 | 4 | const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY'; |
5 | 5 | |
6 | +const childProcess = require('child_process'); | |
7 | +const which = require('which'); | |
6 | 8 | const logger = require('tektrans-logger'); |
7 | 9 | const matrix = require('./matrix'); |
8 | 10 | |
11 | +const hasSystemdNotifyBin = async () => { | |
12 | + const result = await which('systemd-notify', { nothrow: true }); | |
13 | + return result; | |
14 | +}; | |
15 | + | |
16 | +const notifyUseSystemdNotify = async (statusMsg) => { | |
17 | + try { | |
18 | + const notify = require('systemd-notify'); | |
19 | + | |
20 | + const status = statusMsg || 'Ready to go'; | |
21 | + | |
22 | + logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); | |
23 | + | |
24 | + await notify({ | |
25 | + ready: true, | |
26 | + status, | |
27 | + pid: process.pid, | |
28 | + }); | |
29 | + | |
30 | + return true; | |
31 | + } catch (e) { | |
32 | + logger.verbose(`${MODULE_NAME} 488B3245: Failed to notify using systemd-notify package`, { | |
33 | + why: e.message || e.toString(), | |
34 | + }); | |
35 | + | |
36 | + return false; | |
37 | + } | |
38 | +}; | |
39 | + | |
9 | 40 | const notifyUseSdNotify = () => { |
10 | 41 | try { |
11 | 42 | const sdNotify = require('sd-notify'); |
12 | 43 | |
44 | + logger.verbose(`${MODULE_NAME} A200BF49: Trying to notify systemd using sd-notify package`); | |
45 | + | |
13 | 46 | sdNotify.ready(); |
14 | 47 | matrix.systemd_notified = new Date(); |
15 | 48 | |
16 | - logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify module`); | |
49 | + logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify package`); | |
17 | 50 | } catch (e) { |
18 | 51 | logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`); |
19 | 52 | } |
... | ... | @@ -32,22 +65,19 @@ module.exports = async (statusMsg) => { |
32 | 65 | return; |
33 | 66 | } |
34 | 67 | |
35 | - try { | |
36 | - const notify = require('systemd-notify'); | |
68 | + const successOnUseSystemdNotify = await notifyUseSystemdNotify(statusMsg); | |
69 | + if (successOnUseSystemdNotify) { | |
70 | + return; | |
71 | + } | |
37 | 72 | |
38 | - const status = statusMsg || 'Ready to go'; | |
73 | + const useExec = await hasSystemdNotifyBin(); | |
74 | + if (useExec) { | |
75 | + logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`); | |
76 | + childProcess.exec('systemd-notify --ready'); | |
77 | + return; | |
78 | + } | |
39 | 79 | |
40 | - logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); | |
80 | + logger.verbose(`${MODULE_NAME} 9ADD3807: systemd-notify binary not found, fallback to sd-notify package`); | |
41 | 81 | |
42 | - await notify({ | |
43 | - ready: true, | |
44 | - status, | |
45 | - pid: process.pid, | |
46 | - }); | |
47 | - } catch (e) { | |
48 | - logger.verbose(`${MODULE_NAME} 488B3245: Trying to notify sd-notify package`, { | |
49 | - why: e.message || e.toString(), | |
50 | - }); | |
51 | - notifyUseSdNotify(); | |
52 | - } | |
82 | + notifyUseSdNotify(); | |
53 | 83 | }; |