Files
manjapro_v6/load/autoload.js
T
2026-08-20 18:39:37 +07:00

131 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
// === Konfigurasi dasar ===
const phpExecutablePath = '/www/server/php/74/bin/php';
// Daftar file PHP utama
const phpScriptPaths = [
path.join(__dirname, 'load_pesan.php'),
path.join(__dirname, 'load_tagihan_flip.php'),
path.join(__dirname, 'load_tagihan_paydisini.php'),
path.join(__dirname, 'load_pg_winpay_doku.php'),
];
// File tambahan (jalan tiap 1 detik)
const loadWAOfficialPath = path.join(__dirname, 'load_pesan_official.php');
// File tambahan (jalan tiap 1 detik)
const loadWebhookWAOfficialPath = path.join(__dirname, 'load_pesan_webhook.php');
// File tambahan (jalan tiap 2 detik)
const loadCreateFlipPath = path.join(__dirname, 'create_link_flip.php');
// === Pengaturan Log ===
const LOG_DIR = path.join(__dirname, 'logs');
const LOG_FILE = path.join(LOG_DIR, 'service.log');
const MAX_LOG_SIZE = 5 * 1024 * 1024; // 5MB per file
// Pastikan folder logs ada
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR);
// Fungsi menulis log efisien (rotasi otomatis)
function writeLog(message, isError = false) {
const timestamp = new Date().toISOString();
const formatted = `[${timestamp}] ${isError ? '[ERROR]' : '[INFO] '} ${message}\n`;
try {
// Cek ukuran file log
if (fs.existsSync(LOG_FILE) && fs.statSync(LOG_FILE).size > MAX_LOG_SIZE) {
const old = LOG_FILE.replace('.log', `_${Date.now()}.log`);
fs.renameSync(LOG_FILE, old);
}
fs.appendFileSync(LOG_FILE, formatted, { encoding: 'utf8' });
} catch (e) {
console.error('Log write failed:', e.message);
}
}
// Fungsi menjalankan file PHP
function runPhpScript(phpScriptPath) {
return new Promise((resolve) => {
const cmd = `${phpExecutablePath} ${phpScriptPath}`;
writeLog(`Running command: ${cmd}`);
exec(cmd, { maxBuffer: 10 * 1024 * 1024 }, (error, stdout, stderr) => {
if (error) {
writeLog(`Error executing PHP script ${phpScriptPath}: ${error.message}`, true);
} else if (stderr && stderr.trim()) {
writeLog(`PHP script ${phpScriptPath} stderr: ${stderr.trim()}`, true);
} else if (stdout && stdout.trim()) {
// Batasi panjang log agar efisien
const output = stdout.trim().substring(0, 300);
writeLog(`PHP script ${phpScriptPath} output: ${output}${stdout.length > 300 ? '...' : ''}`);
}
resolve();
});
});
}
// Fungsi utama untuk load semua script (5–15 detik)
async function runPhpScripts() {
await Promise.all(phpScriptPaths.map(runPhpScript));
const nextDelay = Math.floor(Math.random() * (10000 - 5000 + 1)) + 5000;
writeLog(`Next run in ${nextDelay / 1000} seconds`);
setTimeout(runPhpScripts, nextDelay);
}
// Fungsi loop khusus loadWAOfficial (tiap 1 detik)
async function runLoadWAOfficial() {
while (true) {
try {
await runPhpScript(loadWAOfficialPath);
} catch (error) {
writeLog(`Error saat menjalankan loadWAOfficial: ${error.message}`, true);
await new Promise(resolve => setTimeout(resolve, 5000));
continue; // ulang dari awal loop
}
await new Promise(resolve => setTimeout(resolve, 2000)); // jeda 1 detik
}
}
// Fungsi loop khusus loadWAOfficial (tiap 1 detik)
async function runLoadWebhookWAOfficial() {
while (true) {
try {
await runPhpScript(loadWebhookWAOfficialPath);
} catch (error) {
writeLog(`Error saat menjalankan loadWebhookWAOfficialPath: ${error.message}`, true);
await new Promise(resolve => setTimeout(resolve, 5000));
continue; // ulang dari awal loop
}
await new Promise(resolve => setTimeout(resolve, 1000)); // jeda 1 detik
}
}
// Fungsi loop khusus runLoadCreateflip (tiap 2 detik)
async function runLoadCreateflip() {
try {
await runPhpScript(loadCreateFlipPath);
} catch (error) {
writeLog(`Error runLoadCreateflip: ${error.message}`, true);
}
// Jalankan lagi setiap 2 detik SETELAH script sebelumnya benar-benar selesai
setTimeout(runLoadCreateflip, 2000);
}
// Tangani error global agar tidak crash
process.on('unhandledRejection', err => writeLog(`Unhandled Rejection: ${err.message}`, true));
process.on('uncaughtException', err => writeLog(`Uncaught Exception: ${err.message}`, true));
// Jalankan pertama kali
runPhpScripts();
runLoadWAOfficial();
runLoadWebhookWAOfficial();
runLoadCreateflip();