diff options
Diffstat (limited to 'server/models/pod')
-rw-r--r-- | server/models/pod/pod-interface.ts | 28 | ||||
-rw-r--r-- | server/models/pod/pod.ts | 115 |
2 files changed, 56 insertions, 87 deletions
diff --git a/server/models/pod/pod-interface.ts b/server/models/pod/pod-interface.ts index d88847c45..f6963d47e 100644 --- a/server/models/pod/pod-interface.ts +++ b/server/models/pod/pod-interface.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import * as Promise from 'bluebird' | ||
2 | 3 | ||
3 | // Don't use barrel, import just what we need | 4 | // Don't use barrel, import just what we need |
4 | import { Pod as FormatedPod } from '../../../shared/models/pod.model' | 5 | import { Pod as FormatedPod } from '../../../shared/models/pod.model' |
@@ -6,32 +7,23 @@ import { Pod as FormatedPod } from '../../../shared/models/pod.model' | |||
6 | export namespace PodMethods { | 7 | export namespace PodMethods { |
7 | export type ToFormatedJSON = (this: PodInstance) => FormatedPod | 8 | export type ToFormatedJSON = (this: PodInstance) => FormatedPod |
8 | 9 | ||
9 | export type CountAllCallback = (err: Error, total: number) => void | 10 | export type CountAll = () => Promise<number> |
10 | export type CountAll = (callback) => void | ||
11 | 11 | ||
12 | export type IncrementScoresCallback = (err: Error) => void | 12 | export type IncrementScores = (ids: number[], value: number) => Promise<[ number, PodInstance[] ]> |
13 | export type IncrementScores = (ids: number[], value: number, callback?: IncrementScoresCallback) => void | ||
14 | 13 | ||
15 | export type ListCallback = (err: Error, podInstances?: PodInstance[]) => void | 14 | export type List = () => Promise<PodInstance[]> |
16 | export type List = (callback: ListCallback) => void | ||
17 | 15 | ||
18 | export type ListAllIdsCallback = (err: Error, ids?: number[]) => void | 16 | export type ListAllIds = (transaction: Sequelize.Transaction) => Promise<number[]> |
19 | export type ListAllIds = (transaction: Sequelize.Transaction, callback: ListAllIdsCallback) => void | ||
20 | 17 | ||
21 | export type ListRandomPodIdsWithRequestCallback = (err: Error, podInstanceIds?: number[]) => void | 18 | export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string) => Promise<number[]> |
22 | export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: ListRandomPodIdsWithRequestCallback) => void | ||
23 | 19 | ||
24 | export type ListBadPodsCallback = (err: Error, podInstances?: PodInstance[]) => void | 20 | export type ListBadPods = () => Promise<PodInstance[]> |
25 | export type ListBadPods = (callback: ListBadPodsCallback) => void | ||
26 | 21 | ||
27 | export type LoadCallback = (err: Error, podInstance: PodInstance) => void | 22 | export type Load = (id: number) => Promise<PodInstance> |
28 | export type Load = (id: number, callback: LoadCallback) => void | ||
29 | 23 | ||
30 | export type LoadByHostCallback = (err: Error, podInstance: PodInstance) => void | 24 | export type LoadByHost = (host: string) => Promise<PodInstance> |
31 | export type LoadByHost = (host: string, callback: LoadByHostCallback) => void | ||
32 | 25 | ||
33 | export type RemoveAllCallback = (err: Error) => void | 26 | export type RemoveAll = () => Promise<number> |
34 | export type RemoveAll = (callback: RemoveAllCallback) => void | ||
35 | 27 | ||
36 | export type UpdatePodsScore = (goodPods: number[], badPods: number[]) => void | 28 | export type UpdatePodsScore = (goodPods: number[], badPods: number[]) => void |
37 | } | 29 | } |
diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts index 4fe7fda1c..9209380fc 100644 --- a/server/models/pod/pod.ts +++ b/server/models/pod/pod.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import { each, waterfall } from 'async' | ||
2 | import { map } from 'lodash' | 1 | import { map } from 'lodash' |
3 | import * as Sequelize from 'sequelize' | 2 | import * as Sequelize from 'sequelize' |
4 | 3 | ||
@@ -7,7 +6,6 @@ import { logger, isHostValid } from '../../helpers' | |||
7 | 6 | ||
8 | import { addMethodsToModel } from '../utils' | 7 | import { addMethodsToModel } from '../utils' |
9 | import { | 8 | import { |
10 | PodClass, | ||
11 | PodInstance, | 9 | PodInstance, |
12 | PodAttributes, | 10 | PodAttributes, |
13 | 11 | ||
@@ -118,13 +116,11 @@ function associate (models) { | |||
118 | }) | 116 | }) |
119 | } | 117 | } |
120 | 118 | ||
121 | countAll = function (callback: PodMethods.CountAllCallback) { | 119 | countAll = function () { |
122 | return Pod.count().asCallback(callback) | 120 | return Pod.count() |
123 | } | 121 | } |
124 | 122 | ||
125 | incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) { | 123 | incrementScores = function (ids: number[], value: number) { |
126 | if (!callback) callback = function () { /* empty */ } | ||
127 | |||
128 | const update = { | 124 | const update = { |
129 | score: Sequelize.literal('score +' + value) | 125 | score: Sequelize.literal('score +' + value) |
130 | } | 126 | } |
@@ -139,33 +135,28 @@ incrementScores = function (ids: number[], value: number, callback?: PodMethods. | |||
139 | validate: false | 135 | validate: false |
140 | } | 136 | } |
141 | 137 | ||
142 | return Pod.update(update, options).asCallback(callback) | 138 | return Pod.update(update, options) |
143 | } | 139 | } |
144 | 140 | ||
145 | list = function (callback: PodMethods.ListCallback) { | 141 | list = function () { |
146 | return Pod.findAll().asCallback(callback) | 142 | return Pod.findAll() |
147 | } | 143 | } |
148 | 144 | ||
149 | listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) { | 145 | listAllIds = function (transaction: Sequelize.Transaction) { |
150 | const query: any = { | 146 | const query: Sequelize.FindOptions = { |
151 | attributes: [ 'id' ] | 147 | attributes: [ 'id' ], |
148 | transaction | ||
152 | } | 149 | } |
153 | 150 | ||
154 | if (transaction !== null) query.transaction = transaction | 151 | return Pod.findAll(query).then(pods => { |
155 | 152 | return map(pods, 'id') | |
156 | return Pod.findAll(query).asCallback(function (err: Error, pods) { | ||
157 | if (err) return callback(err) | ||
158 | |||
159 | return callback(null, map(pods, 'id')) | ||
160 | }) | 153 | }) |
161 | } | 154 | } |
162 | 155 | ||
163 | listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) { | 156 | listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) { |
164 | Pod.count().asCallback(function (err, count) { | 157 | return Pod.count().then(count => { |
165 | if (err) return callback(err) | ||
166 | |||
167 | // Optimization... | 158 | // Optimization... |
168 | if (count === 0) return callback(null, []) | 159 | if (count === 0) return [] |
169 | 160 | ||
170 | let start = Math.floor(Math.random() * count) - limit | 161 | let start = Math.floor(Math.random() * count) - limit |
171 | if (start < 0) start = 0 | 162 | if (start < 0) start = 0 |
@@ -186,56 +177,55 @@ listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, ta | |||
186 | } | 177 | } |
187 | } | 178 | } |
188 | 179 | ||
189 | return Pod.findAll(query).asCallback(function (err, pods) { | 180 | return Pod.findAll(query).then(pods => { |
190 | if (err) return callback(err) | 181 | return map(pods, 'id') |
191 | |||
192 | return callback(null, map(pods, 'id')) | ||
193 | }) | 182 | }) |
194 | }) | 183 | }) |
195 | } | 184 | } |
196 | 185 | ||
197 | listBadPods = function (callback: PodMethods.ListBadPodsCallback) { | 186 | listBadPods = function () { |
198 | const query = { | 187 | const query = { |
199 | where: { | 188 | where: { |
200 | score: { $lte: 0 } | 189 | score: { $lte: 0 } |
201 | } | 190 | } |
202 | } | 191 | } |
203 | 192 | ||
204 | return Pod.findAll(query).asCallback(callback) | 193 | return Pod.findAll(query) |
205 | } | 194 | } |
206 | 195 | ||
207 | load = function (id: number, callback: PodMethods.LoadCallback) { | 196 | load = function (id: number) { |
208 | return Pod.findById(id).asCallback(callback) | 197 | return Pod.findById(id) |
209 | } | 198 | } |
210 | 199 | ||
211 | loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) { | 200 | loadByHost = function (host: string) { |
212 | const query = { | 201 | const query = { |
213 | where: { | 202 | where: { |
214 | host: host | 203 | host: host |
215 | } | 204 | } |
216 | } | 205 | } |
217 | 206 | ||
218 | return Pod.findOne(query).asCallback(callback) | 207 | return Pod.findOne(query) |
219 | } | 208 | } |
220 | 209 | ||
221 | removeAll = function (callback: PodMethods.RemoveAllCallback) { | 210 | removeAll = function () { |
222 | return Pod.destroy().asCallback(callback) | 211 | return Pod.destroy() |
223 | } | 212 | } |
224 | 213 | ||
225 | updatePodsScore = function (goodPods: number[], badPods: number[]) { | 214 | updatePodsScore = function (goodPods: number[], badPods: number[]) { |
226 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | 215 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) |
227 | 216 | ||
228 | if (goodPods.length !== 0) { | 217 | if (goodPods.length !== 0) { |
229 | incrementScores(goodPods, PODS_SCORE.BONUS, function (err) { | 218 | incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => { |
230 | if (err) logger.error('Cannot increment scores of good pods.', { error: err }) | 219 | logger.error('Cannot increment scores of good pods.', { error: err }) |
231 | }) | 220 | }) |
232 | } | 221 | } |
233 | 222 | ||
234 | if (badPods.length !== 0) { | 223 | if (badPods.length !== 0) { |
235 | incrementScores(badPods, PODS_SCORE.MALUS, function (err) { | 224 | incrementScores(badPods, PODS_SCORE.MALUS) |
236 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) | 225 | .then(() => removeBadPods()) |
237 | removeBadPods() | 226 | .catch(err => { |
238 | }) | 227 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) |
228 | }) | ||
239 | } | 229 | } |
240 | } | 230 | } |
241 | 231 | ||
@@ -243,32 +233,19 @@ updatePodsScore = function (goodPods: number[], badPods: number[]) { | |||
243 | 233 | ||
244 | // Remove pods with a score of 0 (too many requests where they were unreachable) | 234 | // Remove pods with a score of 0 (too many requests where they were unreachable) |
245 | function removeBadPods () { | 235 | function removeBadPods () { |
246 | waterfall([ | 236 | return listBadPods() |
247 | function findBadPods (callback) { | 237 | .then(pods => { |
248 | listBadPods(function (err, pods) { | 238 | const podsRemovePromises = pods.map(pod => pod.destroy()) |
249 | if (err) { | 239 | return Promise.all(podsRemovePromises).then(() => pods.length) |
250 | logger.error('Cannot find bad pods.', { error: err }) | 240 | }) |
251 | return callback(err) | 241 | .then(numberOfPodsRemoved => { |
252 | } | 242 | if (numberOfPodsRemoved) { |
253 | 243 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
254 | return callback(null, pods) | 244 | } else { |
255 | }) | 245 | logger.info('No need to remove bad pods.') |
256 | }, | 246 | } |
257 | 247 | }) | |
258 | function removeTheseBadPods (pods, callback) { | 248 | .catch(err => { |
259 | each(pods, function (pod: any, callbackEach) { | ||
260 | pod.destroy().asCallback(callbackEach) | ||
261 | }, function (err) { | ||
262 | return callback(err, pods.length) | ||
263 | }) | ||
264 | } | ||
265 | ], function (err, numberOfPodsRemoved) { | ||
266 | if (err) { | ||
267 | logger.error('Cannot remove bad pods.', { error: err }) | 249 | logger.error('Cannot remove bad pods.', { error: err }) |
268 | } else if (numberOfPodsRemoved) { | 250 | }) |
269 | logger.info('Removed %d pods.', numberOfPodsRemoved) | ||
270 | } else { | ||
271 | logger.info('No need to remove bad pods.') | ||
272 | } | ||
273 | }) | ||
274 | } | 251 | } |