X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Frequest.js;h=bae227c058aaa902ecdba0acc682a5f743bbb64a;hb=bdfbd4f162d66c3a6bd7c312a99e0b692e830792;hp=882f747b76f76eaaa4e984f3f86a87c05f733c26;hpb=feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/request.js b/server/models/request.js index 882f747b7..bae227c05 100644 --- a/server/models/request.js +++ b/server/models/request.js @@ -3,6 +3,7 @@ const each = require('async/each') const eachLimit = require('async/eachLimit') const waterfall = require('async/waterfall') +const values = require('lodash/values') const constants = require('../initializers/constants') const logger = require('../helpers/logger') @@ -17,11 +18,12 @@ module.exports = function (sequelize, DataTypes) { const Request = sequelize.define('Request', { request: { - type: DataTypes.JSON + type: DataTypes.JSON, + allowNull: false }, endpoint: { - // TODO: enum? - type: DataTypes.STRING + type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)), + allowNull: false } }, { @@ -79,9 +81,11 @@ function deactivate () { timer = null } -function flush () { +function flush (callback) { removeAll.call(this, function (err) { if (err) logger.error('Cannot flush the requests.', { error: err }) + + return callback(err) }) } @@ -118,7 +122,7 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) { 'Error sending secure request to %s pod.', toPod.host, { - error: err || new Error('Status code not 20x : ' + res.statusCode) + error: err ? err.message : 'Status code not 20x : ' + res.statusCode } ) @@ -194,7 +198,7 @@ function makeRequests () { makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) { if (success === true) { - logger.debug('Removing requests for %s pod.', requestToMake.toPodId, { requestsIds: requestToMake.ids }) + logger.debug('Removing requests for pod %s.', requestToMake.toPodId, { requestsIds: requestToMake.ids }) goodPods.push(requestToMake.toPodId) @@ -259,13 +263,13 @@ function updatePodsScore (goodPods, badPods) { if (goodPods.length !== 0) { Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { - if (err) logger.error('Cannot increment scores of good pods.') + if (err) logger.error('Cannot increment scores of good pods.', { error: err }) }) } if (badPods.length !== 0) { Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { - if (err) logger.error('Cannot decrement scores of bad pods.') + if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) removeBadPods.call(self) }) } @@ -298,7 +302,7 @@ function listWithLimitAndRandom (limit, callback) { function removeAll (callback) { // Delete all requests - this.destroy({ truncate: true }).asCallback(callback) + this.truncate({ cascade: true }).asCallback(callback) } function removeWithEmptyTo (callback) {