diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/lib/friends.js | 18 | ||||
-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 |
3 files changed, 47 insertions, 47 deletions
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') | |||
9 | const logger = require('../helpers/logger') | 9 | const logger = require('../helpers/logger') |
10 | const peertubeCrypto = require('../helpers/peertubeCrypto') | 10 | const peertubeCrypto = require('../helpers/peertubeCrypto') |
11 | const Pods = require('../models/pods') | 11 | const Pods = require('../models/pods') |
12 | const poolRequests = require('../lib/poolRequests') | 12 | const requestsScheduler = require('../lib/requestsScheduler') |
13 | const requests = require('../helpers/requests') | 13 | const requests = require('../helpers/requests') |
14 | const Videos = require('../models/videos') | 14 | const 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 | ||
36 | function hasFriends (callback) { | 36 | function hasFriends (callback) { |
@@ -70,9 +70,9 @@ function makeFriends (callback) { | |||
70 | 70 | ||
71 | function quitFriends (callback) { | 71 | function 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) { | |||
110 | function removeVideoToFriends (video) { | 110 | function 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 | ||
165 | function makeRequestsToWinningPods (cert, pods_list, callback) { | 165 | function 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') | |||
6 | const constants = require('../initializers/constants') | 6 | const constants = require('../initializers/constants') |
7 | const logger = require('../helpers/logger') | 7 | const logger = require('../helpers/logger') |
8 | const Pods = require('../models/pods') | 8 | const Pods = require('../models/pods') |
9 | const PoolRequests = require('../models/poolRequests') | 9 | const Requests = require('../models/requests') |
10 | const requests = require('../helpers/requests') | 10 | const requests = require('../helpers/requests') |
11 | const Videos = require('../models/videos') | 11 | const Videos = require('../models/videos') |
12 | 12 | ||
13 | let timer = null | 13 | let timer = null |
14 | 14 | ||
15 | const poolRequests = { | 15 | const 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 | ||
22 | function activate () { | 22 | function 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 | ||
27 | function addRequest (id, type, request) { | 27 | function 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 | ||
58 | function deactivate () { | 58 | function deactivate () { |
59 | logger.info('Pool requests deactivated.') | 59 | logger.info('Requests scheduler deactivated.') |
60 | clearInterval(timer) | 60 | clearInterval(timer) |
61 | } | 61 | } |
62 | 62 | ||
63 | function forceSend () { | 63 | function 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 | ||
70 | module.exports = poolRequests | 70 | module.exports = requestsScheduler |
71 | 71 | ||
72 | // --------------------------------------------------------------------------- | 72 | // --------------------------------------------------------------------------- |
73 | 73 | ||
74 | function makePoolRequest (type, requests_to_make, callback) { | 74 | function 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 | ||
121 | function makePoolRequests () { | 121 | function 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 | ||
9 | const poolRequestsSchema = mongoose.Schema({ | 9 | const 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 | }) |
14 | const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) | 14 | const RequestsDB = mongoose.model('requests', requestsSchema) |
15 | 15 | ||
16 | // --------------------------------------------------------------------------- | 16 | // --------------------------------------------------------------------------- |
17 | 17 | ||
18 | const PoolRequests = { | 18 | const 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 | ||
26 | function create (id, type, request, callback) { | 26 | function 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 | ||
30 | function findById (id, callback) { | 30 | function findById (id, callback) { |
31 | PoolRequestsDB.findOne({ id: id }, callback) | 31 | RequestsDB.findOne({ id: id }, callback) |
32 | } | 32 | } |
33 | 33 | ||
34 | function list (callback) { | 34 | function 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 | ||
38 | function removeRequestById (id, callback) { | 38 | function removeRequestById (id, callback) { |
39 | PoolRequestsDB.remove({ id: id }, callback) | 39 | RequestsDB.remove({ id: id }, callback) |
40 | } | 40 | } |
41 | 41 | ||
42 | function removeRequests (ids) { | 42 | function 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 | ||
55 | module.exports = PoolRequests | 55 | module.exports = Requests |