93 lines
3.3 KiB
JavaScript
93 lines
3.3 KiB
JavaScript
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, 'create_link_flip.php'),
|
||
path.join(__dirname, 'load_tagihan_paydisini.php'),
|
||
];
|
||
|
||
// File tambahan (jalan tiap 1 detik)
|
||
const loadWAOfficialPath = path.join(__dirname, 'load_pesan_official.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() {
|
||
try {
|
||
await runPhpScript(loadWAOfficialPath);
|
||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||
runLoadWAOfficial();
|
||
} catch (error) {
|
||
writeLog(`Error saat menjalankan loadWAOfficial: ${error.message}`, true);
|
||
setTimeout(runLoadWAOfficial, 5000);
|
||
}
|
||
}
|
||
|
||
// 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();
|