repair: api restart wa

This commit is contained in:
2026-03-18 17:25:40 +07:00
parent d53f0cba33
commit f4b400fe02
2 changed files with 27 additions and 18 deletions

View File

@@ -28,6 +28,7 @@
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"crypto-js": "^4.2.0",
"dotenv": "^8.2.0",
"exceljs": "^4.4.0",

View File

@@ -15,8 +15,9 @@ const {
const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.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 execPromise = util.promisify(exec);
const fs = require("fs");
const path = require("path");
@@ -150,36 +151,43 @@ class NotifikasiWaService {
async restartWhatsapp() {
try {
const processName = "Whatsapp-API-notification";
const { stdout } = await exec("pm2 jlist");
const { stdout } = await execPromise("pm2 jlist");
const processes = JSON.parse(stdout);
const waProcess = processes.find((p) => p.name === processName);
if (!waProcess) {
throw new Error("PM2 Not Found");
}
if (!waProcess) throw new Error(`PM2 ${processName} not found`);
const processId = waProcess.pm_id;
const waProcessId = waProcess.pm_id;
const pathDelete = waProcess.pm2_env.pm_cwd;
await exec(`pm2 stop ${processId}`);
// const execLogs = (cmd) => {
// const p = exec(cmd);
// p.stdout.pipe(process.stdout);
// p.stderr.pipe(process.stderr);
// return new Promise((res) => p.on("exit", res));
// };
const pathsToDelete = [
path.join(__dirname, ".wwebjs_auth"),
path.join(__dirname, ".wwebjs_cache"),
// console.log(`stop proses id: ${waProcessId}`)
await execPromise(`start powershell -Command "pm2 stop ${waProcessId}"`);
const pathFolderDelete = [
path.join(pathDelete, ".wwebjs_auth"),
path.join(pathDelete, ".wwebjs_cache"),
];
pathsToDelete.forEach((dir) => {
// console.log(`path: ${pathDelete}`);
pathFolderDelete.forEach((dir) => {
if (fs.existsSync(dir)) {
// console.log(`path folder: ${dir}`);
fs.rmSync(dir, { recursive: true, force: true });
}
});
await exec(`pm2 restart ${processId}`);
return {
success: true,
message: `WhatsApp has been restart.`,
};
// console.log(`start proses id: ${waProcessId}`);
await execPromise(`start powershell -Command "pm2 restart ${waProcessId}"`);
return { success: true, message: "WhatsApp has been restarted." };
} catch (err) {
return err;
}