diff options
Diffstat (limited to 'server/models/pod.js')
-rw-r--r-- | server/models/pod.js | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/server/models/pod.js b/server/models/pod.js index 79afb737a..14814708e 100644 --- a/server/models/pod.js +++ b/server/models/pod.js | |||
@@ -1,8 +1,11 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
3 | const map = require('lodash/map') | 4 | const map = require('lodash/map') |
5 | const waterfall = require('async/waterfall') | ||
4 | 6 | ||
5 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
8 | const logger = require('../helpers/logger') | ||
6 | const customPodsValidators = require('../helpers/custom-validators').pods | 9 | const customPodsValidators = require('../helpers/custom-validators').pods |
7 | 10 | ||
8 | // --------------------------------------------------------------------------- | 11 | // --------------------------------------------------------------------------- |
@@ -62,6 +65,7 @@ module.exports = function (sequelize, DataTypes) { | |||
62 | listBadPods, | 65 | listBadPods, |
63 | load, | 66 | load, |
64 | loadByHost, | 67 | loadByHost, |
68 | updatePodsScore, | ||
65 | removeAll | 69 | removeAll |
66 | }, | 70 | }, |
67 | instanceMethods: { | 71 | instanceMethods: { |
@@ -144,7 +148,7 @@ function listAllIds (transaction, callback) { | |||
144 | }) | 148 | }) |
145 | } | 149 | } |
146 | 150 | ||
147 | function listRandomPodIdsWithRequest (limit, callback) { | 151 | function listRandomPodIdsWithRequest (limit, tableRequestPod, callback) { |
148 | const self = this | 152 | const self = this |
149 | 153 | ||
150 | self.count().asCallback(function (err, count) { | 154 | self.count().asCallback(function (err, count) { |
@@ -166,7 +170,7 @@ function listRandomPodIdsWithRequest (limit, callback) { | |||
166 | where: { | 170 | where: { |
167 | id: { | 171 | id: { |
168 | $in: [ | 172 | $in: [ |
169 | this.sequelize.literal('SELECT "podId" FROM "RequestToPods"') | 173 | this.sequelize.literal('SELECT "podId" FROM "' + tableRequestPod + '"') |
170 | ] | 174 | ] |
171 | } | 175 | } |
172 | } | 176 | } |
@@ -207,3 +211,58 @@ function loadByHost (host, callback) { | |||
207 | function removeAll (callback) { | 211 | function removeAll (callback) { |
208 | return this.destroy().asCallback(callback) | 212 | return this.destroy().asCallback(callback) |
209 | } | 213 | } |
214 | |||
215 | function updatePodsScore (goodPods, badPods) { | ||
216 | const self = this | ||
217 | |||
218 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | ||
219 | |||
220 | if (goodPods.length !== 0) { | ||
221 | this.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { | ||
222 | if (err) logger.error('Cannot increment scores of good pods.', { error: err }) | ||
223 | }) | ||
224 | } | ||
225 | |||
226 | if (badPods.length !== 0) { | ||
227 | this.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { | ||
228 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) | ||
229 | removeBadPods.call(self) | ||
230 | }) | ||
231 | } | ||
232 | } | ||
233 | |||
234 | // --------------------------------------------------------------------------- | ||
235 | |||
236 | // Remove pods with a score of 0 (too many requests where they were unreachable) | ||
237 | function removeBadPods () { | ||
238 | const self = this | ||
239 | |||
240 | waterfall([ | ||
241 | function findBadPods (callback) { | ||
242 | self.sequelize.models.Pod.listBadPods(function (err, pods) { | ||
243 | if (err) { | ||
244 | logger.error('Cannot find bad pods.', { error: err }) | ||
245 | return callback(err) | ||
246 | } | ||
247 | |||
248 | return callback(null, pods) | ||
249 | }) | ||
250 | }, | ||
251 | |||
252 | function removeTheseBadPods (pods, callback) { | ||
253 | each(pods, function (pod, callbackEach) { | ||
254 | pod.destroy().asCallback(callbackEach) | ||
255 | }, function (err) { | ||
256 | return callback(err, pods.length) | ||
257 | }) | ||
258 | } | ||
259 | ], function (err, numberOfPodsRemoved) { | ||
260 | if (err) { | ||
261 | logger.error('Cannot remove bad pods.', { error: err }) | ||
262 | } else if (numberOfPodsRemoved) { | ||
263 | logger.info('Removed %d pods.', numberOfPodsRemoved) | ||
264 | } else { | ||
265 | logger.info('No need to remove bad pods.') | ||
266 | } | ||
267 | }) | ||
268 | } | ||