add fs promises
This commit is contained in:
@@ -2,6 +2,7 @@ const Discord = require('discord.js');
|
|||||||
const Docker = require('dockerode');
|
const Docker = require('dockerode');
|
||||||
const { CronJob } = require('cron');
|
const { CronJob } = require('cron');
|
||||||
const { execSync } = require('child_process');
|
const { execSync } = require('child_process');
|
||||||
|
const fs = require('fs').promises; // Added for file operations
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
const client = new Discord.Client({
|
const client = new Discord.Client({
|
||||||
@@ -35,6 +36,7 @@ const SUPER_UPGRADED_SWAP = parseInt(process.env.SUPER_UPGRADED_SWAP) * 1024 * 1
|
|||||||
|
|
||||||
const RESET_UNKNOWN_TO_DEFAULT = process.env.RESET_UNKNOWN_TO_DEFAULT === 'true';
|
const RESET_UNKNOWN_TO_DEFAULT = process.env.RESET_UNKNOWN_TO_DEFAULT === 'true';
|
||||||
const EXEC_TIMEOUT = parseInt(process.env.EXEC_TIMEOUT);
|
const EXEC_TIMEOUT = parseInt(process.env.EXEC_TIMEOUT);
|
||||||
|
const CACHE_FILE = '/var/www/html/current_upgraded.json'; // Cache file path
|
||||||
|
|
||||||
async function execWithTimeout(container, cmd, timeout = EXEC_TIMEOUT) {
|
async function execWithTimeout(container, cmd, timeout = EXEC_TIMEOUT) {
|
||||||
try {
|
try {
|
||||||
@@ -126,8 +128,19 @@ async function updateContainerConfig(container, name, userId, memoryLimit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateCache(upgradedContainers) {
|
||||||
|
try {
|
||||||
|
const data = JSON.stringify(upgradedContainers, null, 2);
|
||||||
|
await fs.writeFile(CACHE_FILE, data);
|
||||||
|
console.log(` ✅ Cache updated at ${CACHE_FILE} with ${upgradedContainers.length} upgraded containers`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(` ❌ Error writing to cache file ${CACHE_FILE}: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function checkContainers() {
|
async function checkContainers() {
|
||||||
console.log('\n=== Starting Container Check ===\n');
|
console.log('\n=== Starting Container Check ===\n');
|
||||||
|
const upgradedContainers = []; // Track upgraded containers for cache
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// List running containers
|
// List running containers
|
||||||
@@ -168,6 +181,15 @@ async function checkContainers() {
|
|||||||
currentMem === SUPER_UPGRADED_MEMORY &&
|
currentMem === SUPER_UPGRADED_MEMORY &&
|
||||||
currentSwap === SUPER_UPGRADED_SWAP;
|
currentSwap === SUPER_UPGRADED_SWAP;
|
||||||
|
|
||||||
|
// Track upgraded containers
|
||||||
|
if (isUpgraded || isSuperUpgraded) {
|
||||||
|
upgradedContainers.push({
|
||||||
|
containerName: name,
|
||||||
|
userId: userId,
|
||||||
|
upgradeType: isSuperUpgraded ? 'super' : 'standard'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Handle unknown limits
|
// Handle unknown limits
|
||||||
if (!isDefault && !isUpgraded && !isSuperUpgraded) {
|
if (!isDefault && !isUpgraded && !isSuperUpgraded) {
|
||||||
console.log(` ⚠️ Warning: Unknown limits detected!`);
|
console.log(` ⚠️ Warning: Unknown limits detected!`);
|
||||||
@@ -218,6 +240,7 @@ async function checkContainers() {
|
|||||||
});
|
});
|
||||||
await updateContainerConfig(container, name, userId, SUPER_UPGRADED_MEMORY);
|
await updateContainerConfig(container, name, userId, SUPER_UPGRADED_MEMORY);
|
||||||
console.log(` ✅ Super Upgraded to: CPUs=${SUPER_UPGRADED_CPUS}, Memory=${SUPER_UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${SUPER_UPGRADED_SWAP / 1024 / 1024} MiB`);
|
console.log(` ✅ Super Upgraded to: CPUs=${SUPER_UPGRADED_CPUS}, Memory=${SUPER_UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${SUPER_UPGRADED_SWAP / 1024 / 1024} MiB`);
|
||||||
|
upgradedContainers.push({ containerName: name, userId: userId, upgradeType: 'super' });
|
||||||
} else if (!hasSuperUpgradeRole && hasStandardOrManualRole && !isUpgraded) {
|
} else if (!hasSuperUpgradeRole && hasStandardOrManualRole && !isUpgraded) {
|
||||||
// Apply standard upgrade
|
// Apply standard upgrade
|
||||||
console.log(` 🔼 Upgrading container...`);
|
console.log(` 🔼 Upgrading container...`);
|
||||||
@@ -228,6 +251,7 @@ async function checkContainers() {
|
|||||||
});
|
});
|
||||||
await updateContainerConfig(container, name, userId, UPGRADED_MEMORY);
|
await updateContainerConfig(container, name, userId, UPGRADED_MEMORY);
|
||||||
console.log(` ✅ Upgraded to: CPUs=${UPGRADED_CPUS}, Memory=${UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${UPGRADED_SWAP / 1024 / 1024} MiB`);
|
console.log(` ✅ Upgraded to: CPUs=${UPGRADED_CPUS}, Memory=${UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${UPGRADED_SWAP / 1024 / 1024} MiB`);
|
||||||
|
upgradedContainers.push({ containerName: name, userId: userId, upgradeType: 'standard' });
|
||||||
} else if (!hasSuperUpgradeRole && !hasStandardOrManualRole && (isUpgraded || isSuperUpgraded)) {
|
} else if (!hasSuperUpgradeRole && !hasStandardOrManualRole && (isUpgraded || isSuperUpgraded)) {
|
||||||
// Downgrade
|
// Downgrade
|
||||||
console.log(` 🔽 Downgrading container...`);
|
console.log(` 🔽 Downgrading container...`);
|
||||||
@@ -238,12 +262,18 @@ async function checkContainers() {
|
|||||||
});
|
});
|
||||||
await updateContainerConfig(container, name, userId, DEFAULT_MEMORY);
|
await updateContainerConfig(container, name, userId, DEFAULT_MEMORY);
|
||||||
console.log(` ✅ Downgraded to: CPUs=${DEFAULT_CPUS}, Memory=${DEFAULT_MEMORY / 1024 / 1024} MiB, Swap=${DEFAULT_SWAP / 1024 / 1024} MiB`);
|
console.log(` ✅ Downgraded to: CPUs=${DEFAULT_CPUS}, Memory=${DEFAULT_MEMORY / 1024 / 1024} MiB, Swap=${DEFAULT_SWAP / 1024 / 1024} MiB`);
|
||||||
|
// Remove from upgradedContainers if present
|
||||||
|
const index = upgradedContainers.findIndex(c => c.containerName === name);
|
||||||
|
if (index !== -1) upgradedContainers.splice(index, 1);
|
||||||
} else {
|
} else {
|
||||||
console.log(` ✅ No action needed. Container settings match role status.`);
|
console.log(` ✅ No action needed. Container settings match role status.`);
|
||||||
}
|
}
|
||||||
console.log('----------------------------------------');
|
console.log('----------------------------------------');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update cache file with upgraded containers
|
||||||
|
await updateCache(upgradedContainers);
|
||||||
console.log('\n=== Container Check Completed ===\n');
|
console.log('\n=== Container Check Completed ===\n');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`\n❌ Error in container check: ${err.message}\n`);
|
console.error(`\n❌ Error in container check: ${err.message}\n`);
|
||||||
|
Reference in New Issue
Block a user