Export monitor list

This commit is contained in:
Louis Lam 2022-04-07 22:53:32 +08:00
parent fcb577097b
commit d5e68f8453
1 changed files with 32 additions and 27 deletions

View File

@ -1,5 +1,8 @@
console.log("Welcome to Uptime Kuma"); console.log("Welcome to Uptime Kuma");
// Alias
const server = module.exports;
// Check Node.js Version // Check Node.js Version
const nodeVersion = parseInt(process.versions.node.split(".")[0]); const nodeVersion = parseInt(process.versions.node.split(".")[0]);
const requiredVersion = 14; const requiredVersion = 14;
@ -48,6 +51,14 @@ debug("Importing 2FA Modules");
const notp = require("notp"); const notp = require("notp");
const base32 = require("thirty-two"); const base32 = require("thirty-two");
/**
* Main monitor list
* @type {{}}
*/
server.monitorList = {};
// `module.exports` (alias: `server`) should be before this line, to avoid circular dependency issue
console.log("Importing this project modules"); console.log("Importing this project modules");
debug("Importing Monitor"); debug("Importing Monitor");
const Monitor = require("./model/monitor"); const Monitor = require("./model/monitor");
@ -115,20 +126,20 @@ if (config.demoMode) {
console.log("Creating express and socket.io instance"); console.log("Creating express and socket.io instance");
const app = express(); const app = express();
let server; let httpServer;
if (sslKey && sslCert) { if (sslKey && sslCert) {
console.log("Server Type: HTTPS"); console.log("Server Type: HTTPS");
server = https.createServer({ httpServer = https.createServer({
key: fs.readFileSync(sslKey), key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert) cert: fs.readFileSync(sslCert)
}, app); }, app);
} else { } else {
console.log("Server Type: HTTP"); console.log("Server Type: HTTP");
server = http.createServer(app); httpServer = http.createServer(app);
} }
const io = new Server(server); const io = new Server(httpServer);
module.exports.io = io; module.exports.io = io;
// Must be after io instantiation // Must be after io instantiation
@ -163,12 +174,6 @@ let totalClient = 0;
*/ */
let jwtSecret = null; let jwtSecret = null;
/**
* Main monitor list
* @type {{}}
*/
let monitorList = {};
/** /**
* Show Setup Page * Show Setup Page
* @type {boolean} * @type {boolean}
@ -630,7 +635,7 @@ exports.entryPage = "dashboard";
} }
// Reset Prometheus labels // Reset Prometheus labels
monitorList[monitor.id]?.prometheus()?.remove(); server.monitorList[monitor.id]?.prometheus()?.remove();
bean.name = monitor.name; bean.name = monitor.name;
bean.type = monitor.type; bean.type = monitor.type;
@ -798,9 +803,9 @@ exports.entryPage = "dashboard";
console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`); console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`);
if (monitorID in monitorList) { if (monitorID in server.monitorList) {
monitorList[monitorID].stop(); server.monitorList[monitorID].stop();
delete monitorList[monitorID]; delete server.monitorList[monitorID];
} }
await R.exec("DELETE FROM monitor WHERE id = ? AND user_id = ? ", [ await R.exec("DELETE FROM monitor WHERE id = ? AND user_id = ? ", [
@ -1139,8 +1144,8 @@ exports.entryPage = "dashboard";
// If the import option is "overwrite" it'll clear most of the tables, except "settings" and "user" // If the import option is "overwrite" it'll clear most of the tables, except "settings" and "user"
if (importHandle == "overwrite") { if (importHandle == "overwrite") {
// Stops every monitor first, so it doesn't execute any heartbeat while importing // Stops every monitor first, so it doesn't execute any heartbeat while importing
for (let id in monitorList) { for (let id in server.monitorList) {
let monitor = monitorList[id]; let monitor = server.monitorList[id];
await monitor.stop(); await monitor.stop();
} }
await R.exec("DELETE FROM heartbeat"); await R.exec("DELETE FROM heartbeat");
@ -1414,12 +1419,12 @@ exports.entryPage = "dashboard";
console.log("Init the server"); console.log("Init the server");
server.once("error", async (err) => { httpServer.once("error", async (err) => {
console.error("Cannot listen: " + err.message); console.error("Cannot listen: " + err.message);
await shutdownFunction(); await shutdownFunction();
}); });
server.listen(port, hostname, () => { httpServer.listen(port, hostname, () => {
if (hostname) { if (hostname) {
console.log(`Listening on ${hostname}:${port}`); console.log(`Listening on ${hostname}:${port}`);
} else { } else {
@ -1559,11 +1564,11 @@ async function startMonitor(userID, monitorID) {
monitorID, monitorID,
]); ]);
if (monitor.id in monitorList) { if (monitor.id in server.monitorList) {
monitorList[monitor.id].stop(); server.monitorList[monitor.id].stop();
} }
monitorList[monitor.id] = monitor; server.monitorList[monitor.id] = monitor;
monitor.start(io); monitor.start(io);
} }
@ -1581,8 +1586,8 @@ async function pauseMonitor(userID, monitorID) {
userID, userID,
]); ]);
if (monitorID in monitorList) { if (monitorID in server.monitorList) {
monitorList[monitorID].stop(); server.monitorList[monitorID].stop();
} }
} }
@ -1593,7 +1598,7 @@ async function startMonitors() {
let list = await R.find("monitor", " active = 1 "); let list = await R.find("monitor", " active = 1 ");
for (let monitor of list) { for (let monitor of list) {
monitorList[monitor.id] = monitor; server.monitorList[monitor.id] = monitor;
} }
for (let monitor of list) { for (let monitor of list) {
@ -1608,8 +1613,8 @@ async function shutdownFunction(signal) {
console.log("Called signal: " + signal); console.log("Called signal: " + signal);
console.log("Stopping all monitors"); console.log("Stopping all monitors");
for (let id in monitorList) { for (let id in server.monitorList) {
let monitor = monitorList[id]; let monitor = server.monitorList[id];
monitor.stop(); monitor.stop();
} }
await sleep(2000); await sleep(2000);
@ -1623,7 +1628,7 @@ function finalFunction() {
console.log("Graceful shutdown successful!"); console.log("Graceful shutdown successful!");
} }
gracefulShutdown(server, { gracefulShutdown(httpServer, {
signals: "SIGINT SIGTERM", signals: "SIGINT SIGTERM",
timeout: 30000, // timeout: 30 secs timeout: 30000, // timeout: 30 secs
development: false, // not in dev mode development: false, // not in dev mode