From f4b400fe021344ce07830158ce0f960a1e4d6a30 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Wed, 18 Mar 2026 17:25:40 +0700 Subject: [PATCH] repair: api restart wa --- package.json | 1 + services/notifikasi-wa.service.js | 44 ++++++++++++++++++------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 3bc2bdb..6e1f9ae 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index e976f19..a002607 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -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; }