Properly detect PM2 manager process instead of using a hardcoded ID

This commit is contained in:
Essem 2022-10-30 23:16:03 -05:00
parent 341f89cda5
commit c3b97c121e
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
2 changed files with 56 additions and 42 deletions

13
app.js
View file

@ -164,12 +164,18 @@ esmBot ${esmBotVersion} (${process.env.GIT_REV})
// PM2-specific handling
if (process.env.PM2_USAGE) {
// callback hell :)
pm2.launchBus((err, pm2Bus) => {
if (err) {
logger.error(err);
return;
}
pm2.list((err, list) => {
if (err) {
logger.error(err);
return;
}
const managerProc = list.filter((v) => v.name === "esmBot-manager")[0];
pm2Bus.on("process:msg", async (packet) => {
switch (packet.data?.type) {
case "reload":
@ -189,8 +195,8 @@ esmBot ${esmBotVersion} (${process.env.GIT_REV})
endBroadcast(client);
break;
case "serverCounts":
pm2.sendDataToProcessId(0, {
id: 0,
pm2.sendDataToProcessId(managerProc.pm_id, {
id: managerProc.pm_id,
type: "process:msg",
data: {
type: "serverCounts",
@ -205,6 +211,7 @@ esmBot ${esmBotVersion} (${process.env.GIT_REV})
}
});
});
});
}
// connect to lavalink

View file

@ -98,8 +98,14 @@ export function getServers(bot) {
};
pm2Bus.on("process:msg", listener);
});
pm2.sendDataToProcessId(0, {
id: 0,
pm2.list((err, list) => {
if (err) {
reject(err);
return;
}
const managerProc = list.filter((v) => v.name === "esmBot-manager")[0];
pm2.sendDataToProcessId(managerProc.pm_id, {
id: managerProc.pm_id,
type: "process:msg",
data: {
type: "getCount"
@ -108,6 +114,7 @@ export function getServers(bot) {
}, (err) => {
if (err) reject(err);
});
});
} else {
resolve(bot.guilds.size);
}