Pisah server penyimpanan dan update payment gateway
This commit is contained in:
+58
-24
@@ -1,10 +1,11 @@
|
||||
const { exec } = require('child_process');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// Jalur ke executable PHP
|
||||
// === Konfigurasi dasar ===
|
||||
const phpExecutablePath = '/www/server/php/74/bin/php';
|
||||
|
||||
// Daftar jalur ke file PHP
|
||||
// Daftar file PHP utama
|
||||
const phpScriptPaths = [
|
||||
path.join(__dirname, 'load_pesan.php'),
|
||||
path.join(__dirname, 'load_tagihan_flip.php'),
|
||||
@@ -12,47 +13,80 @@ const phpScriptPaths = [
|
||||
path.join(__dirname, 'load_tagihan_paydisini.php'),
|
||||
];
|
||||
|
||||
|
||||
// --- File tambahan setiap 1 detik ---
|
||||
// File tambahan (jalan tiap 1 detik)
|
||||
const loadWAOfficialPath = path.join(__dirname, 'load_pesan_official.php');
|
||||
|
||||
// Fungsi untuk menjalankan 1 file PHP (Promise)
|
||||
// === 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) => {
|
||||
console.log(`Running command: ${phpExecutablePath} ${phpScriptPath}`);
|
||||
exec(`${phpExecutablePath} ${phpScriptPath}`, (error, stdout, stderr) => {
|
||||
const cmd = `${phpExecutablePath} ${phpScriptPath}`;
|
||||
writeLog(`Running command: ${cmd}`);
|
||||
|
||||
exec(cmd, { maxBuffer: 10 * 1024 * 1024 }, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`Error executing PHP script ${phpScriptPath}: ${error.message}`);
|
||||
} else if (stderr) {
|
||||
console.error(`PHP script ${phpScriptPath} stderr: ${stderr}`);
|
||||
} else {
|
||||
console.log(`PHP script ${phpScriptPath} output: ${stdout}`);
|
||||
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(); // tetap resolve biar lanjut meskipun error
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Fungsi utama
|
||||
// Fungsi utama untuk load semua script (5–15 detik)
|
||||
async function runPhpScripts() {
|
||||
// Jalankan semua script secara paralel
|
||||
await Promise.all(phpScriptPaths.map(runPhpScript));
|
||||
|
||||
// Random delay 5–15 detik
|
||||
const nextDelay = Math.floor(Math.random() * (10000 - 5000 + 1)) + 5000;
|
||||
console.log(`Next run in ${nextDelay / 1000} seconds\n`);
|
||||
|
||||
// Jalankan lagi setelah delay
|
||||
writeLog(`Next run in ${nextDelay / 1000} seconds`);
|
||||
setTimeout(runPhpScripts, nextDelay);
|
||||
}
|
||||
|
||||
// Fungsi loop cepat (1 detik)
|
||||
function runLoadWAOfficial() {
|
||||
runPhpScript(loadWAOfficialPath).then(() => {
|
||||
setTimeout(runLoadWAOfficial, 1000);
|
||||
});
|
||||
// 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();
|
||||
|
||||
Reference in New Issue
Block a user