2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const async = require('async')
|
2016-04-26 14:56:46 -05:00
|
|
|
const map = require('lodash/map')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const constants = require('../initializers/constants')
|
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
const Pods = require('../models/pods')
|
2016-05-02 10:25:05 -05:00
|
|
|
const Requests = require('../models/requests')
|
2016-03-16 16:29:27 -05:00
|
|
|
const requests = require('../helpers/requests')
|
2016-05-10 14:19:24 -05:00
|
|
|
const videos = require('../lib/videos')
|
2016-03-16 16:29:27 -05:00
|
|
|
const Videos = require('../models/videos')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
const REQUEST_SCHEDULER_TYPE = constants.REQUEST_SCHEDULER_TYPE
|
2016-03-16 16:29:27 -05:00
|
|
|
let timer = null
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-05-02 10:25:05 -05:00
|
|
|
const requestsScheduler = {
|
2016-02-07 04:23:23 -06:00
|
|
|
activate: activate,
|
|
|
|
addRequest: addRequest,
|
|
|
|
deactivate: deactivate,
|
|
|
|
forceSend: forceSend
|
|
|
|
}
|
|
|
|
|
|
|
|
function activate () {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.info('Requests scheduler activated.')
|
|
|
|
timer = setInterval(makeRequests, constants.INTERVAL)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// Add request to the scheduler
|
2016-02-07 04:23:23 -06:00
|
|
|
function addRequest (id, type, request) {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.debug('Add request to the requests scheduler.', { id: id, type: type, request: request })
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-05-02 10:25:05 -05:00
|
|
|
Requests.findById(id, function (err, entity) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) {
|
2016-06-06 08:28:33 -05:00
|
|
|
logger.error('Error when trying to find a request.', { error: err })
|
2016-02-07 04:23:23 -06:00
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// If there were already a request with this id in the scheduler...
|
2016-02-07 04:23:23 -06:00
|
|
|
if (entity) {
|
|
|
|
if (entity.type === type) {
|
|
|
|
logger.error('Cannot insert two same requests.')
|
2016-02-05 12:41:22 -06:00
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// Remove the request of the other type
|
2016-05-02 10:25:05 -05:00
|
|
|
Requests.removeRequestById(id, function (err) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.error('Cannot remove a request.', { error: err })
|
2016-02-05 12:41:22 -06:00
|
|
|
return // Abort
|
|
|
|
}
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
} else {
|
2016-05-02 10:25:05 -05:00
|
|
|
Requests.create(id, type, request, function (err) {
|
|
|
|
if (err) logger.error('Cannot create a request.', { error: err })
|
2016-02-07 04:23:23 -06:00
|
|
|
return // Abort
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-02-05 12:41:22 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function deactivate () {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.info('Requests scheduler deactivated.')
|
2016-02-07 04:23:23 -06:00
|
|
|
clearInterval(timer)
|
|
|
|
}
|
2016-02-05 12:41:22 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function forceSend () {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.info('Force requests scheduler sending.')
|
|
|
|
makeRequests()
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-05-02 10:25:05 -05:00
|
|
|
module.exports = requestsScheduler
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// Make a requests to friends of a certain type
|
2016-05-11 14:19:34 -05:00
|
|
|
function makeRequest (type, requestsToMake, callback) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (!callback) callback = function () {}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
Pods.list(function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const params = {
|
2016-06-06 08:28:33 -05:00
|
|
|
encrypt: true, // Security
|
|
|
|
sign: true, // To prove our identity
|
2016-02-07 04:23:23 -06:00
|
|
|
method: 'POST',
|
2016-06-06 08:28:33 -05:00
|
|
|
path: null, // We build the path later
|
|
|
|
data: requestsToMake // Requests we need to make
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// If this is a valid type, we build the path
|
|
|
|
if (REQUEST_SCHEDULER_TYPE.indexOf(type) > -1) {
|
|
|
|
params.path = '/api/' + constants.API_VERSION + '/remotevideos/' + type
|
2016-02-07 04:23:23 -06:00
|
|
|
} else {
|
|
|
|
return callback(new Error('Unkown pool request type.'))
|
|
|
|
}
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
const badPods = []
|
|
|
|
const goodPods = []
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// Make multiple retry requests to all of pods
|
|
|
|
// The function fire some useful callbacks
|
2016-02-07 04:23:23 -06:00
|
|
|
requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
function callbackEachPodFinished (err, response, body, url, pod, callbackEachPodFinished) {
|
2016-06-06 08:28:33 -05:00
|
|
|
// We failed the request, add the pod unreachable to the bad pods list
|
2016-05-10 14:19:24 -05:00
|
|
|
if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) {
|
2016-05-11 14:19:34 -05:00
|
|
|
badPods.push(pod._id)
|
2016-02-07 04:23:23 -06:00
|
|
|
logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
|
2016-01-31 04:23:52 -06:00
|
|
|
} else {
|
2016-06-06 08:28:33 -05:00
|
|
|
// Request success
|
2016-05-11 14:19:34 -05:00
|
|
|
goodPods.push(pod._id)
|
2016-01-31 04:23:52 -06:00
|
|
|
}
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
return callbackEachPodFinished()
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function callbackAllPodsFinished (err) {
|
|
|
|
if (err) return callback(err)
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// All the requests were made, we update the pods score
|
2016-05-11 14:19:34 -05:00
|
|
|
updatePodsScore(goodPods, badPods)
|
2016-02-07 04:23:23 -06:00
|
|
|
callback(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// Make all the requests of the scheduler
|
2016-05-02 10:25:05 -05:00
|
|
|
function makeRequests () {
|
|
|
|
Requests.list(function (err, requests) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) {
|
2016-05-02 10:25:05 -05:00
|
|
|
logger.error('Cannot get the list of requests.', { err: err })
|
2016-02-07 04:23:23 -06:00
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// If there are no requests, abort
|
|
|
|
if (requests.length === 0) {
|
|
|
|
logger.info('No requests to make.')
|
|
|
|
return
|
|
|
|
}
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
logger.info('Making requests to friends.')
|
|
|
|
|
|
|
|
const requestsToMake = {}
|
|
|
|
for (const type of REQUEST_SCHEDULER_TYPE) {
|
|
|
|
requestsToMake[type] = {
|
2016-02-07 04:23:23 -06:00
|
|
|
ids: [],
|
|
|
|
requests: []
|
2016-01-31 04:23:52 -06:00
|
|
|
}
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// For each requests to make, we add it to the correct request type
|
2016-05-11 14:19:34 -05:00
|
|
|
async.each(requests, function (poolRequest, callbackEach) {
|
2016-06-06 08:28:33 -05:00
|
|
|
if (REQUEST_SCHEDULER_TYPE.indexOf(poolRequest.type) > -1) {
|
|
|
|
const requestTypeToMake = requestsToMake[poolRequest.type]
|
|
|
|
requestTypeToMake.requests.push(poolRequest.request)
|
|
|
|
requestTypeToMake.ids.push(poolRequest._id)
|
2016-02-07 04:23:23 -06:00
|
|
|
} else {
|
2016-05-11 14:19:34 -05:00
|
|
|
logger.error('Unkown request type.', { request_type: poolRequest.type })
|
2016-02-07 04:23:23 -06:00
|
|
|
return // abort
|
2016-01-31 04:23:52 -06:00
|
|
|
}
|
2015-12-06 15:40:30 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
callbackEach()
|
2016-02-07 04:23:23 -06:00
|
|
|
}, function () {
|
2016-06-06 08:28:33 -05:00
|
|
|
for (let type of Object.keys(requestsToMake)) {
|
|
|
|
const requestTypeToMake = requestsToMake[type]
|
|
|
|
// If there are requests for this type
|
|
|
|
if (requestTypeToMake.requests.length !== 0) {
|
|
|
|
makeRequest(type, requestTypeToMake.requests, function (err) {
|
|
|
|
if (err) logger.error('Errors when sent ' + type + ' requests.', { error: err })
|
|
|
|
|
|
|
|
// We made the requests, so we can remove them from the scheduler
|
|
|
|
Requests.removeRequests(requestTypeToMake.ids)
|
|
|
|
})
|
|
|
|
}
|
2015-12-04 09:13:32 -06:00
|
|
|
}
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-06-06 08:28:33 -05:00
|
|
|
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeBadPods () {
|
2016-05-15 11:03:43 -05:00
|
|
|
async.waterfall([
|
|
|
|
function findBadPods (callback) {
|
|
|
|
Pods.findBadPods(function (err, pods) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find bad pods.', { error: err })
|
|
|
|
return callback(err)
|
|
|
|
}
|
2015-12-06 15:40:30 -06:00
|
|
|
|
2016-05-15 11:03:43 -05:00
|
|
|
return callback(null, pods)
|
|
|
|
})
|
|
|
|
},
|
2015-12-06 15:40:30 -06:00
|
|
|
|
2016-05-15 11:03:43 -05:00
|
|
|
function listVideosOfTheseBadPods (pods, callback) {
|
|
|
|
if (pods.length === 0) return callback(null)
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-05-15 11:03:43 -05:00
|
|
|
const urls = map(pods, 'url')
|
|
|
|
const ids = map(pods, '_id')
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-05-15 11:03:43 -05:00
|
|
|
Videos.listFromUrls(urls, function (err, videosList) {
|
2016-02-05 12:02:05 -06:00
|
|
|
if (err) {
|
2016-05-15 11:03:43 -05:00
|
|
|
logger.error('Cannot list videos urls.', { error: err, urls: urls })
|
|
|
|
return callback(null, ids, [])
|
2016-02-05 12:02:05 -06:00
|
|
|
}
|
2016-05-15 11:03:43 -05:00
|
|
|
|
|
|
|
return callback(null, ids, videosList)
|
2016-01-23 11:31:58 -06:00
|
|
|
})
|
2016-05-15 11:03:43 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
function removeVideosOfTheseBadPods (podIds, videosList, callback) {
|
|
|
|
// We don't have to remove pods, skip
|
|
|
|
if (typeof podIds === 'function') return podIds(null)
|
|
|
|
|
|
|
|
// Remove the remote videos
|
|
|
|
videos.removeRemoteVideos(videosList, function (err) {
|
|
|
|
if (err) logger.error('Cannot remove remote videos.', { error: err })
|
|
|
|
|
|
|
|
return callback(null, podIds)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
function removeBadPodsFromDB (podIds, callback) {
|
|
|
|
// We don't have to remove pods, skip
|
|
|
|
if (typeof podIds === 'function') return podIds(null)
|
|
|
|
|
|
|
|
Pods.removeAllByIds(podIds, callback)
|
|
|
|
}
|
|
|
|
], function (err, removeResult) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove bad pods.', { error: err })
|
|
|
|
} else if (removeResult) {
|
|
|
|
const podsRemoved = removeResult.result.n
|
|
|
|
logger.info('Removed %d pods.', podsRemoved)
|
|
|
|
} else {
|
|
|
|
logger.info('No need to remove bad pods.')
|
|
|
|
}
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
function updatePodsScore (goodPods, badPods) {
|
|
|
|
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) logger.error('Cannot increment scores of good pods.')
|
|
|
|
})
|
2016-02-05 12:02:05 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
|
2016-06-06 08:28:33 -05:00
|
|
|
if (err) logger.error('Cannot decrement scores of bad pods.')
|
2016-02-07 04:23:23 -06:00
|
|
|
removeBadPods()
|
|
|
|
})
|
|
|
|
}
|