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