Stop bree and cloudflared while the server shutting down

This commit is contained in:
Louis Lam 2022-04-05 19:41:29 +08:00
parent d8b32d652f
commit 48d679234a
3 changed files with 22 additions and 7 deletions

View file

@ -1,7 +1,7 @@
const path = require("path"); const path = require("path");
const Bree = require("bree"); const Bree = require("bree");
const { SHARE_ENV } = require("worker_threads"); const { SHARE_ENV } = require("worker_threads");
let bree;
const jobs = [ const jobs = [
{ {
name: "clear-old-data", name: "clear-old-data",
@ -10,7 +10,7 @@ const jobs = [
]; ];
const initBackgroundJobs = function (args) { const initBackgroundJobs = function (args) {
const bree = new Bree({ bree = new Bree({
root: path.resolve("server", "jobs"), root: path.resolve("server", "jobs"),
jobs, jobs,
worker: { worker: {
@ -26,6 +26,13 @@ const initBackgroundJobs = function (args) {
return bree; return bree;
}; };
module.exports = { const stopBackgroundJobs = function () {
initBackgroundJobs if (bree) {
bree.stop();
}
};
module.exports = {
initBackgroundJobs,
stopBackgroundJobs
}; };

View file

@ -65,7 +65,7 @@ debug("Importing Database");
const Database = require("./database"); const Database = require("./database");
debug("Importing Background Jobs"); debug("Importing Background Jobs");
const { initBackgroundJobs } = require("./jobs"); const { initBackgroundJobs, stopBackgroundJobs } = require("./jobs");
const { loginRateLimiter, twoFaRateLimiter } = require("./rate-limiter"); const { loginRateLimiter, twoFaRateLimiter } = require("./rate-limiter");
const { basicAuth } = require("./auth"); const { basicAuth } = require("./auth");
@ -137,7 +137,7 @@ const { statusPageSocketHandler } = require("./socket-handlers/status-page-socke
const databaseSocketHandler = require("./socket-handlers/database-socket-handler"); const databaseSocketHandler = require("./socket-handlers/database-socket-handler");
const TwoFA = require("./2fa"); const TwoFA = require("./2fa");
const StatusPage = require("./model/status_page"); const StatusPage = require("./model/status_page");
const { cloudflaredSocketHandler, autoStart: cloudflaredAutoStart } = require("./socket-handlers/cloudflared-socket-handler"); const { cloudflaredSocketHandler, autoStart: cloudflaredAutoStart, stop: cloudflaredStop } = require("./socket-handlers/cloudflared-socket-handler");
app.use(express.json()); app.use(express.json());
@ -1459,7 +1459,7 @@ exports.entryPage = "dashboard";
server.once("error", async (err) => { server.once("error", async (err) => {
console.error("Cannot listen: " + err.message); console.error("Cannot listen: " + err.message);
await Database.close(); await shutdownFunction();
}); });
server.listen(port, hostname, () => { server.listen(port, hostname, () => {
@ -1670,6 +1670,9 @@ async function shutdownFunction(signal) {
} }
await sleep(2000); await sleep(2000);
await Database.close(); await Database.close();
stopBackgroundJobs();
await cloudflaredStop();
} }
function finalFunction() { function finalFunction() {

View file

@ -83,3 +83,8 @@ module.exports.autoStart = async (token) => {
cloudflared.start(); cloudflared.start();
} }
}; };
module.exports.stop = async () => {
console.log("Stop cloudflared");
cloudflared.stop();
};