Server: fix remaining milli seconds before the next requests feature

pull/10/merge
Chocobozzz 2016-10-01 15:33:27 +02:00
parent 1ab844d859
commit 5abeec313f
2 changed files with 19 additions and 6 deletions

View File

@ -28,11 +28,9 @@ function getStatsRequests (req, res, next) {
Request.list(function (err, requests) { Request.list(function (err, requests) {
if (err) return next(err) if (err) return next(err)
const remainingMilliSeconds = constants.REQUESTS_INTERVAL - (Date.now() % constants.REQUESTS_INTERVAL)
return res.json({ return res.json({
requests: requests, requests: requests,
remainingMilliSeconds: remainingMilliSeconds, remainingMilliSeconds: Request.remainingMilliSeconds(),
milliSecondsInterval: constants.REQUESTS_INTERVAL milliSecondsInterval: constants.REQUESTS_INTERVAL
}) })
}) })

View File

@ -14,6 +14,7 @@ const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
let timer = null let timer = null
let lastRequestTimestamp = 0
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -27,7 +28,8 @@ RequestSchema.statics = {
deactivate, deactivate,
flush, flush,
forceSend, forceSend,
list list,
remainingMilliSeconds
} }
RequestSchema.pre('save', function (next) { RequestSchema.pre('save', function (next) {
@ -54,12 +56,19 @@ mongoose.model('Request', RequestSchema)
function activate () { function activate () {
logger.info('Requests scheduler activated.') logger.info('Requests scheduler activated.')
timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL) lastRequestTimestamp = Date.now()
const self = this
timer = setInterval(function () {
lastRequestTimestamp = Date.now()
makeRequests.call(self)
}, constants.REQUESTS_INTERVAL)
} }
function deactivate () { function deactivate () {
logger.info('Requests scheduler deactivated.') logger.info('Requests scheduler deactivated.')
clearInterval(timer) clearInterval(timer)
timer = null
} }
function flush () { function flush () {
@ -77,6 +86,12 @@ function list (callback) {
this.find({ }, 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 // Make a requests to friends of a certain type
@ -159,7 +174,7 @@ function makeRequests () {
return callbackEach() 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) { if (!toPod) {
removePodOf.call(self, requestToMake.ids, toPodId) removePodOf.call(self, requestToMake.ids, toPodId)
return callbackEach() return callbackEach()