aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/poolRequests.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-04 21:10:33 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-04 21:10:33 +0100
commitc173e56520b0fe4206b9ea8049b6add40bfeabcd (patch)
tree264c6cbf1bf81a6522685b4be5771bbeef4cd5dc /lib/poolRequests.js
parentc45f7f84001c2731909db04dd82e1c1f290386eb (diff)
downloadPeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.gz
PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.zst
PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.zip
Split models
Diffstat (limited to 'lib/poolRequests.js')
-rw-r--r--lib/poolRequests.js61
1 files changed, 12 insertions, 49 deletions
diff --git a/lib/poolRequests.js b/lib/poolRequests.js
index 53f47d629..796f06149 100644
--- a/lib/poolRequests.js
+++ b/lib/poolRequests.js
@@ -5,18 +5,16 @@
5 var pluck = require('lodash-node/compat/collection/pluck') 5 var pluck = require('lodash-node/compat/collection/pluck')
6 6
7 var constants = require('../initializers/constants') 7 var constants = require('../initializers/constants')
8 var database = require('../initializers/database')
9 var logger = require('../helpers/logger') 8 var logger = require('../helpers/logger')
10 var PodsDB = database.PodsDB 9 var Pods = require('../models/pods')
11 var PoolRequestsDB = database.PoolRequestsDB 10 var PoolRequests = require('../models/poolRequests')
12 var utils = require('../helpers/utils') 11 var utils = require('../helpers/utils')
13 var VideosDB = database.VideosDB 12 var Videos = require('../models/videos')
14 13
15 var timer = null 14 var timer = null
16 15
17 var poolRequests = { 16 var poolRequests = {
18 activate: activate, 17 activate: activate,
19 addToPoolRequests: addToPoolRequests,
20 deactivate: deactivate, 18 deactivate: deactivate,
21 forceSend: forceSend 19 forceSend: forceSend
22 } 20 }
@@ -36,30 +34,6 @@
36 timer = setInterval(makePoolRequests, constants.INTERVAL) 34 timer = setInterval(makePoolRequests, constants.INTERVAL)
37 } 35 }
38 36
39 function addToPoolRequests (id, type, request) {
40 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
41
42 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
43 if (err) logger.error(err)
44
45 if (entity) {
46 if (entity.type === type) {
47 logger.error(new Error('Cannot insert two same requests.'))
48 return
49 }
50
51 // Remove the request of the other type
52 PoolRequestsDB.remove({ id: id }, function (err) {
53 if (err) logger.error(err)
54 })
55 } else {
56 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
57 if (err) logger.error(err)
58 })
59 }
60 })
61 }
62
63 // --------------------------------------------------------------------------- 37 // ---------------------------------------------------------------------------
64 38
65 module.exports = poolRequests 39 module.exports = poolRequests
@@ -69,7 +43,7 @@
69 function makePoolRequest (type, requests, callback) { 43 function makePoolRequest (type, requests, callback) {
70 if (!callback) callback = function () {} 44 if (!callback) callback = function () {}
71 45
72 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { 46 Pods.list(function (err, pods) {
73 if (err) throw err 47 if (err) throw err
74 48
75 var params = { 49 var params = {
@@ -116,7 +90,7 @@
116 function makePoolRequests () { 90 function makePoolRequests () {
117 logger.info('Making pool requests to friends.') 91 logger.info('Making pool requests to friends.')
118 92
119 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) { 93 PoolRequests.list(function (err, pool_requests) {
120 if (err) throw err 94 if (err) throw err
121 95
122 if (pool_requests.length === 0) return 96 if (pool_requests.length === 0) return
@@ -150,7 +124,7 @@
150 makePoolRequest('add', requests.add.requests, function (err) { 124 makePoolRequest('add', requests.add.requests, function (err) {
151 if (err) logger.error('Errors when sent add pool requests.', { error: err }) 125 if (err) logger.error('Errors when sent add pool requests.', { error: err })
152 126
153 removePoolRequestsFromDB(requests.add.ids) 127 PoolRequests.removeRequests(requests.add.ids)
154 }) 128 })
155 } 129 }
156 130
@@ -159,7 +133,7 @@
159 makePoolRequest('remove', requests.remove.requests, function (err) { 133 makePoolRequest('remove', requests.remove.requests, function (err) {
160 if (err) logger.error('Errors when sent remove pool requests.', { error: err }) 134 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
161 135
162 removePoolRequestsFromDB(requests.remove.ids) 136 PoolRequests.removeRequests(requests.remove.ids)
163 }) 137 })
164 } 138 }
165 }) 139 })
@@ -167,7 +141,7 @@
167 } 141 }
168 142
169 function removeBadPods () { 143 function removeBadPods () {
170 PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { 144 Pods.findBadPods(function (err, pods) {
171 if (err) throw err 145 if (err) throw err
172 146
173 if (pods.length === 0) return 147 if (pods.length === 0) return
@@ -175,12 +149,12 @@
175 var urls = pluck(pods, 'url') 149 var urls = pluck(pods, 'url')
176 var ids = pluck(pods, '_id') 150 var ids = pluck(pods, '_id')
177 151
178 VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) { 152 Videos.removeAllRemotesOf(urls, function (err, r) {
179 if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) 153 if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err })
180 var videos_removed = r.result.n 154 var videos_removed = r.result.n
181 logger.info('Removed %d videos.', videos_removed) 155 logger.info('Removed %d videos.', videos_removed)
182 156
183 PodsDB.remove({ _id: { $in: ids } }, function (err, r) { 157 Pods.removeAllByIds(ids, function (err, r) {
184 if (err) logger.error('Cannot remove bad pods.', { error: err }) 158 if (err) logger.error('Cannot remove bad pods.', { error: err })
185 159
186 var pods_removed = r.result.n 160 var pods_removed = r.result.n
@@ -190,22 +164,11 @@
190 }) 164 })
191 } 165 }
192 166
193 function removePoolRequestsFromDB (ids) {
194 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
195 if (err) {
196 logger.error('Cannot remove requests from the pool requests database.', { error: err })
197 return
198 }
199
200 logger.info('Pool requests flushed.')
201 })
202 }
203
204 function updatePodsScore (good_pods, bad_pods) { 167 function updatePodsScore (good_pods, bad_pods) {
205 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) 168 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
206 169
207 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() 170 Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS)
208 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { 171 Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
209 if (err) throw err 172 if (err) throw err
210 removeBadPods() 173 removeBadPods()
211 }) 174 })