wisdom #44
@@ -14,7 +14,9 @@ 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 util = require("util");
|
||||||
|
const exec = util.promisify(require("child_process").exec);
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
@@ -146,41 +148,41 @@ class NotifikasiWaService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async restartWhatsapp() {
|
async restartWhatsapp() {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
exec('pm2 jlist', (err, stdout) => {
|
|
||||||
if (err) return reject({ success: false, message: "Error list PM2" });
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const processes = JSON.parse(stdout);
|
const processName = "Whatsapp-API-notification";
|
||||||
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 { stdout } = await exec("pm2 jlist");
|
||||||
|
|
||||||
|
const processes = JSON.parse(stdout);
|
||||||
|
const waProcess = processes.find((p) => p.name === processName);
|
||||||
|
|
||||||
|
if (!waProcess) {
|
||||||
|
throw new Error("PM2 Not Found");
|
||||||
|
}
|
||||||
|
|
||||||
const processId = waProcess.pm_id;
|
const processId = waProcess.pm_id;
|
||||||
|
|
||||||
exec(`pm2 stop ${processId}`, () => {
|
await exec(`pm2 stop ${processId}`);
|
||||||
const paths = [
|
|
||||||
path.join(__dirname, "../../.wwebjs_auth"),
|
const pathsToDelete = [
|
||||||
path.join(__dirname, "../../.wwebjs_cache")
|
path.join(__dirname, ".wwebjs_auth"),
|
||||||
|
path.join(__dirname, ".wwebjs_cache"),
|
||||||
];
|
];
|
||||||
|
|
||||||
paths.forEach(dir => {
|
pathsToDelete.forEach((dir) => {
|
||||||
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
|
if (fs.existsSync(dir)) {
|
||||||
});
|
fs.rmSync(dir, { recursive: true, force: true });
|
||||||
|
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
await exec(`pm2 restart ${processId}`);
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: `WhatsApp has been restart.`,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user