Speedup peertube startup

pull/1415/head
Chocobozzz 2018-11-19 15:21:09 +01:00
parent 361805c48b
commit 0b2f03d371
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 47 additions and 27 deletions

View File

@ -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)

View 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)
}
if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
// Try to create the extension ourself
try {
await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
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 })
} 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)
}
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)
}
}
}

View 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)

View 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 }
}