Commit bfa52afc143873bf0bc24cd1ec9e730610b44f45
1 parent
8926a740b4
Exists in
master
Try not to use systemd-notify package
Showing 1 changed file with 7 additions and 3 deletions Inline Diff
sd-notify.js
1 | /* eslint-disable global-require */ | 1 | /* eslint-disable global-require */ |
2 | /* eslint-disable import/no-extraneous-dependencies */ | 2 | /* eslint-disable import/no-extraneous-dependencies */ |
3 | 3 | ||
4 | const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY'; | 4 | const MODULE_NAME = 'KOMODO-SDK.SD-NOTIFY'; |
5 | 5 | ||
6 | const USE_SYSTEMD_NOTIFY_PACKAGE = false; | ||
7 | |||
6 | const util = require('util'); | 8 | const util = require('util'); |
7 | const logger = require('tektrans-logger'); | 9 | const logger = require('tektrans-logger'); |
8 | const matrix = require('./matrix'); | 10 | const matrix = require('./matrix'); |
9 | 11 | ||
10 | const exec = util.promisify(require('child_process').exec); | 12 | const exec = util.promisify(require('child_process').exec); |
11 | 13 | ||
12 | const notifyUseSystemdNotify = async (statusMsg) => { | 14 | const notifyUseSystemdNotify = async (statusMsg) => { |
13 | try { | 15 | try { |
14 | const notify = require('systemd-notify'); | 16 | const notify = require('systemd-notify'); |
15 | 17 | ||
16 | const status = statusMsg || 'Ready to go'; | 18 | const status = statusMsg || 'Ready to go'; |
17 | 19 | ||
18 | logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); | 20 | logger.verbose(`${MODULE_NAME} 3B8DF3BC: Trying to notify systemd using systemd-notify package`, { status }); |
19 | 21 | ||
20 | await notify({ | 22 | await notify({ |
21 | ready: true, | 23 | ready: true, |
22 | status, | 24 | status, |
23 | // pid: process.pid, | 25 | // pid: process.pid, |
24 | }); | 26 | }); |
25 | 27 | ||
26 | logger.info(`${MODULE_NAME} B905A857: Systemd ready notification has been sent using systemd-notify package`); | 28 | logger.info(`${MODULE_NAME} B905A857: Systemd ready notification has been sent using systemd-notify package`); |
27 | 29 | ||
28 | return true; | 30 | return true; |
29 | } catch (e) { | 31 | } catch (e) { |
30 | logger.verbose(`${MODULE_NAME} 488B3245: Failed to notify using systemd-notify package`, { | 32 | logger.verbose(`${MODULE_NAME} 488B3245: Failed to notify using systemd-notify package`, { |
31 | why: e.message || e.toString(), | 33 | why: e.message || e.toString(), |
32 | }); | 34 | }); |
33 | 35 | ||
34 | return false; | 36 | return false; |
35 | } | 37 | } |
36 | }; | 38 | }; |
37 | 39 | ||
38 | const notifyUseSdNotify = () => { | 40 | const notifyUseSdNotify = () => { |
39 | try { | 41 | try { |
40 | const sdNotify = require('sd-notify'); | 42 | const sdNotify = require('sd-notify'); |
41 | 43 | ||
42 | logger.verbose(`${MODULE_NAME} A200BF49: Trying to notify systemd using sd-notify package`); | 44 | logger.verbose(`${MODULE_NAME} A200BF49: Trying to notify systemd using sd-notify package`); |
43 | 45 | ||
44 | sdNotify.ready(); | 46 | sdNotify.ready(); |
45 | matrix.systemd_notified = new Date(); | 47 | matrix.systemd_notified = new Date(); |
46 | 48 | ||
47 | logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify package`); | 49 | logger.info(`${MODULE_NAME} 701F8400: Systemd ready notification has been sent using sd-notify package`); |
48 | 50 | ||
49 | return true; | 51 | return true; |
50 | } catch (e) { | 52 | } catch (e) { |
51 | logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`); | 53 | logger.warn(`${MODULE_NAME} A6C99938: Optional dependency not found: sd-notify`); |
52 | return false; | 54 | return false; |
53 | } | 55 | } |
54 | }; | 56 | }; |
55 | 57 | ||
56 | const notifyUseBin = async () => { | 58 | const notifyUseBin = async () => { |
57 | try { | 59 | try { |
58 | logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`); | 60 | logger.verbose(`${MODULE_NAME} FFBCF4E3: Trying to notify systemd using systemd-notify bin`); |
59 | await exec('systemd-notify --ready'); | 61 | await exec('systemd-notify --ready'); |
60 | logger.info(`${MODULE_NAME} B58921FF: Systemd ready notification has been sent using systemd-notify bin`); | 62 | logger.info(`${MODULE_NAME} B58921FF: Systemd ready notification has been sent using systemd-notify bin`); |
61 | 63 | ||
62 | return true; | 64 | return true; |
63 | } catch (e) { | 65 | } catch (e) { |
64 | logger.verbose(`${MODULE_NAME} 75237B65: Failed to notify using systemd-notify bin`, { | 66 | logger.verbose(`${MODULE_NAME} 75237B65: Failed to notify using systemd-notify bin`, { |
65 | eCode: e.code, | 67 | eCode: e.code, |
66 | eMessage: e.message || e.toString(), | 68 | eMessage: e.message || e.toString(), |
67 | }); | 69 | }); |
68 | 70 | ||
69 | return false; | 71 | return false; |
70 | } | 72 | } |
71 | }; | 73 | }; |
72 | 74 | ||
73 | /** | 75 | /** |
74 | * | 76 | * |
75 | * @param {string} statusMsg | 77 | * @param {string} statusMsg |
76 | * @returns | 78 | * @returns |
77 | */ | 79 | */ |
78 | module.exports = async (statusMsg) => { | 80 | module.exports = async (statusMsg) => { |
79 | const { ppid } = process; | 81 | const { ppid } = process; |
80 | 82 | ||
81 | if (ppid !== 1) { | 83 | if (ppid !== 1) { |
82 | logger.verbose(`${MODULE_NAME} 74A5B2AF: No need to notify systemd`, { ppid }); | 84 | logger.verbose(`${MODULE_NAME} 74A5B2AF: No need to notify systemd`, { ppid }); |
83 | return; | 85 | return; |
84 | } | 86 | } |
85 | 87 | ||
86 | const successOnUsingSystemdNotify = await notifyUseSystemdNotify(statusMsg); | 88 | if (USE_SYSTEMD_NOTIFY_PACKAGE) { |
87 | if (successOnUsingSystemdNotify) { | 89 | const successOnUsingSystemdNotify = await notifyUseSystemdNotify(statusMsg); |
88 | return; | 90 | if (successOnUsingSystemdNotify) { |
91 | return; | ||
92 | } | ||
89 | } | 93 | } |
90 | 94 | ||
91 | const successOnUsingBin = await notifyUseBin(); | 95 | const successOnUsingBin = await notifyUseBin(); |
92 | if (successOnUsingBin) { | 96 | if (successOnUsingBin) { |
93 | return; | 97 | return; |
94 | } | 98 | } |
95 | 99 | ||
96 | notifyUseSdNotify(); | 100 | notifyUseSdNotify(); |
97 | }; | 101 | }; |
98 | 102 |