2015-12-04 10:13:32 -05:00
|
|
|
;(function () {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var async = require('async')
|
2016-01-31 05:23:52 -05:00
|
|
|
var pluck = require('lodash-node/compat/collection/pluck')
|
2015-12-04 10:13:32 -05:00
|
|
|
|
2016-01-30 11:05:22 -05:00
|
|
|
var constants = require('../initializers/constants')
|
2016-01-31 05:23:52 -05:00
|
|
|
var logger = require('../helpers/logger')
|
2016-02-04 15:10:33 -05:00
|
|
|
var Pods = require('../models/pods')
|
|
|
|
var PoolRequests = require('../models/poolRequests')
|
2016-02-05 12:03:20 -05:00
|
|
|
var requests = require('../helpers/requests')
|
2016-02-04 15:10:33 -05:00
|
|
|
var Videos = require('../models/videos')
|
2015-12-04 10:13:32 -05:00
|
|
|
|
|
|
|
var timer = null
|
|
|
|
|
2016-01-31 05:23:52 -05:00
|
|
|
var poolRequests = {
|
|
|
|
activate: activate,
|
2016-02-05 13:41:22 -05:00
|
|
|
addRequest: addRequest,
|
2016-01-31 05:23:52 -05:00
|
|
|
deactivate: deactivate,
|
|
|
|
forceSend: forceSend
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:41:22 -05:00
|
|
|
function activate () {
|
|
|
|
logger.info('Pool requests activated.')
|
|
|
|
timer = setInterval(makePoolRequests, constants.INTERVAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRequest (id, type, request) {
|
|
|
|
logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
|
|
|
|
|
|
|
|
PoolRequests.findById(id, function (err, entity) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find one pool request.', { error: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity) {
|
|
|
|
if (entity.type === type) {
|
|
|
|
logger.error('Cannot insert two same requests.')
|
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the request of the other type
|
|
|
|
PoolRequests.removeById(id, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove a pool request.', { error: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
PoolRequests.create(id, type, request, function (err) {
|
|
|
|
logger.error('Cannot create a pool request.', { error: err })
|
|
|
|
return // Abort
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-31 05:23:52 -05:00
|
|
|
function deactivate () {
|
|
|
|
logger.info('Pool requests deactivated.')
|
|
|
|
clearInterval(timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
function forceSend () {
|
|
|
|
logger.info('Force pool requests sending.')
|
|
|
|
makePoolRequests()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = poolRequests
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-02-05 12:03:20 -05:00
|
|
|
function makePoolRequest (type, requests_to_make, callback) {
|
2016-01-31 05:23:52 -05:00
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
2016-02-04 15:10:33 -05:00
|
|
|
Pods.list(function (err, pods) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) return callback(err)
|
2016-01-31 05:23:52 -05:00
|
|
|
|
|
|
|
var params = {
|
|
|
|
encrypt: true,
|
|
|
|
sign: true,
|
|
|
|
method: 'POST',
|
|
|
|
path: null,
|
2016-02-05 12:03:20 -05:00
|
|
|
data: requests_to_make
|
2016-01-31 05:23:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'add') {
|
|
|
|
params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
|
|
|
|
} else if (type === 'remove') {
|
|
|
|
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
|
|
|
|
} else {
|
2016-02-05 13:02:05 -05:00
|
|
|
return callback(new Error('Unkown pool request type.'))
|
2016-01-31 05:23:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var bad_pods = []
|
|
|
|
var good_pods = []
|
|
|
|
|
2016-02-05 12:03:20 -05:00
|
|
|
requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
|
2016-01-31 05:23:52 -05:00
|
|
|
|
|
|
|
function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
|
|
|
|
if (err || (response.statusCode !== 200 && response.statusCode !== 204)) {
|
|
|
|
bad_pods.push(pod._id)
|
|
|
|
logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
|
|
|
|
} else {
|
|
|
|
good_pods.push(pod._id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback_each_pod_finished()
|
|
|
|
}
|
|
|
|
|
|
|
|
function callbackAllPodsFinished (err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
updatePodsScore(good_pods, bad_pods)
|
|
|
|
callback(null)
|
|
|
|
}
|
2015-12-06 16:40:30 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-04 10:13:32 -05:00
|
|
|
function makePoolRequests () {
|
|
|
|
logger.info('Making pool requests to friends.')
|
|
|
|
|
2016-02-04 15:10:33 -05:00
|
|
|
PoolRequests.list(function (err, pool_requests) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of pool requests.', { err: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
2015-12-04 10:13:32 -05:00
|
|
|
|
2015-12-04 13:17:45 -05:00
|
|
|
if (pool_requests.length === 0) return
|
|
|
|
|
2016-02-05 12:03:20 -05:00
|
|
|
var requests_to_make = {
|
2015-12-06 16:40:30 -05:00
|
|
|
add: {
|
|
|
|
ids: [],
|
|
|
|
requests: []
|
|
|
|
},
|
|
|
|
remove: {
|
|
|
|
ids: [],
|
|
|
|
requests: []
|
|
|
|
}
|
2015-12-04 10:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async.each(pool_requests, function (pool_request, callback_each) {
|
|
|
|
if (pool_request.type === 'add') {
|
2016-02-05 12:03:20 -05:00
|
|
|
requests_to_make.add.requests.push(pool_request.request)
|
|
|
|
requests_to_make.add.ids.push(pool_request._id)
|
2015-12-04 10:13:32 -05:00
|
|
|
} else if (pool_request.type === 'remove') {
|
2016-02-05 12:03:20 -05:00
|
|
|
requests_to_make.remove.requests.push(pool_request.request)
|
|
|
|
requests_to_make.remove.ids.push(pool_request._id)
|
2015-12-04 10:13:32 -05:00
|
|
|
} else {
|
2016-02-05 13:02:05 -05:00
|
|
|
logger.error('Unkown pool request type.', { request_type: pool_request.type })
|
|
|
|
return // abort
|
2015-12-04 10:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
callback_each()
|
|
|
|
}, function () {
|
2015-12-06 16:40:30 -05:00
|
|
|
// Send the add requests
|
2016-02-05 12:03:20 -05:00
|
|
|
if (requests_to_make.add.requests.length !== 0) {
|
|
|
|
makePoolRequest('add', requests_to_make.add.requests, function (err) {
|
2015-12-06 16:40:30 -05:00
|
|
|
if (err) logger.error('Errors when sent add pool requests.', { error: err })
|
|
|
|
|
2016-02-05 12:03:20 -05:00
|
|
|
PoolRequests.removeRequests(requests_to_make.add.ids)
|
2015-12-06 16:40:30 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the remove requests
|
2016-02-05 12:03:20 -05:00
|
|
|
if (requests_to_make.remove.requests.length !== 0) {
|
|
|
|
makePoolRequest('remove', requests_to_make.remove.requests, function (err) {
|
2015-12-06 16:40:30 -05:00
|
|
|
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
|
|
|
|
|
2016-02-05 12:03:20 -05:00
|
|
|
PoolRequests.removeRequests(requests_to_make.remove.ids)
|
2015-12-06 16:40:30 -05:00
|
|
|
})
|
|
|
|
}
|
2015-12-04 10:13:32 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeBadPods () {
|
2016-02-04 15:10:33 -05:00
|
|
|
Pods.findBadPods(function (err, pods) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find bad pods.', { error: err })
|
|
|
|
return // abort
|
|
|
|
}
|
2015-12-04 10:13:32 -05:00
|
|
|
|
2016-01-23 12:31:58 -05:00
|
|
|
if (pods.length === 0) return
|
|
|
|
|
|
|
|
var urls = pluck(pods, 'url')
|
|
|
|
var ids = pluck(pods, '_id')
|
|
|
|
|
2016-02-04 15:10:33 -05:00
|
|
|
Videos.removeAllRemotesOf(urls, function (err, r) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove videos from a pod that we removing.', { error: err })
|
|
|
|
} else {
|
|
|
|
var videos_removed = r.result.n
|
|
|
|
logger.info('Removed %d videos.', videos_removed)
|
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-02-04 15:10:33 -05:00
|
|
|
Pods.removeAllByIds(ids, function (err, r) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove bad pods.', { error: err })
|
|
|
|
} else {
|
|
|
|
var pods_removed = r.result.n
|
|
|
|
logger.info('Removed %d pods.', pods_removed)
|
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
})
|
|
|
|
})
|
2015-12-04 10:13:32 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-31 05:23:52 -05:00
|
|
|
function updatePodsScore (good_pods, bad_pods) {
|
|
|
|
logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
|
2015-12-04 10:13:32 -05:00
|
|
|
|
2016-02-05 13:02:05 -05:00
|
|
|
Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) {
|
|
|
|
if (err) logger.error('Cannot increment scores of good pods.')
|
|
|
|
})
|
|
|
|
|
2016-02-04 15:10:33 -05:00
|
|
|
Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
|
2016-02-05 13:02:05 -05:00
|
|
|
if (err) logger.error('Cannot increment scores of bad pods.')
|
2016-01-31 05:23:52 -05:00
|
|
|
removeBadPods()
|
2015-12-04 10:13:32 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})()
|