]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/poolRequests.js
Fix tests
[github/Chocobozzz/PeerTube.git] / src / poolRequests.js
CommitLineData
0b697522
C
1;(function () {
2 'use strict'
3
4 var async = require('async')
5
656ea8f7 6 var constants = require('./constants')
0b697522
C
7 var logger = require('./logger')
8 var database = require('./database')
9 var PoolRequestsDB = database.PoolRequestsDB
10 var PodsDB = database.PodsDB
11 var utils = require('./utils')
12
13 var poolRequests = {}
14
0b697522
C
15 // ----------- Private -----------
16 var timer = null
17
8d6ae227
C
18 function removePoolRequestsFromDB (ids) {
19 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
20 if (err) {
21 logger.error('Cannot remove requests from the pool requests database.', { error: err })
22 return
23 }
24
25 logger.info('Pool requests flushed.')
26 })
27 }
28
0b697522
C
29 function makePoolRequests () {
30 logger.info('Making pool requests to friends.')
31
8d6ae227 32 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) {
0b697522
C
33 if (err) throw err
34
155098af
C
35 if (pool_requests.length === 0) return
36
0b697522 37 var requests = {
8d6ae227
C
38 add: {
39 ids: [],
40 requests: []
41 },
42 remove: {
43 ids: [],
44 requests: []
45 }
0b697522
C
46 }
47
48 async.each(pool_requests, function (pool_request, callback_each) {
49 if (pool_request.type === 'add') {
8d6ae227
C
50 requests.add.requests.push(pool_request.request)
51 requests.add.ids.push(pool_request._id)
0b697522 52 } else if (pool_request.type === 'remove') {
8d6ae227
C
53 requests.remove.requests.push(pool_request.request)
54 requests.remove.ids.push(pool_request._id)
0b697522
C
55 } else {
56 throw new Error('Unkown pool request type.')
57 }
58
59 callback_each()
60 }, function () {
8d6ae227
C
61 // Send the add requests
62 if (requests.add.requests.length !== 0) {
63 makePoolRequest('add', requests.add.requests, function (err) {
64 if (err) logger.error('Errors when sent add pool requests.', { error: err })
65
66 removePoolRequestsFromDB(requests.add.ids)
67 })
68 }
69
70 // Send the remove requests
71 if (requests.remove.requests.length !== 0) {
72 makePoolRequest('remove', requests.remove.requests, function (err) {
73 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
74
75 removePoolRequestsFromDB(requests.remove.ids)
76 })
77 }
0b697522
C
78 })
79 })
80 }
81
82 function updatePodsScore (good_pods, bad_pods) {
83 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
84
656ea8f7
C
85 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec()
86 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) {
0b697522
C
87 if (err) throw err
88 removeBadPods()
89 })
90 }
91
92 function removeBadPods () {
93 PodsDB.remove({ score: 0 }, function (err, result) {
94 if (err) throw err
95
96 var number_removed = result.result.n
97 if (number_removed !== 0) logger.info('Removed %d pod.', number_removed)
98 })
99 }
100
8d6ae227
C
101 function makePoolRequest (type, requests, callback) {
102 if (!callback) callback = function () {}
103
0b697522
C
104 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) {
105 if (err) throw err
106
107 var params = {
108 encrypt: true,
109 sign: true,
110 method: 'POST',
111 path: null,
112 data: requests
113 }
114
115 if (type === 'add') {
656ea8f7 116 params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
0b697522 117 } else if (type === 'remove') {
656ea8f7 118 params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
0b697522
C
119 } else {
120 throw new Error('Unkown pool request type.')
121 }
122
123 var bad_pods = []
124 var good_pods = []
125
126 utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
127
128 function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
129 if (err || response.statusCode !== 200) {
130 bad_pods.push(pod._id)
131 logger.error('Error sending secure request to %s pod.', url, { error: err })
132 } else {
133 good_pods.push(pod._id)
134 }
135
136 return callback_each_pod_finished()
137 }
138
139 function callbackAllPodsFinished (err) {
8d6ae227 140 if (err) return callback(err)
0b697522
C
141
142 updatePodsScore(good_pods, bad_pods)
8d6ae227 143 callback(null)
0b697522
C
144 }
145 })
146 }
147
148 // ----------- Public -----------
149 poolRequests.activate = function () {
150 logger.info('Pool requests activated.')
656ea8f7 151 timer = setInterval(makePoolRequests, constants.INTERVAL)
0b697522
C
152 }
153
154 poolRequests.addToPoolRequests = function (id, type, request) {
155 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
156
157 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
158 if (err) logger.error(err)
159
160 if (entity) {
161 if (entity.type === type) {
162 logger.error(new Error('Cannot insert two same requests.'))
163 return
164 }
165
166 // Remove the request of the other type
167 PoolRequestsDB.remove({ id: id }, function (err) {
168 if (err) logger.error(err)
169 })
170 } else {
171 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
172 if (err) logger.error(err)
173 })
174 }
175 })
176 }
177
178 poolRequests.deactivate = function () {
179 logger.info('Pool requests deactivated.')
180 clearInterval(timer)
181 }
182
183 module.exports = poolRequests
184})()