diff options
Diffstat (limited to 'server/models/request.js')
-rw-r--r-- | server/models/request.js | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/server/models/request.js b/server/models/request.js index 4d521919a..2d1c5af15 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -14,19 +14,22 @@ 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 | ||
20 | const RequestSchema = mongoose.Schema({ | 21 | const RequestSchema = mongoose.Schema({ |
21 | request: mongoose.Schema.Types.Mixed, | 22 | request: mongoose.Schema.Types.Mixed, |
22 | to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ] | 23 | to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ] |
23 | }) | 24 | }) |
24 | 25 | ||
25 | RequestSchema.statics = { | 26 | RequestSchema.statics = { |
26 | activate, | 27 | activate, |
27 | deactivate, | 28 | deactivate, |
28 | flush, | 29 | flush, |
29 | forceSend | 30 | forceSend, |
31 | list, | ||
32 | remainingMilliSeconds | ||
30 | } | 33 | } |
31 | 34 | ||
32 | RequestSchema.pre('save', function (next) { | 35 | RequestSchema.pre('save', function (next) { |
@@ -53,12 +56,19 @@ mongoose.model('Request', RequestSchema) | |||
53 | 56 | ||
54 | function activate () { | 57 | function activate () { |
55 | logger.info('Requests scheduler activated.') | 58 | logger.info('Requests scheduler activated.') |
56 | timer = setInterval(makeRequests.bind(this), constants.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) | ||
57 | } | 66 | } |
58 | 67 | ||
59 | function deactivate () { | 68 | function deactivate () { |
60 | logger.info('Requests scheduler deactivated.') | 69 | logger.info('Requests scheduler deactivated.') |
61 | clearInterval(timer) | 70 | clearInterval(timer) |
71 | timer = null | ||
62 | } | 72 | } |
63 | 73 | ||
64 | function flush () { | 74 | function flush () { |
@@ -72,6 +82,16 @@ function forceSend () { | |||
72 | makeRequests.call(this) | 82 | makeRequests.call(this) |
73 | } | 83 | } |
74 | 84 | ||
85 | function list (callback) { | ||
86 | this.find({ }, callback) | ||
87 | } | ||
88 | |||
89 | function remainingMilliSeconds () { | ||
90 | if (timer === null) return -1 | ||
91 | |||
92 | return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp) | ||
93 | } | ||
94 | |||
75 | // --------------------------------------------------------------------------- | 95 | // --------------------------------------------------------------------------- |
76 | 96 | ||
77 | // Make a requests to friends of a certain type | 97 | // Make a requests to friends of a certain type |
@@ -91,7 +111,13 @@ function makeRequest (toPod, requestsToMake, callback) { | |||
91 | // The function fire some useful callbacks | 111 | // The function fire some useful callbacks |
92 | requests.makeSecureRequest(params, function (err, res) { | 112 | requests.makeSecureRequest(params, function (err, res) { |
93 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | 113 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { |
94 | logger.error('Error sending secure request to %s pod.', toPod.url, { error: err || new Error('Status code not 20x') }) | 114 | logger.error( |
115 | 'Error sending secure request to %s pod.', | ||
116 | toPod.url, | ||
117 | { | ||
118 | error: err || new Error('Status code not 20x : ' + res.statusCode) | ||
119 | } | ||
120 | ) | ||
95 | 121 | ||
96 | return callback(false) | 122 | return callback(false) |
97 | } | 123 | } |
@@ -148,19 +174,14 @@ function makeRequests () { | |||
148 | return callbackEach() | 174 | return callbackEach() |
149 | } | 175 | } |
150 | 176 | ||
151 | // 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 |
152 | if (!toPod) { | 178 | if (!toPod) { |
179 | logger.info('Removing %d requests of unexisting pod %s.', requestToMake.ids.length, toPodId) | ||
153 | removePodOf.call(self, requestToMake.ids, toPodId) | 180 | removePodOf.call(self, requestToMake.ids, toPodId) |
154 | return callbackEach() | 181 | return callbackEach() |
155 | } | 182 | } |
156 | 183 | ||
157 | makeRequest(toPod, requestToMake.datas, function (success) { | 184 | makeRequest(toPod, requestToMake.datas, function (success) { |
158 | if (err) { | ||
159 | logger.error('Errors when sent request to %s.', toPod.url, { error: err }) | ||
160 | // Do not stop the process just for one error | ||
161 | return callbackEach() | ||
162 | } | ||
163 | |||
164 | if (success === true) { | 185 | if (success === true) { |
165 | logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) | 186 | logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) |
166 | 187 | ||