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 removePoolRequestsFromDB (ids) {
+ PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
+ if (err) {
+ logger.error('Cannot remove requests from the pool requests database.', { error: err })
+ return
+ }
+
+ logger.info('Pool requests flushed.')
+ })
+ }
+
function makePoolRequests () {
logger.info('Making pool requests to friends.')
- PoolRequestsDB.find({}, { type: 1, request: 1 }, function (err, pool_requests) {
+ PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) {
if (err) throw err
if (pool_requests.length === 0) return
var requests = {
- add: [],
- remove: []
+ add: {
+ ids: [],
+ requests: []
+ },
+ remove: {
+ ids: [],
+ requests: []
+ }
}
async.each(pool_requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') {
- requests.add.push(pool_request.request)
+ requests.add.requests.push(pool_request.request)
+ requests.add.ids.push(pool_request._id)
} else if (pool_request.type === 'remove') {
- requests.remove.push(pool_request.request)
+ requests.remove.requests.push(pool_request.request)
+ requests.remove.ids.push(pool_request._id)
} else {
throw new Error('Unkown pool request type.')
}
callback_each()
}, function () {
- makePoolRequest('add', requests.add)
- makePoolRequest('remove', requests.remove)
- logger.info('Pool requests to friends sent.')
+ // Send the add requests
+ if (requests.add.requests.length !== 0) {
+ makePoolRequest('add', requests.add.requests, function (err) {
+ if (err) logger.error('Errors when sent add pool requests.', { error: err })
+
+ removePoolRequestsFromDB(requests.add.ids)
+ })
+ }
+
+ // Send the remove requests
+ if (requests.remove.requests.length !== 0) {
+ makePoolRequest('remove', requests.remove.requests, function (err) {
+ if (err) logger.error('Errors when sent remove pool requests.', { error: err })
+
+ removePoolRequestsFromDB(requests.remove.ids)
+ })
+ }
})
})
}
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()
})
})
}
- function makePoolRequest (type, requests) {
+ function makePoolRequest (type, requests, callback) {
+ if (!callback) callback = function () {}
+
PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) {
if (err) throw err
}
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.')
}
}
function callbackAllPodsFinished (err) {
- if (err) {
- logger.error('There was some errors when sending the video meta data.', { error: err })
- }
+ if (err) return callback(err)
updatePodsScore(good_pods, bad_pods)
- PoolRequestsDB.remove().exec()
+ callback(null)
}
})
}
// ----------- Public -----------
poolRequests.activate = function () {
logger.info('Pool requests activated.')
- timer = setInterval(makePoolRequests, INTERVAL)
+ timer = setInterval(makePoolRequests, constants.INTERVAL)
}
poolRequests.addToPoolRequests = function (id, type, request) {