'use strict'
-const async = require('async')
+const each = require('async/each')
+const eachLimit = require('async/eachLimit')
const map = require('lodash/map')
const mongoose = require('mongoose')
+const waterfall = require('async/waterfall')
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const Video = mongoose.model('Video')
let timer = null
+let lastRequestTimestamp = 0
// ---------------------------------------------------------------------------
const RequestSchema = mongoose.Schema({
request: mongoose.Schema.Types.Mixed,
- to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ]
+ to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ]
})
RequestSchema.statics = {
activate,
deactivate,
flush,
- forceSend
+ forceSend,
+ list,
+ remainingMilliSeconds
}
RequestSchema.pre('save', function (next) {
function activate () {
logger.info('Requests scheduler activated.')
- timer = setInterval(makeRequests.bind(this), constants.INTERVAL)
+ lastRequestTimestamp = Date.now()
+
+ const self = this
+ timer = setInterval(function () {
+ lastRequestTimestamp = Date.now()
+ makeRequests.call(self)
+ }, constants.REQUESTS_INTERVAL)
}
function deactivate () {
logger.info('Requests scheduler deactivated.')
clearInterval(timer)
+ timer = null
}
function flush () {
makeRequests.call(this)
}
+function list (callback) {
+ this.find({ }, callback)
+}
+
+function remainingMilliSeconds () {
+ if (timer === null) return -1
+
+ return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp)
+}
+
// ---------------------------------------------------------------------------
// Make a requests to friends of a certain type
// The function fire some useful callbacks
requests.makeSecureRequest(params, function (err, res) {
if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
- logger.error('Error sending secure request to %s pod.', toPod.url, { error: err || new Error('Status code not 20x') })
+ logger.error(
+ 'Error sending secure request to %s pod.',
+ toPod.url,
+ {
+ error: err || new Error('Status code not 20x : ' + res.statusCode)
+ }
+ )
return callback(false)
}
const goodPods = []
const badPods = []
- async.eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
+ eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
const requestToMake = requestsToMake[toPodId]
// FIXME: mongodb request inside a loop :/
return callbackEach()
}
- // Maybe the pod is not our friend anymore so simply remove them
+ // Maybe the pod is not our friend anymore so simply remove it
if (!toPod) {
removePodOf.call(self, requestToMake.ids, toPodId)
return callbackEach()
}
makeRequest(toPod, requestToMake.datas, function (success) {
- if (err) {
- logger.error('Errors when sent request to %s.', toPod.url, { error: err })
- // Do not stop the process just for one error
- return callbackEach()
- }
-
if (success === true) {
logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids })
// Remove pods with a score of 0 (too many requests where they were unreachable)
function removeBadPods () {
- async.waterfall([
+ waterfall([
function findBadPods (callback) {
Pod.listBadPods(function (err, pods) {
if (err) {
return callback(null)
}
- async.each(videosList, function (video, callbackEach) {
+ each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, function (err) {
if (err) {
return callback(null)
}
- async.each(pods, function (pod, callbackEach) {
+ each(pods, function (pod, callbackEach) {
pod.remove(callbackEach)
}, function (err) {
if (err) return callback(err)