Merge branch 'master' into Spiritreader_master

# Conflicts:
#	src/pages/EditMonitor.vue
This commit is contained in:
LouisLam 2021-07-22 17:34:41 +08:00
commit 86e18ac11d
20 changed files with 363 additions and 80 deletions

View file

@ -12,6 +12,7 @@ const fs = require("fs");
const {getSettings} = require("./util-server");
const {Notification} = require("./notification")
const gracefulShutdown = require('http-graceful-shutdown');
const Database = require("./database");
const {sleep} = require("./util");
const args = require('args-parser')(process.argv);
@ -27,9 +28,28 @@ const server = http.createServer(app);
const io = new Server(server);
app.use(express.json())
/**
* Total WebSocket client connected to server currently, no actual use
* @type {number}
*/
let totalClient = 0;
/**
* Use for decode the auth object
* @type {null}
*/
let jwtSecret = null;
/**
* Main monitor list
* @type {{}}
*/
let monitorList = {};
/**
* Show Setup Page
* @type {boolean}
*/
let needSetup = false;
(async () => {
@ -163,10 +183,6 @@ let needSetup = false;
msg: e.message
});
}
});
// Auth Only API
@ -556,19 +572,21 @@ function checkLogin(socket) {
}
async function initDatabase() {
const path = './data/kuma.db';
if (! fs.existsSync(path)) {
if (! fs.existsSync(Database.path)) {
console.log("Copying Database")
fs.copyFileSync("./db/kuma.db", path);
fs.copyFileSync(Database.templatePath, Database.path);
}
console.log("Connecting to Database")
R.setup('sqlite', {
filename: path
filename: Database.path
});
console.log("Connected")
// Patch the database
await Database.patch()
// Auto map the model to a bean object
R.freeze(true)
await R.autoloadModels("./server/model");
@ -588,6 +606,7 @@ async function initDatabase() {
console.log("Load JWT secret from database.")
}
// If there is no record in user table, it is a new Uptime Kuma instance, need to setup
if ((await R.count("user")) === 0) {
console.log("No user, need setup")
needSetup = true;
@ -706,11 +725,6 @@ const startGracefulShutdown = async () => {
}
let noReject = true;
process.on('unhandledRejection', (reason, p) => {
noReject = false;
});
async function shutdownFunction(signal) {
console.log('Called signal: ' + signal);
@ -719,24 +733,8 @@ async function shutdownFunction(signal) {
let monitor = monitorList[id]
monitor.stop()
}
await sleep(2000)
console.log("Closing DB")
// Special handle, because tarn.js throw a promise reject that cannot be caught
while (true) {
noReject = true;
await R.close()
await sleep(2000)
if (noReject) {
break;
} else {
console.log("Waiting...")
}
}
console.log("OK")
await sleep(2000);
await Database.close();
}
function finalFunction() {