Merge branch 'main' of https://github.com/Xargana/blahaj-srv
This commit is contained in:
commit
ae217db3ca
|
@ -25,18 +25,24 @@ REMOTE_SERVERS.forEach(server => {
|
||||||
let pm2ServicesStatus = {};
|
let pm2ServicesStatus = {};
|
||||||
|
|
||||||
async function checkServers() {
|
async function checkServers() {
|
||||||
for (const server of REMOTE_SERVERS) {
|
|
||||||
const startTime = Date.now();
|
|
||||||
try {
|
try {
|
||||||
const res = await ping.promise.probe(server.host);
|
for (const server of REMOTE_SERVERS) {
|
||||||
|
try {
|
||||||
|
const res = await ping.promise.probe(server.host, {
|
||||||
|
timeout: 2, // Set a timeout of 2 seconds
|
||||||
|
});
|
||||||
serversStatus[server.name].online = res.alive;
|
serversStatus[server.name].online = res.alive;
|
||||||
serversStatus[server.name].responseTime = res.time;
|
serversStatus[server.name].responseTime = res.time;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(`Error pinging ${server.host}:`, error);
|
||||||
serversStatus[server.name].online = false;
|
serversStatus[server.name].online = false;
|
||||||
serversStatus[server.name].responseTime = null;
|
serversStatus[server.name].responseTime = null;
|
||||||
}
|
}
|
||||||
serversStatus[server.name].lastChecked = new Date().toISOString();
|
serversStatus[server.name].lastChecked = new Date().toISOString();
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error in checkServers function:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkPM2Services() {
|
async function checkPM2Services() {
|
||||||
|
@ -63,8 +69,8 @@ async function checkPM2Services() {
|
||||||
name: process.name,
|
name: process.name,
|
||||||
id: process.pm_id,
|
id: process.pm_id,
|
||||||
status: process.pm2_env.status,
|
status: process.pm2_env.status,
|
||||||
cpu: process.monit.cpu,
|
cpu: process.monit ? process.monit.cpu : null,
|
||||||
memory: process.monit.memory,
|
memory: process.monit ? process.monit.memory : null,
|
||||||
uptime: process.pm2_env.pm_uptime ?
|
uptime: process.pm2_env.pm_uptime ?
|
||||||
Date.now() - process.pm2_env.pm_uptime :
|
Date.now() - process.pm2_env.pm_uptime :
|
||||||
null,
|
null,
|
||||||
|
@ -81,18 +87,46 @@ async function checkPM2Services() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkAll() {
|
async function checkAll() {
|
||||||
|
try {
|
||||||
await checkServers();
|
await checkServers();
|
||||||
await checkPM2Services();
|
await checkPM2Services();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error in checkAll function:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(checkAll, CHECK_INTERVAL);
|
// Initial check with error handling
|
||||||
checkAll();
|
try {
|
||||||
|
checkAll();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during initial check:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set interval with error handling
|
||||||
|
setInterval(() => {
|
||||||
|
try {
|
||||||
|
checkAll();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during scheduled check:", error);
|
||||||
|
}
|
||||||
|
}, CHECK_INTERVAL);
|
||||||
|
|
||||||
|
// Route with error handling
|
||||||
router.get("/", (req, res) => {
|
router.get("/", (req, res) => {
|
||||||
|
try {
|
||||||
res.json({
|
res.json({
|
||||||
servers: serversStatus,
|
servers: serversStatus,
|
||||||
pm2Services: pm2ServicesStatus
|
pm2Services: pm2ServicesStatus
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error sending status response:", error);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add a simple health check endpoint
|
||||||
|
router.get("/health", (req, res) => {
|
||||||
|
res.status(200).send("OK");
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in a new issue