Don't start application until all components were initialized
authorChocobozzz <me@florianbigard.com>
Wed, 4 Apr 2018 09:04:14 +0000 (11:04 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 4 Apr 2018 09:04:14 +0000 (11:04 +0200)
server.ts
server/lib/emailer.ts

index b307e67a1df71ee2924f2f492bd40db69c85770e..97941c95822b26fd5377f598fe486ecafe8b966a 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -58,7 +58,11 @@ import { initDatabaseModels } from './server/initializers/database'
 import { migrate } from './server/initializers/migrator'
 migrate()
   .then(() => initDatabaseModels(false))
-  .then(() => onDatabaseInitDone())
+  .then(() => startApplication())
+  .catch(err => {
+    logger.error('Cannot start application.', { err })
+    process.exit(-1)
+  })
 
 // ----------- PeerTube modules -----------
 import { installApplication } from './server/initializers'
@@ -179,30 +183,29 @@ app.use(function (err, req, res, next) {
 
 // ----------- Run -----------
 
-function onDatabaseInitDone () {
+async function startApplication () {
   const port = CONFIG.LISTEN.PORT
 
-  installApplication()
-    .then(() => {
-      // ----------- Make the server listening -----------
-      server.listen(port, () => {
-        // Emailer initialization and then job queue initialization
-        Emailer.Instance.init()
-        Emailer.Instance.checkConnectionOrDie()
-          .then(() => JobQueue.Instance.init())
-
-        // Caches initializations
-        VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
-
-        // Enable Schedulers
-        BadActorFollowScheduler.Instance.enable()
-        RemoveOldJobsScheduler.Instance.enable()
-
-        // Redis initialization
-        Redis.Instance.init()
-
-        logger.info('Server listening on port %d', port)
-        logger.info('Web server: %s', CONFIG.WEBSERVER.URL)
-      })
-    })
+  await installApplication()
+
+  // Email initialization
+  Emailer.Instance.init()
+  await Emailer.Instance.checkConnectionOrDie()
+
+  await JobQueue.Instance.init()
+
+  // Caches initializations
+  VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
+
+  // Enable Schedulers
+  BadActorFollowScheduler.Instance.enable()
+  RemoveOldJobsScheduler.Instance.enable()
+
+  // Redis initialization
+  Redis.Instance.init()
+
+  // Make server listening
+  server.listen(port)
+  logger.info('Server listening on port %d', port)
+  logger.info('Web server: %s', CONFIG.WEBSERVER.URL)
 }
index 85cc725fa9991b2e58c54a98362f91f65ffa5f7c..d17749b9ab430d2ed03ac1e0de01c85eb690a40b 100644 (file)
@@ -60,6 +60,8 @@ class Emailer {
   async checkConnectionOrDie () {
     if (!this.transporter) return
 
+    logger.info('Testing SMTP server...')
+
     try {
       const success = await this.transporter.verify()
       if (success !== true) this.dieOnConnectionFailure()