mirror of
https://github.com/ibratabian17/OpenParty.git
synced 2026-01-15 14:22:54 -03:00
Refactor process management in run.js to improve logging and restart functionality
This commit is contained in:
@@ -1,57 +1,112 @@
|
|||||||
const { spawnSync } = require('child_process');
|
const { spawn } = require('child_process');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
let outputLogs = [];
|
class ProcessManager {
|
||||||
|
constructor() {
|
||||||
function start() {
|
this.outputLogs = [];
|
||||||
const { stdout, stderr, status } = spawnSync('node', ['jduparty.js']);
|
this.requestLogs = [];
|
||||||
|
this.process = null;
|
||||||
if (stdout) {
|
this.restartCount = 0;
|
||||||
const log = {
|
this.maxRestarts = 10;
|
||||||
method: 'LOG',
|
this.restartDelay = 1000;
|
||||||
url: stdout.toString().trim(),
|
this.logPath = path.join(__dirname, '../database/tmp/logs.txt');
|
||||||
timestamp: new Date().toISOString()
|
|
||||||
};
|
// Ensure log directory exists
|
||||||
outputLogs.push(log);
|
const logDir = path.dirname(this.logPath);
|
||||||
|
if (!fs.existsSync(logDir)) {
|
||||||
fs.writeFileSync('database/tmp/logs.txt', JSON.stringify(outputLogs));
|
fs.mkdirSync(logDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (stderr) {
|
|
||||||
const log = {
|
start() {
|
||||||
method: 'LOG ERROR',
|
this.process = spawn('node', ['server.js'], {
|
||||||
url: stderr.toString().trim(),
|
stdio: 'pipe',
|
||||||
timestamp: new Date().toISOString()
|
detached: false
|
||||||
};
|
});
|
||||||
outputLogs.push(log);
|
|
||||||
|
this.process.stdout.on('data', (data) => {
|
||||||
fs.writeFileSync('database/tmp/logs.txt', JSON.stringify(outputLogs));
|
this.logOutput('INFO', data.toString().trim());
|
||||||
}
|
});
|
||||||
|
|
||||||
console.log(`[PARTY] child process exited with code ${status}`);
|
this.process.stderr.on('data', (data) => {
|
||||||
if (status === 42) { // Replace 42 with your desired exit code
|
this.logOutput('ERROR', data.toString().trim());
|
||||||
start(); // Restart the process
|
});
|
||||||
}
|
|
||||||
}
|
this.process.on('exit', (code) => {
|
||||||
|
console.log(`[PARTY] Process exited with code ${code}`);
|
||||||
function generateLog(req, res, next) {
|
|
||||||
counted++;
|
if (code === 42) {
|
||||||
if (!req.url.startsWith('/party/panel/')) {
|
if (this.restartCount < this.maxRestarts) {
|
||||||
const log = {
|
console.log(`[PARTY] Restarting process in ${this.restartDelay}ms...`);
|
||||||
timestamp: new Date().toISOString(),
|
setTimeout(() => this.start(), this.restartDelay);
|
||||||
message: `[PARTY] ${req.method} ${req.url}`
|
this.restartCount++;
|
||||||
};
|
} else {
|
||||||
requestLogs.push(log);
|
console.error('[PARTY] Max restart attempts reached');
|
||||||
if (requestLogs.length > 50) {
|
}
|
||||||
requestLogs.shift();
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
logOutput(level, message) {
|
||||||
|
// Write directly to console
|
||||||
|
console.log(`${message}`);
|
||||||
|
|
||||||
|
const log = {
|
||||||
|
level,
|
||||||
|
message,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
|
||||||
|
this.outputLogs.push(log);
|
||||||
|
if (this.outputLogs.length > 100) {
|
||||||
|
this.outputLogs.shift();
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.appendFileSync(this.logPath, JSON.stringify(log) + '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
generateLog(req, res, next) {
|
||||||
|
if (!req.url.startsWith('/party/panel/')) {
|
||||||
|
const log = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
method: req.method,
|
||||||
|
url: req.url,
|
||||||
|
ip: req.ip
|
||||||
|
};
|
||||||
|
|
||||||
|
this.requestLogs.push(log);
|
||||||
|
if (this.requestLogs.length > 50) {
|
||||||
|
this.requestLogs.shift();
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.appendFileSync(this.logPath, JSON.stringify(log) + '\n');
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
stop() {
|
||||||
|
if (this.process) {
|
||||||
|
this.process.kill();
|
||||||
|
this.process = null;
|
||||||
}
|
}
|
||||||
fs.appendFileSync('database/tmp/logs.txt', `${JSON.stringify(log)}\n`);
|
|
||||||
}
|
}
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start();
|
const manager = new ProcessManager();
|
||||||
|
manager.start();
|
||||||
|
|
||||||
|
// Handle process signals
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
process.exit();
|
console.log('[PARTY] Gracefully shutting down...');
|
||||||
|
manager.stop();
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.on('SIGTERM', () => {
|
||||||
|
console.log('[PARTY] Terminating...');
|
||||||
|
manager.stop();
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = manager;
|
||||||
|
|||||||
Reference in New Issue
Block a user