From f21cea2aec03ddab341219f8374d7ce0ce5f7a24 Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Sat, 24 Nov 2018 01:49:01 -0500 Subject: [PATCH] Added error message when someone tries to access mailtrain while it is loading. This prevents one to perform action on services that are still initializing (e.g. senders, where update of a send configuration causes config reload on the sender process, which may not be started yet and thus responds with error that send method is not defined) --- server/app-builder.js | 21 +++++++++++++++++++++ server/index.js | 39 +++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/server/app-builder.js b/server/app-builder.js index d7cacabb..10c655a4 100644 --- a/server/app-builder.js +++ b/server/app-builder.js @@ -63,6 +63,13 @@ const interoperableErrors = require('../shared/interoperable-errors'); const { getTrustedUrl } = require('./lib/urls'); const { AppType } = require('../shared/app'); + +let isReady = false; +function setReady() { + isReady = true; +} + + hbs.registerPartials(__dirname + '/views/partials'); hbs.registerPartials(__dirname + '/views/subscription/partials/'); @@ -187,6 +194,19 @@ function createApp(appType) { limit: config.www.postSize })); + + app.use((req, res, next) => { + if (isReady) { + next(); + } else { + res.status(500); + res.render('error', { + message: 'Mailtrain is starting. Try again after a few seconds.', + error: {} + }); + } + }); + if (appType === AppType.TRUSTED) { passport.setupRegularAuth(app); } else if (appType === AppType.SANDBOXED) { @@ -371,3 +391,4 @@ function createApp(appType) { } module.exports.createApp = createApp; +module.exports.setReady = setReady; diff --git a/server/index.js b/server/index.js index b6d9e77d..081b4f0e 100644 --- a/server/index.js +++ b/server/index.js @@ -92,37 +92,36 @@ dbcheck(err => { // Check if database needs upgrading before starting the server */ .then(() => - executor.spawn(() => { - testServer(() => { - verpServer(() => { - startHTTPServer(AppType.TRUSTED, 'trusted', trustedPort, () => { - startHTTPServer(AppType.SANDBOXED, 'sandbox', sandboxPort, () => { + executor.spawn(() => + testServer(() => + verpServer(() => + startHTTPServer(AppType.TRUSTED, 'trusted', trustedPort, () => + startHTTPServer(AppType.SANDBOXED, 'sandbox', sandboxPort, () => startHTTPServer(AppType.PUBLIC, 'public', publicPort, () => { privilegeHelpers.dropRootPrivileges(); tzupdate.start(); - importer.spawn(() => { - feedcheck.spawn(() => { + importer.spawn(() => + feedcheck.spawn(() => senders.spawn(() => { triggers.start(); gdprCleanup.start(); postfixBounceServer(async () => { - (async () => { - await reportProcessor.init(); - log.info('Service', 'All services started'); - })(); + await reportProcessor.init(); + log.info('Service', 'All services started'); + appBuilder.setReady(); }); - }); - }); - }); - }); - }); - }); - }); - }); - }) + }) + ) + ); + }) + ) + ) + ) + ) + ) ); });