diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 11:20:45 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 11:20:45 +0200 |
commit | 80a6c9e76fda57f01e37fe4620771ae70738a211 (patch) | |
tree | 7381a94285c11d954efca2be8103c8b86df3e832 /server/models | |
parent | 792b893ed414301c060391ed8a00368d68688236 (diff) | |
download | PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.tar.gz PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.tar.zst PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.zip |
Server: pod removing refractoring
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/pods.js | 14 | ||||
-rw-r--r-- | server/models/request.js | 49 | ||||
-rw-r--r-- | server/models/video.js | 6 |
3 files changed, 20 insertions, 49 deletions
diff --git a/server/models/pods.js b/server/models/pods.js index 4020a9603..6ab018c1c 100644 --- a/server/models/pods.js +++ b/server/models/pods.js | |||
@@ -1,11 +1,14 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
3 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
4 | const map = require('lodash/map') | 5 | const map = require('lodash/map') |
5 | const validator = require('express-validator').validator | 6 | const validator = require('express-validator').validator |
6 | 7 | ||
7 | const constants = require('../initializers/constants') | 8 | const constants = require('../initializers/constants') |
8 | 9 | ||
10 | const Video = mongoose.model('Video') | ||
11 | |||
9 | // --------------------------------------------------------------------------- | 12 | // --------------------------------------------------------------------------- |
10 | 13 | ||
11 | const PodSchema = mongoose.Schema({ | 14 | const PodSchema = mongoose.Schema({ |
@@ -51,6 +54,17 @@ PodSchema.pre('save', function (next) { | |||
51 | }) | 54 | }) |
52 | }) | 55 | }) |
53 | 56 | ||
57 | PodSchema.pre('remove', function (next) { | ||
58 | // Remove the videos owned by this pod too | ||
59 | Video.listByUrl(this.url, function (err, videos) { | ||
60 | if (err) return next(err) | ||
61 | |||
62 | each(videos, function (video, callbackEach) { | ||
63 | video.remove(callbackEach) | ||
64 | }, next) | ||
65 | }) | ||
66 | }) | ||
67 | |||
54 | const Pod = mongoose.model('Pod', PodSchema) | 68 | const Pod = mongoose.model('Pod', PodSchema) |
55 | 69 | ||
56 | // ------------------------------ METHODS ------------------------------ | 70 | // ------------------------------ METHODS ------------------------------ |
diff --git a/server/models/request.js b/server/models/request.js index 1a99dd2ea..f11c20b52 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -2,7 +2,6 @@ | |||
2 | 2 | ||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const eachLimit = require('async/eachLimit') | 4 | const eachLimit = require('async/eachLimit') |
5 | const map = require('lodash/map') | ||
6 | const mongoose = require('mongoose') | 5 | const mongoose = require('mongoose') |
7 | const waterfall = require('async/waterfall') | 6 | const waterfall = require('async/waterfall') |
8 | 7 | ||
@@ -11,7 +10,6 @@ const logger = require('../helpers/logger') | |||
11 | const requests = require('../helpers/requests') | 10 | const requests = require('../helpers/requests') |
12 | 11 | ||
13 | const Pod = mongoose.model('Pod') | 12 | const Pod = mongoose.model('Pod') |
14 | const Video = mongoose.model('Video') | ||
15 | 13 | ||
16 | let timer = null | 14 | let timer = null |
17 | let lastRequestTimestamp = 0 | 15 | let lastRequestTimestamp = 0 |
@@ -218,54 +216,13 @@ function removeBadPods () { | |||
218 | }) | 216 | }) |
219 | }, | 217 | }, |
220 | 218 | ||
221 | function listVideosOfTheseBadPods (pods, callback) { | 219 | function removeTheseBadPods (pods, callback) { |
222 | if (pods.length === 0) return callback(null) | 220 | if (pods.length === 0) return callback(null, 0) |
223 | |||
224 | const urls = map(pods, 'url') | ||
225 | |||
226 | Video.listByUrls(urls, function (err, videosList) { | ||
227 | if (err) { | ||
228 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) | ||
229 | return callback(null, pods, []) | ||
230 | } | ||
231 | |||
232 | return callback(null, pods, videosList) | ||
233 | }) | ||
234 | }, | ||
235 | |||
236 | function removeVideosOfTheseBadPods (pods, videosList, callback) { | ||
237 | // We don't have to remove pods, skip | ||
238 | if (typeof pods === 'function') { | ||
239 | callback = pods | ||
240 | return callback(null) | ||
241 | } | ||
242 | |||
243 | each(videosList, function (video, callbackEach) { | ||
244 | video.remove(callbackEach) | ||
245 | }, function (err) { | ||
246 | if (err) { | ||
247 | // Don't stop the process | ||
248 | logger.error('Error while removing videos of bad pods.', { error: err }) | ||
249 | return | ||
250 | } | ||
251 | |||
252 | return callback(null, pods) | ||
253 | }) | ||
254 | }, | ||
255 | |||
256 | function removeBadPodsFromDB (pods, callback) { | ||
257 | // We don't have to remove pods, skip | ||
258 | if (typeof pods === 'function') { | ||
259 | callback = pods | ||
260 | return callback(null) | ||
261 | } | ||
262 | 221 | ||
263 | each(pods, function (pod, callbackEach) { | 222 | each(pods, function (pod, callbackEach) { |
264 | pod.remove(callbackEach) | 223 | pod.remove(callbackEach) |
265 | }, function (err) { | 224 | }, function (err) { |
266 | if (err) return callback(err) | 225 | return callback(err, pods.length) |
267 | |||
268 | return callback(null, pods.length) | ||
269 | }) | 226 | }) |
270 | } | 227 | } |
271 | ], function (err, numberOfPodsRemoved) { | 228 | ], function (err, numberOfPodsRemoved) { |
diff --git a/server/models/video.js b/server/models/video.js index 4b941a40c..1feefe24f 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -57,7 +57,7 @@ VideoSchema.statics = { | |||
57 | getDurationFromFile, | 57 | getDurationFromFile, |
58 | listForApi, | 58 | listForApi, |
59 | listByUrlAndMagnet, | 59 | listByUrlAndMagnet, |
60 | listByUrls, | 60 | listByUrl, |
61 | listOwned, | 61 | listOwned, |
62 | listOwnedByAuthor, | 62 | listOwnedByAuthor, |
63 | listRemotes, | 63 | listRemotes, |
@@ -218,8 +218,8 @@ function listByUrlAndMagnet (fromUrl, magnetUri, callback) { | |||
218 | this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback) | 218 | this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback) |
219 | } | 219 | } |
220 | 220 | ||
221 | function listByUrls (fromUrls, callback) { | 221 | function listByUrl (fromUrl, callback) { |
222 | this.find({ podUrl: { $in: fromUrls } }, callback) | 222 | this.find({ podUrl: fromUrl }, callback) |
223 | } | 223 | } |
224 | 224 | ||
225 | function listOwned (callback) { | 225 | function listOwned (callback) { |