From 0b2f03d3712f438f67eccf86b67acd047284f9b4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Nov 2018 15:21:09 +0100 Subject: Speedup peertube startup --- server/initializers/database.ts | 32 +++++++++++++++++--------------- server/initializers/installer.ts | 21 +++++++++++++++------ server/lib/user.ts | 15 +++++++++++---- 3 files changed, 43 insertions(+), 25 deletions(-) (limited to 'server') diff --git a/server/initializers/database.ts b/server/initializers/database.ts index dd5b9bf67..40cd659ab 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -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) } } } diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index c952ad46c..b9a9da183 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -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) diff --git a/server/lib/user.ts b/server/lib/user.ts index db29469eb..acb883e23 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -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 } } -- cgit v1.2.3