Added better check and error handling to log file

This commit is contained in:
2023-06-29 14:50:18 +02:00
parent fb759c693c
commit 6a7a0069aa

View File

@@ -244,8 +244,8 @@ async function runAll() {
for (const vpn of vpns) {
const currentDate = new Date();
const formattedDate = `${currentDate.getDate()}-${currentDate.getMonth() + 1}-${currentDate.getFullYear()}`;
const logEntries = JSON.parse(fs.readFileSync('batch_logs.json', 'utf8')) || [];
const entry = logEntries.find(entries => entries.accountName === vpn);
const logEntries = JSON.parse(fs.readFileSync('batch_logs.json', 'utf8'));
const entry = logEntries.find(entries => entries.accountName === vpn) || false;
if (entry && entry.date === formattedDate) {
logManager(`[${host}] ${vpn} already ran today!`, 'info');
continue;
@@ -281,13 +281,23 @@ async function runBatch(vpn) {
logManager(`[${host}] Starting mcr-bot on host: ${host}`, 'info');
if (!fs.existsSync('batch_logs.json')) {
fs.writeFile('batch_logs.json', JSON.stringify([], null, 2), err => {
fs.writeFile('batch_logs.json', '[]', err => {
if (err) {
logManager(`Failed to create batch_logs.json: ${err}`, 'error');
process.exit(1);
}
});
}
else {
const fileContent = await fs.readFile('batch_logs.json', 'utf-8');
try {
JSON.parse(fileContent);
}
catch (error) {
logManager(`[${host}] Existing log file is not in proper JSON format: ${error}`, 'error');
process.exit(1);
}
}
logManager(`[${host}] Log file: batch_logs.json`, 'info');
for (let i = 0; i < args.length; i++) {