From 6fcd19ba737f1f5614a56c6925adb882dea43b8d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Jul 2017 13:26:25 +0200 Subject: Move to promises Closes https://github.com/Chocobozzz/PeerTube/issues/74 --- server/models/pod/pod-interface.ts | 28 ++++----- server/models/pod/pod.ts | 115 +++++++++++++++---------------------- 2 files changed, 56 insertions(+), 87 deletions(-) (limited to 'server/models/pod') diff --git a/server/models/pod/pod-interface.ts b/server/models/pod/pod-interface.ts index d88847c45..f6963d47e 100644 --- a/server/models/pod/pod-interface.ts +++ b/server/models/pod/pod-interface.ts @@ -1,4 +1,5 @@ import * as Sequelize from 'sequelize' +import * as Promise from 'bluebird' // Don't use barrel, import just what we need import { Pod as FormatedPod } from '../../../shared/models/pod.model' @@ -6,32 +7,23 @@ import { Pod as FormatedPod } from '../../../shared/models/pod.model' export namespace PodMethods { export type ToFormatedJSON = (this: PodInstance) => FormatedPod - export type CountAllCallback = (err: Error, total: number) => void - export type CountAll = (callback) => void + export type CountAll = () => Promise - export type IncrementScoresCallback = (err: Error) => void - export type IncrementScores = (ids: number[], value: number, callback?: IncrementScoresCallback) => void + export type IncrementScores = (ids: number[], value: number) => Promise<[ number, PodInstance[] ]> - export type ListCallback = (err: Error, podInstances?: PodInstance[]) => void - export type List = (callback: ListCallback) => void + export type List = () => Promise - export type ListAllIdsCallback = (err: Error, ids?: number[]) => void - export type ListAllIds = (transaction: Sequelize.Transaction, callback: ListAllIdsCallback) => void + export type ListAllIds = (transaction: Sequelize.Transaction) => Promise - export type ListRandomPodIdsWithRequestCallback = (err: Error, podInstanceIds?: number[]) => void - export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: ListRandomPodIdsWithRequestCallback) => void + export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string) => Promise - export type ListBadPodsCallback = (err: Error, podInstances?: PodInstance[]) => void - export type ListBadPods = (callback: ListBadPodsCallback) => void + export type ListBadPods = () => Promise - export type LoadCallback = (err: Error, podInstance: PodInstance) => void - export type Load = (id: number, callback: LoadCallback) => void + export type Load = (id: number) => Promise - export type LoadByHostCallback = (err: Error, podInstance: PodInstance) => void - export type LoadByHost = (host: string, callback: LoadByHostCallback) => void + export type LoadByHost = (host: string) => Promise - export type RemoveAllCallback = (err: Error) => void - export type RemoveAll = (callback: RemoveAllCallback) => void + export type RemoveAll = () => Promise export type UpdatePodsScore = (goodPods: number[], badPods: number[]) => void } diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts index 4fe7fda1c..9209380fc 100644 --- a/server/models/pod/pod.ts +++ b/server/models/pod/pod.ts @@ -1,4 +1,3 @@ -import { each, waterfall } from 'async' import { map } from 'lodash' import * as Sequelize from 'sequelize' @@ -7,7 +6,6 @@ import { logger, isHostValid } from '../../helpers' import { addMethodsToModel } from '../utils' import { - PodClass, PodInstance, PodAttributes, @@ -118,13 +116,11 @@ function associate (models) { }) } -countAll = function (callback: PodMethods.CountAllCallback) { - return Pod.count().asCallback(callback) +countAll = function () { + return Pod.count() } -incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) { - if (!callback) callback = function () { /* empty */ } - +incrementScores = function (ids: number[], value: number) { const update = { score: Sequelize.literal('score +' + value) } @@ -139,33 +135,28 @@ incrementScores = function (ids: number[], value: number, callback?: PodMethods. validate: false } - return Pod.update(update, options).asCallback(callback) + return Pod.update(update, options) } -list = function (callback: PodMethods.ListCallback) { - return Pod.findAll().asCallback(callback) +list = function () { + return Pod.findAll() } -listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) { - const query: any = { - attributes: [ 'id' ] +listAllIds = function (transaction: Sequelize.Transaction) { + const query: Sequelize.FindOptions = { + attributes: [ 'id' ], + transaction } - if (transaction !== null) query.transaction = transaction - - return Pod.findAll(query).asCallback(function (err: Error, pods) { - if (err) return callback(err) - - return callback(null, map(pods, 'id')) + return Pod.findAll(query).then(pods => { + return map(pods, 'id') }) } -listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) { - Pod.count().asCallback(function (err, count) { - if (err) return callback(err) - +listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) { + return Pod.count().then(count => { // Optimization... - if (count === 0) return callback(null, []) + if (count === 0) return [] let start = Math.floor(Math.random() * count) - limit if (start < 0) start = 0 @@ -186,56 +177,55 @@ listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, ta } } - return Pod.findAll(query).asCallback(function (err, pods) { - if (err) return callback(err) - - return callback(null, map(pods, 'id')) + return Pod.findAll(query).then(pods => { + return map(pods, 'id') }) }) } -listBadPods = function (callback: PodMethods.ListBadPodsCallback) { +listBadPods = function () { const query = { where: { score: { $lte: 0 } } } - return Pod.findAll(query).asCallback(callback) + return Pod.findAll(query) } -load = function (id: number, callback: PodMethods.LoadCallback) { - return Pod.findById(id).asCallback(callback) +load = function (id: number) { + return Pod.findById(id) } -loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) { +loadByHost = function (host: string) { const query = { where: { host: host } } - return Pod.findOne(query).asCallback(callback) + return Pod.findOne(query) } -removeAll = function (callback: PodMethods.RemoveAllCallback) { - return Pod.destroy().asCallback(callback) +removeAll = function () { + return Pod.destroy() } updatePodsScore = function (goodPods: number[], badPods: number[]) { logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) if (goodPods.length !== 0) { - incrementScores(goodPods, PODS_SCORE.BONUS, function (err) { - if (err) logger.error('Cannot increment scores of good pods.', { error: err }) + incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => { + logger.error('Cannot increment scores of good pods.', { error: err }) }) } if (badPods.length !== 0) { - incrementScores(badPods, PODS_SCORE.MALUS, function (err) { - if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) - removeBadPods() - }) + incrementScores(badPods, PODS_SCORE.MALUS) + .then(() => removeBadPods()) + .catch(err => { + if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) + }) } } @@ -243,32 +233,19 @@ updatePodsScore = function (goodPods: number[], badPods: number[]) { // Remove pods with a score of 0 (too many requests where they were unreachable) function removeBadPods () { - waterfall([ - function findBadPods (callback) { - listBadPods(function (err, pods) { - if (err) { - logger.error('Cannot find bad pods.', { error: err }) - return callback(err) - } - - return callback(null, pods) - }) - }, - - function removeTheseBadPods (pods, callback) { - each(pods, function (pod: any, callbackEach) { - pod.destroy().asCallback(callbackEach) - }, function (err) { - return callback(err, pods.length) - }) - } - ], function (err, numberOfPodsRemoved) { - if (err) { + return listBadPods() + .then(pods => { + const podsRemovePromises = pods.map(pod => pod.destroy()) + return Promise.all(podsRemovePromises).then(() => pods.length) + }) + .then(numberOfPodsRemoved => { + if (numberOfPodsRemoved) { + logger.info('Removed %d pods.', numberOfPodsRemoved) + } else { + logger.info('No need to remove bad pods.') + } + }) + .catch(err => { logger.error('Cannot remove bad pods.', { error: err }) - } else if (numberOfPodsRemoved) { - logger.info('Removed %d pods.', numberOfPodsRemoved) - } else { - logger.info('No need to remove bad pods.') - } - }) + }) } -- cgit v1.2.3