mirror of https://github.com/Chocobozzz/PeerTube
Create a constants module to easily modify some constants in a test
instance for example.pull/10/head
parent
1d7974c758
commit
656ea8f70e
|
@ -1,8 +1,10 @@
|
||||||
;(function () {
|
;(function () {
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
var constants = require('../src/constants')
|
||||||
|
|
||||||
var routes = {
|
var routes = {
|
||||||
api: require('./api/' + global.API_VERSION),
|
api: require('./api/' + constants.API_VERSION),
|
||||||
views: require('./views')
|
views: require('./views')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
server.js
10
server.js
|
@ -26,18 +26,14 @@
|
||||||
|
|
||||||
checker.createDirectoriesIfNotExist()
|
checker.createDirectoriesIfNotExist()
|
||||||
|
|
||||||
// ----------- Constants -----------
|
|
||||||
var utils = require('./src/utils')
|
|
||||||
|
|
||||||
global.API_VERSION = 'v1'
|
|
||||||
global.FRIEND_BASE_SCORE = utils.isTestInstance() ? 20 : 100
|
|
||||||
|
|
||||||
// ----------- PeerTube modules -----------
|
// ----------- PeerTube modules -----------
|
||||||
var config = require('config')
|
var config = require('config')
|
||||||
|
var constants = require('./src/constants')
|
||||||
var customValidators = require('./src/customValidators')
|
var customValidators = require('./src/customValidators')
|
||||||
var logger = require('./src/logger')
|
var logger = require('./src/logger')
|
||||||
var poolRequests = require('./src/poolRequests')
|
var poolRequests = require('./src/poolRequests')
|
||||||
var routes = require('./routes')
|
var routes = require('./routes')
|
||||||
|
var utils = require('./src/utils')
|
||||||
var videos = require('./src/videos')
|
var videos = require('./src/videos')
|
||||||
var webtorrent = require('./src/webTorrentNode')
|
var webtorrent = require('./src/webTorrentNode')
|
||||||
|
|
||||||
|
@ -76,7 +72,7 @@
|
||||||
app.set('view engine', 'jade')
|
app.set('view engine', 'jade')
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
var api_route = '/api/' + global.API_VERSION
|
var api_route = '/api/' + constants.API_VERSION
|
||||||
app.use(api_route, routes.api)
|
app.use(api_route, routes.api)
|
||||||
|
|
||||||
// Views routes
|
// Views routes
|
||||||
|
|
|
@ -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
|
||||||
|
})()
|
|
@ -4,6 +4,7 @@
|
||||||
var config = require('config')
|
var config = require('config')
|
||||||
var mongoose = require('mongoose')
|
var mongoose = require('mongoose')
|
||||||
|
|
||||||
|
var constants = require('./constants')
|
||||||
var logger = require('./logger')
|
var logger = require('./logger')
|
||||||
|
|
||||||
var dbname = 'peertube' + config.get('database.suffix')
|
var dbname = 'peertube' + config.get('database.suffix')
|
||||||
|
@ -25,7 +26,7 @@
|
||||||
var podsSchema = mongoose.Schema({
|
var podsSchema = mongoose.Schema({
|
||||||
url: String,
|
url: String,
|
||||||
publicKey: 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)
|
var PodsDB = mongoose.model('pods', podsSchema)
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
var request = require('request')
|
var request = require('request')
|
||||||
|
|
||||||
|
var constants = require('./constants')
|
||||||
var logger = require('./logger')
|
var logger = require('./logger')
|
||||||
var PodsDB = require('./database').PodsDB
|
var PodsDB = require('./database').PodsDB
|
||||||
var poolRequests = require('./poolRequests')
|
var poolRequests = require('./poolRequests')
|
||||||
|
@ -20,7 +21,7 @@
|
||||||
// ----------- Private functions -----------
|
// ----------- Private functions -----------
|
||||||
|
|
||||||
function getForeignPodsList (url, callback) {
|
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) {
|
request.get(url + path, function (err, response, body) {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
|
@ -48,7 +49,7 @@
|
||||||
var params = {
|
var params = {
|
||||||
url: data.url,
|
url: data.url,
|
||||||
publicKey: data.publicKey,
|
publicKey: data.publicKey,
|
||||||
score: global.FRIEND_BASE_SCORE
|
score: constants.FRIEND_BASE_SCORE
|
||||||
}
|
}
|
||||||
|
|
||||||
PodsDB.create(params, function (err, pod) {
|
PodsDB.create(params, function (err, pod) {
|
||||||
|
@ -142,14 +143,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.makeMultipleRetryRequest(
|
utils.makeMultipleRetryRequest(
|
||||||
{ method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data },
|
{ method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
|
||||||
|
|
||||||
pods_list,
|
pods_list,
|
||||||
|
|
||||||
function eachRequest (err, response, body, url, pod, callback_each_request) {
|
function eachRequest (err, response, body, url, pod, callback_each_request) {
|
||||||
// We add the pod if it responded correctly with its public certificate
|
// We add the pod if it responded correctly with its public certificate
|
||||||
if (!err && response.statusCode === 200) {
|
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) {
|
if (err) {
|
||||||
logger.error('Error with adding %s pod.', pod.url, { error: err })
|
logger.error('Error with adding %s pod.', pod.url, { error: err })
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
var async = require('async')
|
var async = require('async')
|
||||||
|
|
||||||
|
var constants = require('./constants')
|
||||||
var logger = require('./logger')
|
var logger = require('./logger')
|
||||||
var database = require('./database')
|
var database = require('./database')
|
||||||
var PoolRequestsDB = database.PoolRequestsDB
|
var PoolRequestsDB = database.PoolRequestsDB
|
||||||
|
@ -11,15 +12,6 @@
|
||||||
|
|
||||||
var poolRequests = {}
|
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 -----------
|
// ----------- Private -----------
|
||||||
var timer = null
|
var timer = null
|
||||||
|
|
||||||
|
@ -90,8 +82,8 @@
|
||||||
function updatePodsScore (good_pods, bad_pods) {
|
function updatePodsScore (good_pods, bad_pods) {
|
||||||
logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
|
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: good_pods } }, { $inc: { score: constants.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: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
removeBadPods()
|
removeBadPods()
|
||||||
})
|
})
|
||||||
|
@ -121,9 +113,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'add') {
|
if (type === 'add') {
|
||||||
params.path = '/api/' + global.API_VERSION + '/remotevideos/add'
|
params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
|
||||||
} else if (type === 'remove') {
|
} else if (type === 'remove') {
|
||||||
params.path = '/api/' + global.API_VERSION + '/remotevideos/remove'
|
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Unkown pool request type.')
|
throw new Error('Unkown pool request type.')
|
||||||
}
|
}
|
||||||
|
@ -156,7 +148,7 @@
|
||||||
// ----------- Public -----------
|
// ----------- Public -----------
|
||||||
poolRequests.activate = function () {
|
poolRequests.activate = function () {
|
||||||
logger.info('Pool requests activated.')
|
logger.info('Pool requests activated.')
|
||||||
timer = setInterval(makePoolRequests, INTERVAL)
|
timer = setInterval(makePoolRequests, constants.INTERVAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
poolRequests.addToPoolRequests = function (id, type, request) {
|
poolRequests.addToPoolRequests = function (id, type, request) {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
var replay = require('request-replay')
|
var replay = require('request-replay')
|
||||||
var ursa = require('ursa')
|
var ursa = require('ursa')
|
||||||
|
|
||||||
|
var constants = require('./constants')
|
||||||
var logger = require('./logger')
|
var logger = require('./logger')
|
||||||
|
|
||||||
var utils = {}
|
var utils = {}
|
||||||
|
@ -31,15 +32,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Make retry requests to %s.', to_pod.url)
|
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(
|
replay(
|
||||||
request.post(params, function (err, response, body) {
|
request.post(params, function (err, response, body) {
|
||||||
callbackEach(err, response, body, params.url, to_pod)
|
callbackEach(err, response, body, params.url, to_pod)
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
retries: retries,
|
retries: constants.REQUEST_RETRIES,
|
||||||
factor: 3,
|
factor: 3,
|
||||||
maxTimeout: Infinity,
|
maxTimeout: Infinity,
|
||||||
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
|
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
|
||||||
|
@ -195,9 +194,5 @@
|
||||||
process.kill(-webtorrent_process.pid)
|
process.kill(-webtorrent_process.pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.isTestInstance = function () {
|
|
||||||
return (process.env.NODE_ENV === 'test')
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = utils
|
module.exports = utils
|
||||||
})()
|
})()
|
||||||
|
|
Loading…
Reference in New Issue