wisdom #44
@@ -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();
|
||||
module.exports = new NotifikasiWaService();
|
||||
|
||||
Reference in New Issue
Block a user