aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod/pod.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/models/pod/pod.ts
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/models/pod/pod.ts')
-rw-r--r--server/models/pod/pod.ts115
1 files changed, 46 insertions, 69 deletions
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 @@
1import { each, waterfall } from 'async'
2import { map } from 'lodash' 1import { map } from 'lodash'
3import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
4 3
@@ -7,7 +6,6 @@ import { logger, isHostValid } from '../../helpers'
7 6
8import { addMethodsToModel } from '../utils' 7import { addMethodsToModel } from '../utils'
9import { 8import {
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
121countAll = function (callback: PodMethods.CountAllCallback) { 119countAll = function () {
122 return Pod.count().asCallback(callback) 120 return Pod.count()
123} 121}
124 122
125incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) { 123incrementScores = 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
145list = function (callback: PodMethods.ListCallback) { 141list = function () {
146 return Pod.findAll().asCallback(callback) 142 return Pod.findAll()
147} 143}
148 144
149listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) { 145listAllIds = 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
163listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) { 156listRandomPodIdsWithRequest = 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
197listBadPods = function (callback: PodMethods.ListBadPodsCallback) { 186listBadPods = 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
207load = function (id: number, callback: PodMethods.LoadCallback) { 196load = function (id: number) {
208 return Pod.findById(id).asCallback(callback) 197 return Pod.findById(id)
209} 198}
210 199
211loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) { 200loadByHost = 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
221removeAll = function (callback: PodMethods.RemoveAllCallback) { 210removeAll = function () {
222 return Pod.destroy().asCallback(callback) 211 return Pod.destroy()
223} 212}
224 213
225updatePodsScore = function (goodPods: number[], badPods: number[]) { 214updatePodsScore = 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)
245function removeBadPods () { 235function 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}