Speedup peertube startup
authorChocobozzz <me@florianbigard.com>
Mon, 19 Nov 2018 14:21:09 +0000 (15:21 +0100)
committerChocobozzz <me@florianbigard.com>
Mon, 19 Nov 2018 14:21:09 +0000 (15:21 +0100)
server.ts
server/initializers/database.ts
server/initializers/installer.ts
server/lib/user.ts

index f3514cf9c1d8d9b487e1bcec00a4deb92be42723..3025a6fd7fbf0c5c28f4957bf026d39a7e735e8d 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -204,9 +204,11 @@ async function startApplication () {
 
   // Email initialization
   Emailer.Instance.init()
-  await Emailer.Instance.checkConnectionOrDie()
 
-  await JobQueue.Instance.init()
+  await Promise.all([
+    Emailer.Instance.checkConnectionOrDie(),
+    JobQueue.Instance.init()
+  ])
 
   // Caches initializations
   VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, CACHE.PREVIEWS.MAX_AGE)
index dd5b9bf67db8930db50492668a5b22b113c6128a..40cd659ab8bdf7d0708c8bb9904413297c1631aa 100644 (file)
@@ -119,25 +119,27 @@ export {
 // ---------------------------------------------------------------------------
 
 async function checkPostgresExtensions () {
-  const extensions = [
-    'pg_trgm',
-    'unaccent'
+  const promises = [
+    checkPostgresExtension('pg_trgm'),
+    checkPostgresExtension('unaccent')
   ]
 
-  for (const extension of extensions) {
-    const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
-    const [ res ] = await sequelizeTypescript.query(query, { raw: true })
+  return Promise.all(promises)
+}
+
+async function checkPostgresExtension (extension: string) {
+  const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
+  const [ res ] = await sequelizeTypescript.query(query, { raw: true })
 
-    if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
-      // Try to create the extension ourself
-      try {
-        await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
+  if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
+    // Try to create the extension ourself
+    try {
+      await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
 
-      } catch {
-        const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
-          `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
-        throw new Error(errorMessage)
-      }
+    } catch {
+      const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
+        `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
+      throw new Error(errorMessage)
     }
   }
 }
index c952ad46c42228c825bec7116d375411e1935525..b9a9da18307e5301e3c66d552052ab4f965343fc 100644 (file)
@@ -12,12 +12,21 @@ import { remove, ensureDir } from 'fs-extra'
 
 async function installApplication () {
   try {
-    await sequelizeTypescript.sync()
-    await removeCacheDirectories()
-    await createDirectoriesIfNotExist()
-    await createApplicationIfNotExist()
-    await createOAuthClientIfNotExist()
-    await createOAuthAdminIfNotExist()
+    await Promise.all([
+      // Database related
+      sequelizeTypescript.sync()
+        .then(() => {
+          return Promise.all([
+            createApplicationIfNotExist(),
+            createOAuthClientIfNotExist(),
+            createOAuthAdminIfNotExist()
+          ])
+        }),
+
+      // Directories
+      removeCacheDirectories()
+        .then(() => createDirectoriesIfNotExist())
+    ])
   } catch (err) {
     logger.error('Cannot install application.', { err })
     process.exit(-1)
index db29469eb39942df22702aa1af275751f79537c0..acb883e23d202668403a48dc2af039515779aa47 100644 (file)
@@ -17,8 +17,10 @@ async function createUserAccountAndChannel (userToCreate: UserModel, validateUse
       validate: validateUser
     }
 
-    const userCreated = await userToCreate.save(userOptions)
-    const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
+    const [ userCreated, accountCreated ] = await Promise.all([
+      userToCreate.save(userOptions),
+      createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
+    ])
     userCreated.Account = accountCreated
 
     let channelName = userCreated.username + '_channel'
@@ -37,8 +39,13 @@ async function createUserAccountAndChannel (userToCreate: UserModel, validateUse
     return { user: userCreated, account: accountCreated, videoChannel }
   })
 
-  account.Actor = await setAsyncActorKeys(account.Actor)
-  videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
+  const [ accountKeys, channelKeys ] = await Promise.all([
+    setAsyncActorKeys(account.Actor),
+    setAsyncActorKeys(videoChannel.Actor)
+  ])
+
+  account.Actor = accountKeys
+  videoChannel.Actor = channelKeys
 
   return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
 }