2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const async = require('async')
|
2016-04-26 21:56:46 +02:00
|
|
|
const map = require('lodash/map')
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const constants = require('../initializers/constants')
|
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
const Pods = require('../models/pods')
|
|
|
|
const PoolRequests = require('../models/poolRequests')
|
|
|
|
const requests = require('../helpers/requests')
|
|
|
|
const Videos = require('../models/videos')
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
let timer = null
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const poolRequests = {
|
2016-02-07 11:23:23 +01:00
|
|
|
activate: activate,
|
|
|
|
addRequest: addRequest,
|
|
|
|
deactivate: deactivate,
|
|
|
|
forceSend: forceSend
|
|
|
|
}
|
|
|
|
|
|
|
|
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.')
|
2016-02-05 19:41:22 +01:00
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// Remove the request of the other type
|
|
|
|
PoolRequests.removeRequestById(id, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove a pool request.', { error: err })
|
2016-02-05 19:41:22 +01:00
|
|
|
return // Abort
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
PoolRequests.create(id, type, request, function (err) {
|
|
|
|
if (err) logger.error('Cannot create a pool request.', { error: err })
|
|
|
|
return // Abort
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-02-05 19:41:22 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function deactivate () {
|
|
|
|
logger.info('Pool requests deactivated.')
|
|
|
|
clearInterval(timer)
|
|
|
|
}
|
2016-02-05 19:41:22 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function forceSend () {
|
|
|
|
logger.info('Force pool requests sending.')
|
|
|
|
makePoolRequests()
|
|
|
|
}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
module.exports = poolRequests
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function makePoolRequest (type, requests_to_make, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.list(function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const params = {
|
2016-02-07 11:23:23 +01:00
|
|
|
encrypt: true,
|
|
|
|
sign: true,
|
|
|
|
method: 'POST',
|
|
|
|
path: null,
|
|
|
|
data: requests_to_make
|
|
|
|
}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01: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 {
|
|
|
|
return callback(new Error('Unkown pool request type.'))
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const bad_pods = []
|
|
|
|
const good_pods = []
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
|
|
|
|
|
|
|
|
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') })
|
2016-01-31 11:23:52 +01:00
|
|
|
} else {
|
2016-02-07 11:23:23 +01:00
|
|
|
good_pods.push(pod._id)
|
2016-01-31 11:23:52 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
return callback_each_pod_finished()
|
|
|
|
}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function callbackAllPodsFinished (err) {
|
|
|
|
if (err) return callback(err)
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
updatePodsScore(good_pods, bad_pods)
|
|
|
|
callback(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePoolRequests () {
|
|
|
|
logger.info('Making pool requests to friends.')
|
|
|
|
|
|
|
|
PoolRequests.list(function (err, pool_requests) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of pool requests.', { err: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool_requests.length === 0) return
|
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const requests_to_make = {
|
2016-02-07 11:23:23 +01:00
|
|
|
add: {
|
|
|
|
ids: [],
|
|
|
|
requests: []
|
|
|
|
},
|
|
|
|
remove: {
|
|
|
|
ids: [],
|
|
|
|
requests: []
|
2016-01-31 11:23:52 +01:00
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async.each(pool_requests, function (pool_request, callback_each) {
|
|
|
|
if (pool_request.type === 'add') {
|
|
|
|
requests_to_make.add.requests.push(pool_request.request)
|
|
|
|
requests_to_make.add.ids.push(pool_request._id)
|
|
|
|
} else if (pool_request.type === 'remove') {
|
|
|
|
requests_to_make.remove.requests.push(pool_request.request)
|
|
|
|
requests_to_make.remove.ids.push(pool_request._id)
|
|
|
|
} else {
|
|
|
|
logger.error('Unkown pool request type.', { request_type: pool_request.type })
|
|
|
|
return // abort
|
2016-01-31 11:23:52 +01:00
|
|
|
}
|
2015-12-06 22:40:30 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
callback_each()
|
|
|
|
}, function () {
|
|
|
|
// Send the add requests
|
|
|
|
if (requests_to_make.add.requests.length !== 0) {
|
|
|
|
makePoolRequest('add', requests_to_make.add.requests, function (err) {
|
|
|
|
if (err) logger.error('Errors when sent add pool requests.', { error: err })
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
PoolRequests.removeRequests(requests_to_make.add.ids)
|
|
|
|
})
|
2016-02-05 19:02:05 +01:00
|
|
|
}
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// Send the remove requests
|
|
|
|
if (requests_to_make.remove.requests.length !== 0) {
|
|
|
|
makePoolRequest('remove', requests_to_make.remove.requests, function (err) {
|
|
|
|
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
|
2015-12-04 19:17:45 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
PoolRequests.removeRequests(requests_to_make.remove.ids)
|
|
|
|
})
|
2015-12-04 16:13:32 +01:00
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function removeBadPods () {
|
|
|
|
Pods.findBadPods(function (err, pods) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find bad pods.', { error: err })
|
|
|
|
return // abort
|
|
|
|
}
|
2015-12-06 22:40:30 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
if (pods.length === 0) return
|
2015-12-06 22:40:30 +01:00
|
|
|
|
2016-04-19 22:06:46 +02:00
|
|
|
const urls = map(pods, 'url')
|
|
|
|
const ids = map(pods, '_id')
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Videos.removeAllRemotesOf(urls, function (err, r) {
|
2016-02-05 19:02:05 +01:00
|
|
|
if (err) {
|
2016-02-07 11:23:23 +01:00
|
|
|
logger.error('Cannot remove videos from a pod that we removing.', { error: err })
|
|
|
|
} else {
|
2016-03-16 22:29:27 +01:00
|
|
|
const videos_removed = r.result.n
|
2016-02-07 11:23:23 +01:00
|
|
|
logger.info('Removed %d videos.', videos_removed)
|
2016-02-05 19:02:05 +01:00
|
|
|
}
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.removeAllByIds(ids, function (err, r) {
|
2016-02-05 19:02:05 +01:00
|
|
|
if (err) {
|
2016-02-07 11:23:23 +01:00
|
|
|
logger.error('Cannot remove bad pods.', { error: err })
|
2016-02-05 19:02:05 +01:00
|
|
|
} else {
|
2016-03-16 22:29:27 +01:00
|
|
|
const pods_removed = r.result.n
|
2016-02-07 11:23:23 +01:00
|
|
|
logger.info('Removed %d pods.', pods_removed)
|
2016-02-05 19:02:05 +01:00
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
})
|
2015-12-04 16:13:32 +01:00
|
|
|
})
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
}
|
2015-12-04 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01: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 16:13:32 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) {
|
|
|
|
if (err) logger.error('Cannot increment scores of good pods.')
|
|
|
|
})
|
2016-02-05 19:02:05 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
|
|
|
|
if (err) logger.error('Cannot increment scores of bad pods.')
|
|
|
|
removeBadPods()
|
|
|
|
})
|
|
|
|
}
|