From d53f0cba33e78e2e99cfb3562d9b8d51de314137 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Mon, 16 Mar 2026 14:22:19 +0700 Subject: [PATCH] repair restart api: replace name in pm2 list --- services/notifikasi-wa.service.js | 60 ++++++++++++++++--------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index 81d38ca..e976f19 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -14,7 +14,9 @@ const { } = require("../db/notification_wa.db"); 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 fs = require("fs"); const path = require("path"); @@ -146,42 +148,42 @@ class NotifikasiWaService { } async restartWhatsapp() { - return new Promise((resolve, reject) => { - exec('pm2 jlist', (err, stdout) => { - if (err) return reject({ success: false, message: "Error list PM2" }); + try { + const processName = "Whatsapp-API-notification"; - try { - const processes = JSON.parse(stdout); - const waProcess = processes.find(p => - p.name.toLowerCase().includes('whatsapp') || - p.name.toLowerCase().includes('wa-api') - ); + const { stdout } = await exec("pm2 jlist"); - 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 paths = [ - path.join(__dirname, "../../.wwebjs_auth"), - path.join(__dirname, "../../.wwebjs_cache") - ]; + const processId = waProcess.pm_id; - paths.forEach(dir => { - if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true }); - }); + await exec(`pm2 stop ${processId}`); - exec(`pm2 restart ${processId}`, (reErr) => { - if (reErr) return reject({ success: false, message: "Gagal restart" }); - resolve({ success: true, message: `WA has been restart.` }); - }); - }); - } catch (e) { - reject({ success: false, message: "JSON Parse Error: " + e.message }); + const pathsToDelete = [ + path.join(__dirname, ".wwebjs_auth"), + path.join(__dirname, ".wwebjs_cache"), + ]; + + pathsToDelete.forEach((dir) => { + 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; + } } } -module.exports = new NotifikasiWaService(); \ No newline at end of file +module.exports = new NotifikasiWaService();