instance for example.
;(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')
}
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')
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
--- /dev/null
+;(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
+})()
var config = require('config')
var mongoose = require('mongoose')
+ var constants = require('./constants')
var logger = require('./logger')
var dbname = 'peertube' + config.get('database.suffix')
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)
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')
// ----------- 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
var params = {
url: data.url,
publicKey: data.publicKey,
- score: global.FRIEND_BASE_SCORE
+ score: constants.FRIEND_BASE_SCORE
}
PodsDB.create(params, function (err, pod) {
}
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 })
}
var async = require('async')
+ var constants = require('./constants')
var logger = require('./logger')
var database = require('./database')
var PoolRequestsDB = database.PoolRequestsDB
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
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()
})
}
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.')
}
// ----------- Public -----------
poolRequests.activate = function () {
logger.info('Pool requests activated.')
- timer = setInterval(makePoolRequests, INTERVAL)
+ timer = setInterval(makePoolRequests, constants.INTERVAL)
}
poolRequests.addToPoolRequests = function (id, type, request) {
var replay = require('request-replay')
var ursa = require('ursa')
+ var constants = require('./constants')
var logger = require('./logger')
var utils = {}
}
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' ]
process.kill(-webtorrent_process.pid)
}
- utils.isTestInstance = function () {
- return (process.env.NODE_ENV === 'test')
- }
-
module.exports = utils
})()