Commit 6fe3d1b79b676f9e22667715bc8ccf71ac38bda5

Authored by Adhidarma Hadiwinoto
1 parent 35326bd19d
Exists in master

Try not specifying pid on calling systemd-notify

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

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