repair restart api: replace name in pm2 list

This commit is contained in:
2026-03-16 14:22:19 +07:00
parent a20d36c012
commit d53f0cba33

View File

@@ -14,7 +14,9 @@ const {
} = require("../db/notification_wa.db"); } = require("../db/notification_wa.db");
const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.db"); const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.db");
const { getDeviceNotificationByIdDb } = require("../db/notification_error.db"); const { getDeviceNotificationByIdDb } = require("../db/notification_error.db");
const { exec } = require("child_process");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
@@ -146,41 +148,41 @@ class NotifikasiWaService {
} }
async restartWhatsapp() { async restartWhatsapp() {
return new Promise((resolve, reject) => { try {
exec('pm2 jlist', (err, stdout) => { const processName = "Whatsapp-API-notification";
if (err) return reject({ success: false, message: "Error list PM2" });
try { const { stdout } = await exec("pm2 jlist");
const processes = JSON.parse(stdout);
const waProcess = processes.find(p =>
p.name.toLowerCase().includes('whatsapp') ||
p.name.toLowerCase().includes('wa-api')
);
if (!waProcess) return reject({ success: false, message: "PM2 List PM2 Not Found" }); const processes = JSON.parse(stdout);
const waProcess = processes.find((p) => p.name === processName);
const processId = waProcess.pm_id; if (!waProcess) {
throw new Error("PM2 Not Found");
}
exec(`pm2 stop ${processId}`, () => { const processId = waProcess.pm_id;
const paths = [
path.join(__dirname, "../../.wwebjs_auth"),
path.join(__dirname, "../../.wwebjs_cache")
];
paths.forEach(dir => { await exec(`pm2 stop ${processId}`);
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
});
exec(`pm2 restart ${processId}`, (reErr) => { const pathsToDelete = [
if (reErr) return reject({ success: false, message: "Gagal restart" }); path.join(__dirname, ".wwebjs_auth"),
resolve({ success: true, message: `WA has been restart.` }); path.join(__dirname, ".wwebjs_cache"),
}); ];
});
} catch (e) { pathsToDelete.forEach((dir) => {
reject({ success: false, message: "JSON Parse Error: " + e.message }); if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
} }
}); });
});
await exec(`pm2 restart ${processId}`);
return {
success: true,
message: `WhatsApp has been restart.`,
};
} catch (err) {
return err;
}
} }
} }