diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/request.js | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/server/models/request.js b/server/models/request.js index 73b17dc8c..2d50d94e0 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -14,6 +14,7 @@ const Pod = mongoose.model('Pod') | |||
14 | const Video = mongoose.model('Video') | 14 | const Video = mongoose.model('Video') |
15 | 15 | ||
16 | let timer = null | 16 | let timer = null |
17 | let lastRequestTimestamp = 0 | ||
17 | 18 | ||
18 | // --------------------------------------------------------------------------- | 19 | // --------------------------------------------------------------------------- |
19 | 20 | ||
@@ -27,7 +28,8 @@ RequestSchema.statics = { | |||
27 | deactivate, | 28 | deactivate, |
28 | flush, | 29 | flush, |
29 | forceSend, | 30 | forceSend, |
30 | list | 31 | list, |
32 | remainingMilliSeconds | ||
31 | } | 33 | } |
32 | 34 | ||
33 | RequestSchema.pre('save', function (next) { | 35 | RequestSchema.pre('save', function (next) { |
@@ -54,12 +56,19 @@ mongoose.model('Request', RequestSchema) | |||
54 | 56 | ||
55 | function activate () { | 57 | function activate () { |
56 | logger.info('Requests scheduler activated.') | 58 | logger.info('Requests scheduler activated.') |
57 | timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL) | 59 | lastRequestTimestamp = Date.now() |
60 | |||
61 | const self = this | ||
62 | timer = setInterval(function () { | ||
63 | lastRequestTimestamp = Date.now() | ||
64 | makeRequests.call(self) | ||
65 | }, constants.REQUESTS_INTERVAL) | ||
58 | } | 66 | } |
59 | 67 | ||
60 | function deactivate () { | 68 | function deactivate () { |
61 | logger.info('Requests scheduler deactivated.') | 69 | logger.info('Requests scheduler deactivated.') |
62 | clearInterval(timer) | 70 | clearInterval(timer) |
71 | timer = null | ||
63 | } | 72 | } |
64 | 73 | ||
65 | function flush () { | 74 | function flush () { |
@@ -77,6 +86,12 @@ function list (callback) { | |||
77 | this.find({ }, callback) | 86 | this.find({ }, callback) |
78 | } | 87 | } |
79 | 88 | ||
89 | function remainingMilliSeconds () { | ||
90 | if (timer === null) return -1 | ||
91 | |||
92 | return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp) | ||
93 | } | ||
94 | |||
80 | // --------------------------------------------------------------------------- | 95 | // --------------------------------------------------------------------------- |
81 | 96 | ||
82 | // Make a requests to friends of a certain type | 97 | // Make a requests to friends of a certain type |
@@ -159,7 +174,7 @@ function makeRequests () { | |||
159 | return callbackEach() | 174 | return callbackEach() |
160 | } | 175 | } |
161 | 176 | ||
162 | // Maybe the pod is not our friend anymore so simply remove them | 177 | // Maybe the pod is not our friend anymore so simply remove it |
163 | if (!toPod) { | 178 | if (!toPod) { |
164 | removePodOf.call(self, requestToMake.ids, toPodId) | 179 | removePodOf.call(self, requestToMake.ids, toPodId) |
165 | return callbackEach() | 180 | return callbackEach() |