From: Chocobozzz Date: Mon, 14 Dec 2015 20:09:25 +0000 (+0100) Subject: Create a constants module to easily modify some constants in a test X-Git-Tag: v0.0.1-alpha~1064 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=656ea8f70e848b03c0b668584dd533d72b376498;p=oweals%2Fpeertube.git Create a constants module to easily modify some constants in a test instance for example. --- diff --git a/routes/index.js b/routes/index.js index ecca80721..f45aa7ba5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,10 @@ ;(function () { 'use strict' + var constants = require('../src/constants') + var routes = { - api: require('./api/' + global.API_VERSION), + api: require('./api/' + constants.API_VERSION), views: require('./views') } diff --git a/server.js b/server.js index 4b93a7adf..ad57649b2 100644 --- a/server.js +++ b/server.js @@ -26,18 +26,14 @@ checker.createDirectoriesIfNotExist() - // ----------- Constants ----------- - var utils = require('./src/utils') - - global.API_VERSION = 'v1' - global.FRIEND_BASE_SCORE = utils.isTestInstance() ? 20 : 100 - // ----------- PeerTube modules ----------- var config = require('config') + var constants = require('./src/constants') var customValidators = require('./src/customValidators') var logger = require('./src/logger') var poolRequests = require('./src/poolRequests') var routes = require('./routes') + var utils = require('./src/utils') var videos = require('./src/videos') var webtorrent = require('./src/webTorrentNode') @@ -76,7 +72,7 @@ app.set('view engine', 'jade') // API routes - var api_route = '/api/' + global.API_VERSION + var api_route = '/api/' + constants.API_VERSION app.use(api_route, routes.api) // Views routes diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 000000000..00b713961 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,37 @@ +;(function () { + 'use strict' + + var constants = {} + + function isTestInstance () { + return (process.env.NODE_ENV === 'test') + } + + // API version of our pod + constants.API_VERSION = 'v1' + + // Score a pod has when we create it as a friend + constants.FRIEND_BASE_SCORE = 100 + + // Time to wait between requests to the friends + constants.INTERVAL = 60000 + + // Number of points we add/remove from a friend after a successful/bad request + constants.PODS_SCORE = { + MALUS: -10, + BONUS: 10 + } + + // Number of retries we make for the make retry requests (to friends...) + constants.REQUEST_RETRIES = 10 + + // Special constants for a test instance + if (isTestInstance() === true) { + constants.FRIEND_BASE_SCORE = 20 + constants.INTERVAL = 10000 + constants.REQUEST_RETRIES = 2 + } + + // ----------- Export ----------- + module.exports = constants +})() diff --git a/src/database.js b/src/database.js index 514a622dc..e03176b31 100644 --- a/src/database.js +++ b/src/database.js @@ -4,6 +4,7 @@ var config = require('config') var mongoose = require('mongoose') + var constants = require('./constants') var logger = require('./logger') var dbname = 'peertube' + config.get('database.suffix') @@ -25,7 +26,7 @@ var podsSchema = mongoose.Schema({ url: String, publicKey: String, - score: { type: Number, max: global.FRIEND_BASE_SCORE } + score: { type: Number, max: constants.FRIEND_BASE_SCORE } }) var PodsDB = mongoose.model('pods', podsSchema) diff --git a/src/pods.js b/src/pods.js index a0dbb6b68..8da216a55 100644 --- a/src/pods.js +++ b/src/pods.js @@ -6,6 +6,7 @@ var fs = require('fs') var request = require('request') + var constants = require('./constants') var logger = require('./logger') var PodsDB = require('./database').PodsDB var poolRequests = require('./poolRequests') @@ -20,7 +21,7 @@ // ----------- Private functions ----------- function getForeignPodsList (url, callback) { - var path = '/api/' + global.API_VERSION + '/pods' + var path = '/api/' + constants.API_VERSION + '/pods' request.get(url + path, function (err, response, body) { if (err) throw err @@ -48,7 +49,7 @@ var params = { url: data.url, publicKey: data.publicKey, - score: global.FRIEND_BASE_SCORE + score: constants.FRIEND_BASE_SCORE } PodsDB.create(params, function (err, pod) { @@ -142,14 +143,14 @@ } utils.makeMultipleRetryRequest( - { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, + { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, pods_list, function eachRequest (err, response, body, url, pod, callback_each_request) { // We add the pod if it responded correctly with its public certificate if (!err && response.statusCode === 200) { - pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) { + pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { if (err) { logger.error('Error with adding %s pod.', pod.url, { error: err }) } diff --git a/src/poolRequests.js b/src/poolRequests.js index 190fb3659..edb12b1e8 100644 --- a/src/poolRequests.js +++ b/src/poolRequests.js @@ -3,6 +3,7 @@ var async = require('async') + var constants = require('./constants') var logger = require('./logger') var database = require('./database') var PoolRequestsDB = database.PoolRequestsDB @@ -11,15 +12,6 @@ var poolRequests = {} - // ----------- Constants ----------- - - // Time to wait between requests to the friends - var INTERVAL = utils.isTestInstance() ? 10000 : 60000 - var PODS_SCORE = { - MALUS: -10, - BONUS: 10 - } - // ----------- Private ----------- var timer = null @@ -90,8 +82,8 @@ function updatePodsScore (good_pods, bad_pods) { logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: PODS_SCORE.BONUS } }, { multi: true }).exec() - PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: PODS_SCORE.MALUS } }, { multi: true }, function (err) { + PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() + PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { if (err) throw err removeBadPods() }) @@ -121,9 +113,9 @@ } if (type === 'add') { - params.path = '/api/' + global.API_VERSION + '/remotevideos/add' + params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' } else if (type === 'remove') { - params.path = '/api/' + global.API_VERSION + '/remotevideos/remove' + params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' } else { throw new Error('Unkown pool request type.') } @@ -156,7 +148,7 @@ // ----------- Public ----------- poolRequests.activate = function () { logger.info('Pool requests activated.') - timer = setInterval(makePoolRequests, INTERVAL) + timer = setInterval(makePoolRequests, constants.INTERVAL) } poolRequests.addToPoolRequests = function (id, type, request) { diff --git a/src/utils.js b/src/utils.js index 30edcd0e9..5880c6c90 100644 --- a/src/utils.js +++ b/src/utils.js @@ -10,6 +10,7 @@ var replay = require('request-replay') var ursa = require('ursa') + var constants = require('./constants') var logger = require('./logger') var utils = {} @@ -31,15 +32,13 @@ } logger.debug('Make retry requests to %s.', to_pod.url) - // Default 10 but in tests we want to be faster - var retries = utils.isTestInstance() ? 2 : 10 replay( request.post(params, function (err, response, body) { callbackEach(err, response, body, params.url, to_pod) }), { - retries: retries, + retries: constants.REQUEST_RETRIES, factor: 3, maxTimeout: Infinity, errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] @@ -195,9 +194,5 @@ process.kill(-webtorrent_process.pid) } - utils.isTestInstance = function () { - return (process.env.NODE_ENV === 'test') - } - module.exports = utils })()