aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js18
-rw-r--r--server/lib/requestsScheduler.js (renamed from server/lib/poolRequests.js)56
2 files changed, 37 insertions, 37 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')
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 })