Compare commits

...

4 Commits

View File

@@ -14,7 +14,10 @@ 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 execPromise = util.promisify(exec);
const fs = require("fs");
const path = require("path");
@@ -107,6 +110,7 @@ class NotifikasiWaService {
const resultNotificationErrorUser =
await createNotificationErrorUserDb({
notification_error_id: param.idData,
contact_phone: param.userPhone,
contact_name: param.userName,
message_error_issue: param.bodyMessage,
@@ -146,41 +150,48 @@ 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" && "cod-whatsapp-notif";
const { stdout } = await execPromise("pm2 jlist");
const processes = JSON.parse(stdout);
const waProcess = processes.find((p) => p.name === processName);
try {
const processes = JSON.parse(stdout);
const waProcess = processes.find(p =>
p.name.toLowerCase().includes('whatsapp') ||
p.name.toLowerCase().includes('wa-api')
);
if (!waProcess) throw new Error(`PM2 ${processName} not found`);
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}`, () => {
const paths = [
path.join(__dirname, "../../.wwebjs_auth"),
path.join(__dirname, "../../.wwebjs_cache")
];
// console.log(`stop proses id: ${waProcessId}`)
await execPromise(`start powershell -Command "pm2 stop ${waProcessId}"`);
paths.forEach(dir => {
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
});
const pathFolderDelete = [
path.join(pathDelete, ".wwebjs_auth"),
path.join(pathDelete, ".wwebjs_cache"),
];
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 });
// console.log(`path: ${pathDelete}`);
pathFolderDelete.forEach((dir) => {
if (fs.existsSync(dir)) {
// console.log(`path folder: ${dir}`);
fs.rmSync(dir, { recursive: true, force: true });
}
});
});
// 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;
}
}
}