Compare commits

...

4 Commits

View File

@@ -14,7 +14,10 @@ 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 { exec } = require("child_process");
const util = require("util");
const execPromise = util.promisify(exec);
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
@@ -107,6 +110,7 @@ class NotifikasiWaService {
const resultNotificationErrorUser = const resultNotificationErrorUser =
await createNotificationErrorUserDb({ await createNotificationErrorUserDb({
notification_error_id: param.idData, notification_error_id: param.idData,
contact_phone: param.userPhone, contact_phone: param.userPhone,
contact_name: param.userName, contact_name: param.userName,
message_error_issue: param.bodyMessage, message_error_issue: param.bodyMessage,
@@ -146,41 +150,48 @@ class NotifikasiWaService {
} }
async restartWhatsapp() { async restartWhatsapp() {
return new Promise((resolve, reject) => { try {
exec('pm2 jlist', (err, stdout) => { const processName = "Whatsapp-API-notification" && "cod-whatsapp-notif";
if (err) return reject({ success: false, message: "Error list PM2" }); const { stdout } = await execPromise("pm2 jlist");
const processes = JSON.parse(stdout);
const waProcess = processes.find((p) => p.name === processName);
try { if (!waProcess) throw new Error(`PM2 ${processName} not found`);
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 waProcessId = waProcess.pm_id;
const pathDelete = waProcess.pm2_env.pm_cwd;
const processId = waProcess.pm_id; // 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));
// };
exec(`pm2 stop ${processId}`, () => { // console.log(`stop proses id: ${waProcessId}`)
const paths = [ await execPromise(`start powershell -Command "pm2 stop ${waProcessId}"`);
path.join(__dirname, "../../.wwebjs_auth"),
path.join(__dirname, "../../.wwebjs_cache")
];
paths.forEach(dir => { const pathFolderDelete = [
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true }); path.join(pathDelete, ".wwebjs_auth"),
}); path.join(pathDelete, ".wwebjs_cache"),
];
exec(`pm2 restart ${processId}`, (reErr) => { // console.log(`path: ${pathDelete}`);
if (reErr) return reject({ success: false, message: "Gagal restart" });
resolve({ success: true, message: `WA has been restart.` }); pathFolderDelete.forEach((dir) => {
}); if (fs.existsSync(dir)) {
}); // console.log(`path folder: ${dir}`);
} catch (e) { fs.rmSync(dir, { recursive: true, force: true });
reject({ success: false, message: "JSON Parse Error: " + e.message });
} }
}); });
});
// console.log(`start proses id: ${waProcessId}`);
await execPromise(`start powershell -Command "pm2 start ${waProcessId}"`);
return { success: true, message: "WhatsApp has been restarted." };
} catch (err) {
return err;
}
} }
} }