From f5028693a896a3076dd286ac0030e3d8f78f5ebf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 25 Oct 2017 16:03:33 +0200 Subject: Use async/await in lib and initializers --- server/initializers/checker.ts | 46 ++++++------- server/initializers/database.ts | 90 ++++++++++++------------- server/initializers/index.ts | 2 +- server/initializers/installer.ts | 141 +++++++++++++++++++-------------------- server/initializers/migrator.ts | 107 ++++++++++++----------------- 5 files changed, 178 insertions(+), 208 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts index eb9e9e280..9eaef1695 100644 --- a/server/initializers/checker.ts +++ b/server/initializers/checker.ts @@ -37,39 +37,37 @@ function checkMissedConfig () { // Check the available codecs // We get CONFIG by param to not import it in this file (import orders) -function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) { +async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) { const Ffmpeg = require('fluent-ffmpeg') const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs) - getAvailableCodecsPromise() - .then(codecs => { - if (CONFIG.TRANSCODING.ENABLED === false) return undefined - - const canEncode = [ 'libx264' ] - canEncode.forEach(codec => { - if (codecs[codec] === undefined) { - throw new Error('Unknown codec ' + codec + ' in FFmpeg.') - } - - if (codecs[codec].canEncode !== true) { - throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg') - } - }) - }) + const codecs = await getAvailableCodecsPromise() + if (CONFIG.TRANSCODING.ENABLED === false) return undefined + + const canEncode = [ 'libx264' ] + for (const codec of canEncode) { + if (codecs[codec] === undefined) { + throw new Error('Unknown codec ' + codec + ' in FFmpeg.') + } + + if (codecs[codec].canEncode !== true) { + throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg') + } + } } // We get db by param to not import it in this file (import orders) -function clientsExist (OAuthClient: OAuthClientModel) { - return OAuthClient.countTotal().then(totalClients => { - return totalClients !== 0 - }) +async function clientsExist (OAuthClient: OAuthClientModel) { + const totalClients = await OAuthClient.countTotal() + + return totalClients !== 0 } // We get db by param to not import it in this file (import orders) -function usersExist (User: UserModel) { - return User.countTotal().then(totalUsers => { - return totalUsers !== 0 - }) +async function usersExist (User: UserModel) { + const totalUsers = await User.countTotal() + + return totalUsers !== 0 } // --------------------------------------------------------------------------- diff --git a/server/initializers/database.ts b/server/initializers/database.ts index ea2b68f59..ade72b62f 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -2,7 +2,7 @@ import { join } from 'path' import { flattenDepth } from 'lodash' require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string import * as Sequelize from 'sequelize' -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' import { CONFIG } from './constants' // Do not use barrel, we need to load database first @@ -77,26 +77,26 @@ const sequelize = new Sequelize(dbname, username, password, { database.sequelize = sequelize -database.init = (silent: boolean) => { +database.init = async (silent: boolean) => { const modelDirectory = join(__dirname, '..', 'models') - return getModelFiles(modelDirectory).then(filePaths => { - filePaths.forEach(filePath => { - const model = sequelize.import(filePath) + const filePaths = await getModelFiles(modelDirectory) - database[model['name']] = model - }) + for (const filePath of filePaths) { + const model = sequelize.import(filePath) - Object.keys(database).forEach(modelName => { - if ('associate' in database[modelName]) { - database[modelName].associate(database) - } - }) + database[model['name']] = model + } - if (!silent) logger.info('Database %s is ready.', dbname) + for (const modelName of Object.keys(database)) { + if ('associate' in database[modelName]) { + database[modelName].associate(database) + } + } - return undefined - }) + if (!silent) logger.info('Database %s is ready.', dbname) + + return undefined } // --------------------------------------------------------------------------- @@ -107,31 +107,29 @@ export { // --------------------------------------------------------------------------- -function getModelFiles (modelDirectory: string) { - return readdirPromise(modelDirectory) - .then(files => { - const directories: string[] = files.filter(directory => { - // Find directories - if ( - directory.endsWith('.js.map') || - directory === 'index.js' || directory === 'index.ts' || - directory === 'utils.js' || directory === 'utils.ts' - ) return false - - return true - }) +async function getModelFiles (modelDirectory: string) { + const files = await readdirPromise(modelDirectory) + const directories = files.filter(directory => { + // Find directories + if ( + directory.endsWith('.js.map') || + directory === 'index.js' || directory === 'index.ts' || + directory === 'utils.js' || directory === 'utils.ts' + ) return false + + return true + }) - return directories - }) - .then(directories => { - const tasks = [] + const tasks: Bluebird[] = [] - // For each directory we read it and append model in the modelFilePaths array - directories.forEach(directory => { - const modelDirectoryPath = join(modelDirectory, directory) + // For each directory we read it and append model in the modelFilePaths array + for (const directory of directories) { + const modelDirectoryPath = join(modelDirectory, directory) - const promise = readdirPromise(modelDirectoryPath).then(files => { - const filteredFiles = files.filter(file => { + const promise = readdirPromise(modelDirectoryPath) + .then(files => { + const filteredFiles = files + .filter(file => { if ( file === 'index.js' || file === 'index.ts' || file === 'utils.js' || file === 'utils.ts' || @@ -140,17 +138,15 @@ function getModelFiles (modelDirectory: string) { ) return false return true - }).map(file => join(modelDirectoryPath, file)) - - return filteredFiles - }) + }) + .map(file => join(modelDirectoryPath, file)) - tasks.push(promise) + return filteredFiles }) - return Promise.all(tasks) - }) - .then((filteredFiles: string[][]) => { - return flattenDepth(filteredFiles, 1) - }) + tasks.push(promise) + } + + const filteredFilesArray: string[][] = await Promise.all(tasks) + return flattenDepth(filteredFilesArray, 1) } diff --git a/server/initializers/index.ts b/server/initializers/index.ts index b8400ff84..332702774 100644 --- a/server/initializers/index.ts +++ b/server/initializers/index.ts @@ -1,4 +1,4 @@ -// Constants first, databse in second! +// Constants first, database in second! export * from './constants' export * from './database' export * from './checker' diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index b997de07f..4c04290fc 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -1,5 +1,5 @@ import * as passwordGenerator from 'password-generator' -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' import { database as db } from './database' import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' @@ -7,13 +7,13 @@ import { clientsExist, usersExist } from './checker' import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' import { createUserAuthorAndChannel } from '../lib' -function installApplication () { - return db.sequelize.sync() - .then(() => removeCacheDirectories()) - .then(() => createDirectoriesIfNotExist()) - .then(() => createCertsIfNotExist()) - .then(() => createOAuthClientIfNotExist()) - .then(() => createOAuthAdminIfNotExist()) +async function installApplication () { + await db.sequelize.sync() + await removeCacheDirectories() + await createDirectoriesIfNotExist() + await createCertsIfNotExist() + await createOAuthClientIfNotExist() + await createOAuthAdminIfNotExist() } // --------------------------------------------------------------------------- @@ -27,13 +27,13 @@ export { function removeCacheDirectories () { const cacheDirectories = CACHE.DIRECTORIES - const tasks = [] + const tasks: Bluebird[] = [] // Cache directories - Object.keys(cacheDirectories).forEach(key => { + for (const key of Object.keys(cacheDirectories)) { const dir = cacheDirectories[key] tasks.push(rimrafPromise(dir)) - }) + } return Promise.all(tasks) } @@ -43,88 +43,83 @@ function createDirectoriesIfNotExist () { const cacheDirectories = CACHE.DIRECTORIES const tasks = [] - Object.keys(storage).forEach(key => { + for (const key of Object.keys(storage)) { const dir = storage[key] tasks.push(mkdirpPromise(dir)) - }) + } // Cache directories - Object.keys(cacheDirectories).forEach(key => { + for (const key of Object.keys(cacheDirectories)) { const dir = cacheDirectories[key] tasks.push(mkdirpPromise(dir)) - }) + } return Promise.all(tasks) } -function createOAuthClientIfNotExist () { - return clientsExist(db.OAuthClient).then(exist => { - // Nothing to do, clients already exist - if (exist === true) return undefined - - logger.info('Creating a default OAuth Client.') - - const id = passwordGenerator(32, false, /[a-z0-9]/) - const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) - const client = db.OAuthClient.build({ - clientId: id, - clientSecret: secret, - grants: [ 'password', 'refresh_token' ], - redirectUris: null - }) +async function createOAuthClientIfNotExist () { + const exist = await clientsExist(db.OAuthClient) + // Nothing to do, clients already exist + if (exist === true) return undefined - return client.save().then(createdClient => { - logger.info('Client id: ' + createdClient.clientId) - logger.info('Client secret: ' + createdClient.clientSecret) + logger.info('Creating a default OAuth Client.') - return undefined - }) + const id = passwordGenerator(32, false, /[a-z0-9]/) + const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) + const client = db.OAuthClient.build({ + clientId: id, + clientSecret: secret, + grants: [ 'password', 'refresh_token' ], + redirectUris: null }) -} -function createOAuthAdminIfNotExist () { - return usersExist(db.User).then(exist => { - // Nothing to do, users already exist - if (exist === true) return undefined + const createdClient = await client.save() + logger.info('Client id: ' + createdClient.clientId) + logger.info('Client secret: ' + createdClient.clientSecret) - logger.info('Creating the administrator.') + return undefined +} - const username = 'root' - const role = USER_ROLES.ADMIN - const email = CONFIG.ADMIN.EMAIL - let validatePassword = true - let password = '' +async function createOAuthAdminIfNotExist () { + const exist = await usersExist(db.User) + // Nothing to do, users already exist + if (exist === true) return undefined - // Do not generate a random password for tests - if (process.env.NODE_ENV === 'test') { - password = 'test' + logger.info('Creating the administrator.') - if (process.env.NODE_APP_INSTANCE) { - password += process.env.NODE_APP_INSTANCE - } + const username = 'root' + const role = USER_ROLES.ADMIN + const email = CONFIG.ADMIN.EMAIL + let validatePassword = true + let password = '' - // Our password is weak so do not validate it - validatePassword = false - } else { - password = passwordGenerator(8, true) - } + // Do not generate a random password for tests + if (process.env.NODE_ENV === 'test') { + password = 'test' - const userData = { - username, - email, - password, - role, - videoQuota: -1 + if (process.env.NODE_APP_INSTANCE) { + password += process.env.NODE_APP_INSTANCE } - const user = db.User.build(userData) - return createUserAuthorAndChannel(user, validatePassword) - .then(({ user }) => { - logger.info('Username: ' + username) - logger.info('User password: ' + password) - - logger.info('Creating Application table.') - return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) - }) - }) + // Our password is weak so do not validate it + validatePassword = false + } else { + password = passwordGenerator(8, true) + } + + const userData = { + username, + email, + password, + role, + videoQuota: -1 + } + const user = db.User.build(userData) + + await createUserAuthorAndChannel(user, validatePassword) + logger.info('Username: ' + username) + logger.info('User password: ' + password) + + logger.info('Creating Application table.') + await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) } diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts index 7b535aea9..4fbe1cf5b 100644 --- a/server/initializers/migrator.ts +++ b/server/initializers/migrator.ts @@ -1,52 +1,35 @@ import * as path from 'path' -import * as Promise from 'bluebird' import { database as db } from './database' import { LAST_MIGRATION_VERSION } from './constants' import { logger, readdirPromise } from '../helpers' -function migrate () { - const p = db.sequelize.getQueryInterface().showAllTables() - .then(tables => { - // No tables, we don't need to migrate anything - // The installer will do that - if (tables.length === 0) throw null - }) - .then(() => { - return db.Application.loadMigrationVersion() - }) - .then(actualVersion => { - if (actualVersion === null) { - return db.Application.create({ migrationVersion: 0 }).then(() => 0) - } +async function migrate () { + const tables = await db.sequelize.getQueryInterface().showAllTables() - return actualVersion - }) - .then(actualVersion => { - // No need migrations, abort - if (actualVersion >= LAST_MIGRATION_VERSION) throw null + // No tables, we don't need to migrate anything + // The installer will do that + if (tables.length === 0) return - return actualVersion - }) - .then(actualVersion => { - // If there are a new migration scripts - logger.info('Begin migrations.') + let actualVersion = await db.Application.loadMigrationVersion() + if (actualVersion === null) { + await db.Application.create({ migrationVersion: 0 }) + actualVersion = 0 + } - return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts })) - }) - .then(({ actualVersion, migrationScripts }) => { - return Promise.each(migrationScripts, entity => executeMigration(actualVersion, entity)) - }) - .then(() => { - logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION) - }) - .catch(err => { - if (err === null) return undefined + // No need migrations, abort + if (actualVersion >= LAST_MIGRATION_VERSION) return - throw err - }) + // If there are a new migration scripts + logger.info('Begin migrations.') + + const migrationScripts = await getMigrationScripts() - return p + for (const migrationScript of migrationScripts) { + await executeMigration(actualVersion, migrationScript) + } + + logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION) } // --------------------------------------------------------------------------- @@ -57,29 +40,28 @@ export { // --------------------------------------------------------------------------- -function getMigrationScripts () { - return readdirPromise(path.join(__dirname, 'migrations')).then(files => { - const filesToMigrate: { - version: string, - script: string - }[] = [] - - files - .filter(file => file.endsWith('.js.map') === false) - .forEach(file => { - // Filename is something like 'version-blabla.js' - const version = file.split('-')[0] - filesToMigrate.push({ - version, - script: file - }) +async function getMigrationScripts () { + const files = await readdirPromise(path.join(__dirname, 'migrations')) + const filesToMigrate: { + version: string, + script: string + }[] = [] + + files + .filter(file => file.endsWith('.js.map') === false) + .forEach(file => { + // Filename is something like 'version-blabla.js' + const version = file.split('-')[0] + filesToMigrate.push({ + version, + script: file }) + }) - return filesToMigrate - }) + return filesToMigrate } -function executeMigration (actualVersion: number, entity: { version: string, script: string }) { +async function executeMigration (actualVersion: number, entity: { version: string, script: string }) { const versionScript = parseInt(entity.version, 10) // Do not execute old migration scripts @@ -91,7 +73,7 @@ function executeMigration (actualVersion: number, entity: { version: string, scr const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) - return db.sequelize.transaction(t => { + await db.sequelize.transaction(async t => { const options = { transaction: t, queryInterface: db.sequelize.getQueryInterface(), @@ -99,10 +81,9 @@ function executeMigration (actualVersion: number, entity: { version: string, scr db } - return migrationScript.up(options) - .then(() => { - // Update the new migration version - return db.Application.updateMigrationVersion(versionScript, t) - }) + await migrationScript.up(options) + + // Update the new migration version + await db.Application.updateMigrationVersion(versionScript, t) }) } -- cgit v1.2.3