aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server.js2
-rw-r--r--server/lib/friends.js18
-rw-r--r--server/lib/requestsScheduler.js (renamed from server/lib/poolRequests.js)56
-rw-r--r--server/models/requests.js (renamed from server/models/poolRequests.js)20
4 files changed, 48 insertions, 48 deletions
diff --git a/server.js b/server.js
index 9772ce92b..204cc146f 100644
--- a/server.js
+++ b/server.js
@@ -28,7 +28,7 @@ const customValidators = require('./server/helpers/customValidators')
28const database = require('./server/initializers/database') 28const database = require('./server/initializers/database')
29const installer = require('./server/initializers/installer') 29const installer = require('./server/initializers/installer')
30const logger = require('./server/helpers/logger') 30const logger = require('./server/helpers/logger')
31const poolRequests = require('./server/lib/poolRequests') 31const poolRequests = require('./server/lib/requestsScheduler')
32const routes = require('./server/controllers') 32const routes = require('./server/controllers')
33const utils = require('./server/helpers/utils') 33const utils = require('./server/helpers/utils')
34const videos = require('./server/lib/videos') 34const videos = require('./server/lib/videos')
diff --git a/server/lib/friends.js b/server/lib/friends.js
index c3c231604..9a2c5c06e 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -9,7 +9,7 @@ const constants = require('../initializers/constants')
9const logger = require('../helpers/logger') 9const logger = require('../helpers/logger')
10const peertubeCrypto = require('../helpers/peertubeCrypto') 10const peertubeCrypto = require('../helpers/peertubeCrypto')
11const Pods = require('../models/pods') 11const Pods = require('../models/pods')
12const poolRequests = require('../lib/poolRequests') 12const requestsScheduler = require('../lib/requestsScheduler')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14const Videos = require('../models/videos') 14const Videos = require('../models/videos')
15 15
@@ -30,7 +30,7 @@ function addVideoToFriends (video) {
30 const id = video.name + video.magnetUri 30 const id = video.name + video.magnetUri
31 // ensure namePath is null 31 // ensure namePath is null
32 video.namePath = null 32 video.namePath = null
33 poolRequests.addRequest(id, 'add', video) 33 requestsScheduler.addRequest(id, 'add', video)
34} 34}
35 35
36function hasFriends (callback) { 36function hasFriends (callback) {
@@ -70,9 +70,9 @@ function makeFriends (callback) {
70 70
71function quitFriends (callback) { 71function quitFriends (callback) {
72 // Stop pool requests 72 // Stop pool requests
73 poolRequests.deactivate() 73 requestsScheduler.deactivate()
74 // Flush pool requests 74 // Flush pool requests
75 poolRequests.forceSend() 75 requestsScheduler.forceSend()
76 76
77 Pods.list(function (err, pods) { 77 Pods.list(function (err, pods) {
78 if (err) return callback(err) 78 if (err) return callback(err)
@@ -90,7 +90,7 @@ function quitFriends (callback) {
90 // Announce we quit them 90 // Announce we quit them
91 requests.makeMultipleRetryRequest(request, pods, function () { 91 requests.makeMultipleRetryRequest(request, pods, function () {
92 Pods.removeAll(function (err) { 92 Pods.removeAll(function (err) {
93 poolRequests.activate() 93 requestsScheduler.activate()
94 94
95 if (err) return callback(err) 95 if (err) return callback(err)
96 96
@@ -110,7 +110,7 @@ function quitFriends (callback) {
110function removeVideoToFriends (video) { 110function removeVideoToFriends (video) {
111 // To avoid duplicates 111 // To avoid duplicates
112 const id = video.name + video.magnetUri 112 const id = video.name + video.magnetUri
113 poolRequests.addRequest(id, 'remove', video) 113 requestsScheduler.addRequest(id, 'remove', video)
114} 114}
115 115
116// --------------------------------------------------------------------------- 116// ---------------------------------------------------------------------------
@@ -164,9 +164,9 @@ function getForeignPodsList (url, callback) {
164 164
165function makeRequestsToWinningPods (cert, pods_list, callback) { 165function makeRequestsToWinningPods (cert, pods_list, callback) {
166 // Stop pool requests 166 // Stop pool requests
167 poolRequests.deactivate() 167 requestsScheduler.deactivate()
168 // Flush pool requests 168 // Flush pool requests
169 poolRequests.forceSend() 169 requestsScheduler.forceSend()
170 170
171 // Get the list of our videos to send to our new friends 171 // Get the list of our videos to send to our new friends
172 Videos.listOwned(function (err, videos_list) { 172 Videos.listOwned(function (err, videos_list) {
@@ -213,7 +213,7 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
213 213
214 function endRequests (err) { 214 function endRequests (err) {
215 // Now we made new friends, we can re activate the pool of requests 215 // Now we made new friends, we can re activate the pool of requests
216 poolRequests.activate() 216 requestsScheduler.activate()
217 217
218 if (err) { 218 if (err) {
219 logger.error('There was some errors when we wanted to make friends.') 219 logger.error('There was some errors when we wanted to make friends.')
diff --git a/server/lib/poolRequests.js b/server/lib/requestsScheduler.js
index 49e61700c..2c5474e51 100644
--- a/server/lib/poolRequests.js
+++ b/server/lib/requestsScheduler.js
@@ -6,13 +6,13 @@ const map = require('lodash/map')
6const constants = require('../initializers/constants') 6const constants = require('../initializers/constants')
7const logger = require('../helpers/logger') 7const logger = require('../helpers/logger')
8const Pods = require('../models/pods') 8const Pods = require('../models/pods')
9const PoolRequests = require('../models/poolRequests') 9const Requests = require('../models/requests')
10const requests = require('../helpers/requests') 10const requests = require('../helpers/requests')
11const Videos = require('../models/videos') 11const Videos = require('../models/videos')
12 12
13let timer = null 13let timer = null
14 14
15const poolRequests = { 15const requestsScheduler = {
16 activate: activate, 16 activate: activate,
17 addRequest: addRequest, 17 addRequest: addRequest,
18 deactivate: deactivate, 18 deactivate: deactivate,
@@ -20,16 +20,16 @@ const poolRequests = {
20} 20}
21 21
22function activate () { 22function activate () {
23 logger.info('Pool requests activated.') 23 logger.info('Requests scheduler activated.')
24 timer = setInterval(makePoolRequests, constants.INTERVAL) 24 timer = setInterval(makeRequests, constants.INTERVAL)
25} 25}
26 26
27function addRequest (id, type, request) { 27function addRequest (id, type, request) {
28 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) 28 logger.debug('Add request to the requests scheduler.', { id: id, type: type, request: request })
29 29
30 PoolRequests.findById(id, function (err, entity) { 30 Requests.findById(id, function (err, entity) {
31 if (err) { 31 if (err) {
32 logger.error('Cannot find one pool request.', { error: err }) 32 logger.error('Cannot find one request.', { error: err })
33 return // Abort 33 return // Abort
34 } 34 }
35 35
@@ -40,15 +40,15 @@ function addRequest (id, type, request) {
40 } 40 }
41 41
42 // Remove the request of the other type 42 // Remove the request of the other type
43 PoolRequests.removeRequestById(id, function (err) { 43 Requests.removeRequestById(id, function (err) {
44 if (err) { 44 if (err) {
45 logger.error('Cannot remove a pool request.', { error: err }) 45 logger.error('Cannot remove a request.', { error: err })
46 return // Abort 46 return // Abort
47 } 47 }
48 }) 48 })
49 } else { 49 } else {
50 PoolRequests.create(id, type, request, function (err) { 50 Requests.create(id, type, request, function (err) {
51 if (err) logger.error('Cannot create a pool request.', { error: err }) 51 if (err) logger.error('Cannot create a request.', { error: err })
52 return // Abort 52 return // Abort
53 }) 53 })
54 } 54 }
@@ -56,22 +56,22 @@ function addRequest (id, type, request) {
56} 56}
57 57
58function deactivate () { 58function deactivate () {
59 logger.info('Pool requests deactivated.') 59 logger.info('Requests scheduler deactivated.')
60 clearInterval(timer) 60 clearInterval(timer)
61} 61}
62 62
63function forceSend () { 63function forceSend () {
64 logger.info('Force pool requests sending.') 64 logger.info('Force requests scheduler sending.')
65 makePoolRequests() 65 makeRequests()
66} 66}
67 67
68// --------------------------------------------------------------------------- 68// ---------------------------------------------------------------------------
69 69
70module.exports = poolRequests 70module.exports = requestsScheduler
71 71
72// --------------------------------------------------------------------------- 72// ---------------------------------------------------------------------------
73 73
74function makePoolRequest (type, requests_to_make, callback) { 74function makeRequest (type, requests_to_make, callback) {
75 if (!callback) callback = function () {} 75 if (!callback) callback = function () {}
76 76
77 Pods.list(function (err, pods) { 77 Pods.list(function (err, pods) {
@@ -118,16 +118,16 @@ function makePoolRequest (type, requests_to_make, callback) {
118 }) 118 })
119} 119}
120 120
121function makePoolRequests () { 121function makeRequests () {
122 logger.info('Making pool requests to friends.') 122 logger.info('Making requests to friends.')
123 123
124 PoolRequests.list(function (err, pool_requests) { 124 Requests.list(function (err, requests) {
125 if (err) { 125 if (err) {
126 logger.error('Cannot get the list of pool requests.', { err: err }) 126 logger.error('Cannot get the list of requests.', { err: err })
127 return // Abort 127 return // Abort
128 } 128 }
129 129
130 if (pool_requests.length === 0) return 130 if (requests.length === 0) return
131 131
132 const requests_to_make = { 132 const requests_to_make = {
133 add: { 133 add: {
@@ -140,7 +140,7 @@ function makePoolRequests () {
140 } 140 }
141 } 141 }
142 142
143 async.each(pool_requests, function (pool_request, callback_each) { 143 async.each(requests, function (pool_request, callback_each) {
144 if (pool_request.type === 'add') { 144 if (pool_request.type === 'add') {
145 requests_to_make.add.requests.push(pool_request.request) 145 requests_to_make.add.requests.push(pool_request.request)
146 requests_to_make.add.ids.push(pool_request._id) 146 requests_to_make.add.ids.push(pool_request._id)
@@ -148,7 +148,7 @@ function makePoolRequests () {
148 requests_to_make.remove.requests.push(pool_request.request) 148 requests_to_make.remove.requests.push(pool_request.request)
149 requests_to_make.remove.ids.push(pool_request._id) 149 requests_to_make.remove.ids.push(pool_request._id)
150 } else { 150 } else {
151 logger.error('Unkown pool request type.', { request_type: pool_request.type }) 151 logger.error('Unkown request type.', { request_type: pool_request.type })
152 return // abort 152 return // abort
153 } 153 }
154 154
@@ -156,19 +156,19 @@ function makePoolRequests () {
156 }, function () { 156 }, function () {
157 // Send the add requests 157 // Send the add requests
158 if (requests_to_make.add.requests.length !== 0) { 158 if (requests_to_make.add.requests.length !== 0) {
159 makePoolRequest('add', requests_to_make.add.requests, function (err) { 159 makeRequest('add', requests_to_make.add.requests, function (err) {
160 if (err) logger.error('Errors when sent add pool requests.', { error: err }) 160 if (err) logger.error('Errors when sent add requests.', { error: err })
161 161
162 PoolRequests.removeRequests(requests_to_make.add.ids) 162 Requests.removeRequests(requests_to_make.add.ids)
163 }) 163 })
164 } 164 }
165 165
166 // Send the remove requests 166 // Send the remove requests
167 if (requests_to_make.remove.requests.length !== 0) { 167 if (requests_to_make.remove.requests.length !== 0) {
168 makePoolRequest('remove', requests_to_make.remove.requests, function (err) { 168 makeRequest('remove', requests_to_make.remove.requests, function (err) {
169 if (err) logger.error('Errors when sent remove pool requests.', { error: err }) 169 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
170 170
171 PoolRequests.removeRequests(requests_to_make.remove.ids) 171 Requests.removeRequests(requests_to_make.remove.ids)
172 }) 172 })
173 } 173 }
174 }) 174 })
diff --git a/server/models/poolRequests.js b/server/models/requests.js
index 28093a94c..2152ae0e9 100644
--- a/server/models/poolRequests.js
+++ b/server/models/requests.js
@@ -6,16 +6,16 @@ const logger = require('../helpers/logger')
6 6
7// --------------------------------------------------------------------------- 7// ---------------------------------------------------------------------------
8 8
9const poolRequestsSchema = mongoose.Schema({ 9const requestsSchema = mongoose.Schema({
10 type: String, 10 type: String,
11 id: String, // Special id to find duplicates (video created we want to remove...) 11 id: String, // Special id to find duplicates (video created we want to remove...)
12 request: mongoose.Schema.Types.Mixed 12 request: mongoose.Schema.Types.Mixed
13}) 13})
14const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) 14const RequestsDB = mongoose.model('requests', requestsSchema)
15 15
16// --------------------------------------------------------------------------- 16// ---------------------------------------------------------------------------
17 17
18const PoolRequests = { 18const Requests = {
19 create: create, 19 create: create,
20 findById: findById, 20 findById: findById,
21 list: list, 21 list: list,
@@ -24,25 +24,25 @@ const PoolRequests = {
24} 24}
25 25
26function create (id, type, request, callback) { 26function create (id, type, request, callback) {
27 PoolRequestsDB.create({ id: id, type: type, request: request }, callback) 27 RequestsDB.create({ id: id, type: type, request: request }, callback)
28} 28}
29 29
30function findById (id, callback) { 30function findById (id, callback) {
31 PoolRequestsDB.findOne({ id: id }, callback) 31 RequestsDB.findOne({ id: id }, callback)
32} 32}
33 33
34function list (callback) { 34function list (callback) {
35 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) 35 RequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
36} 36}
37 37
38function removeRequestById (id, callback) { 38function removeRequestById (id, callback) {
39 PoolRequestsDB.remove({ id: id }, callback) 39 RequestsDB.remove({ id: id }, callback)
40} 40}
41 41
42function removeRequests (ids) { 42function removeRequests (ids) {
43 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { 43 RequestsDB.remove({ _id: { $in: ids } }, function (err) {
44 if (err) { 44 if (err) {
45 logger.error('Cannot remove requests from the pool requests database.', { error: err }) 45 logger.error('Cannot remove requests from the requests database.', { error: err })
46 return // Abort 46 return // Abort
47 } 47 }
48 48
@@ -52,4 +52,4 @@ function removeRequests (ids) {
52 52
53// --------------------------------------------------------------------------- 53// ---------------------------------------------------------------------------
54 54
55module.exports = PoolRequests 55module.exports = Requests