aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/poolRequests.js59
1 files changed, 46 insertions, 13 deletions
diff --git a/src/poolRequests.js b/src/poolRequests.js
index 391ed6ab2..190fb3659 100644
--- a/src/poolRequests.js
+++ b/src/poolRequests.js
@@ -23,33 +23,66 @@
23 // ----------- Private ----------- 23 // ----------- Private -----------
24 var timer = null 24 var timer = null
25 25
26 function removePoolRequestsFromDB (ids) {
27 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
28 if (err) {
29 logger.error('Cannot remove requests from the pool requests database.', { error: err })
30 return
31 }
32
33 logger.info('Pool requests flushed.')
34 })
35 }
36
26 function makePoolRequests () { 37 function makePoolRequests () {
27 logger.info('Making pool requests to friends.') 38 logger.info('Making pool requests to friends.')
28 39
29 PoolRequestsDB.find({}, { type: 1, request: 1 }, function (err, pool_requests) { 40 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) {
30 if (err) throw err 41 if (err) throw err
31 42
32 if (pool_requests.length === 0) return 43 if (pool_requests.length === 0) return
33 44
34 var requests = { 45 var requests = {
35 add: [], 46 add: {
36 remove: [] 47 ids: [],
48 requests: []
49 },
50 remove: {
51 ids: [],
52 requests: []
53 }
37 } 54 }
38 55
39 async.each(pool_requests, function (pool_request, callback_each) { 56 async.each(pool_requests, function (pool_request, callback_each) {
40 if (pool_request.type === 'add') { 57 if (pool_request.type === 'add') {
41 requests.add.push(pool_request.request) 58 requests.add.requests.push(pool_request.request)
59 requests.add.ids.push(pool_request._id)
42 } else if (pool_request.type === 'remove') { 60 } else if (pool_request.type === 'remove') {
43 requests.remove.push(pool_request.request) 61 requests.remove.requests.push(pool_request.request)
62 requests.remove.ids.push(pool_request._id)
44 } else { 63 } else {
45 throw new Error('Unkown pool request type.') 64 throw new Error('Unkown pool request type.')
46 } 65 }
47 66
48 callback_each() 67 callback_each()
49 }, function () { 68 }, function () {
50 makePoolRequest('add', requests.add) 69 // Send the add requests
51 makePoolRequest('remove', requests.remove) 70 if (requests.add.requests.length !== 0) {
52 logger.info('Pool requests to friends sent.') 71 makePoolRequest('add', requests.add.requests, function (err) {
72 if (err) logger.error('Errors when sent add pool requests.', { error: err })
73
74 removePoolRequestsFromDB(requests.add.ids)
75 })
76 }
77
78 // Send the remove requests
79 if (requests.remove.requests.length !== 0) {
80 makePoolRequest('remove', requests.remove.requests, function (err) {
81 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
82
83 removePoolRequestsFromDB(requests.remove.ids)
84 })
85 }
53 }) 86 })
54 }) 87 })
55 } 88 }
@@ -73,7 +106,9 @@
73 }) 106 })
74 } 107 }
75 108
76 function makePoolRequest (type, requests) { 109 function makePoolRequest (type, requests, callback) {
110 if (!callback) callback = function () {}
111
77 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { 112 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) {
78 if (err) throw err 113 if (err) throw err
79 114
@@ -110,12 +145,10 @@
110 } 145 }
111 146
112 function callbackAllPodsFinished (err) { 147 function callbackAllPodsFinished (err) {
113 if (err) { 148 if (err) return callback(err)
114 logger.error('There was some errors when sending the video meta data.', { error: err })
115 }
116 149
117 updatePodsScore(good_pods, bad_pods) 150 updatePodsScore(good_pods, bad_pods)
118 PoolRequestsDB.remove().exec() 151 callback(null)
119 } 152 }
120 }) 153 })
121 } 154 }