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