Create a constants module to easily modify some constants in a test

instance for example.
pull/10/head
Chocobozzz 2015-12-14 21:09:25 +01:00
parent 1d7974c758
commit 656ea8f70e
7 changed files with 58 additions and 34 deletions

View File

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

View File

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

37
src/constants.js Normal file
View File

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

View File

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

View File

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

View File

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

View File

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