Split pre and post listen setup tasks. Only shutdown plugins once

This commit is contained in:
Cohee 2024-04-27 21:41:32 +03:00
parent ea050b98ef
commit 1bcdc2652c
1 changed files with 15 additions and 3 deletions

View File

@ -529,7 +529,10 @@ const autorunUrl = new URL(
(':' + server_port), (':' + server_port),
); );
const setupTasks = async function () { /**
* Tasks that need to be run before the server starts listening.
*/
const preSetupTasks = async function () {
const version = await getVersion(); const version = await getVersion();
// Print formatted header // Print formatted header
@ -553,7 +556,10 @@ const setupTasks = async function () {
const cleanupPlugins = await loadPlugins(); const cleanupPlugins = await loadPlugins();
const consoleTitle = process.title; const consoleTitle = process.title;
let isExiting = false;
const exitProcess = async () => { const exitProcess = async () => {
if (isExiting) return;
isExiting = true;
statsEndpoint.onExit(); statsEndpoint.onExit();
if (typeof cleanupPlugins === 'function') { if (typeof cleanupPlugins === 'function') {
await cleanupPlugins(); await cleanupPlugins();
@ -569,7 +575,12 @@ const setupTasks = async function () {
console.error('Uncaught exception:', err); console.error('Uncaught exception:', err);
exitProcess(); exitProcess();
}); });
};
/**
* Tasks that need to be run after the server starts listening.
*/
const postSetupTasks = async function () {
console.log('Launching...'); console.log('Launching...');
if (autorun) open(autorunUrl.toString()); if (autorun) open(autorunUrl.toString());
@ -637,6 +648,7 @@ function setWindowTitle(title) {
userModule.initUserStorage(dataRoot) userModule.initUserStorage(dataRoot)
.then(userModule.ensurePublicDirectoriesExist) .then(userModule.ensurePublicDirectoriesExist)
.then(userModule.migrateUserData) .then(userModule.migrateUserData)
.then(preSetupTasks)
.finally(() => { .finally(() => {
if (cliArguments.ssl) { if (cliArguments.ssl) {
https.createServer( https.createServer(
@ -647,13 +659,13 @@ userModule.initUserStorage(dataRoot)
.listen( .listen(
Number(tavernUrl.port) || 443, Number(tavernUrl.port) || 443,
tavernUrl.hostname, tavernUrl.hostname,
setupTasks, postSetupTasks,
); );
} else { } else {
http.createServer(app).listen( http.createServer(app).listen(
Number(tavernUrl.port) || 80, Number(tavernUrl.port) || 80,
tavernUrl.hostname, tavernUrl.hostname,
setupTasks, postSetupTasks,
); );
} }
}); });