4 var async
= require('async')
6 var logger
= require('./logger')
7 var database
= require('./database')
8 var PoolRequestsDB
= database
.PoolRequestsDB
9 var PodsDB
= database
.PodsDB
10 var utils
= require('./utils')
14 // ----------- Constants -----------
16 // Time to wait between requests to the friends
17 var INTERVAL
= utils
.isTestInstance() ? 10000 : 60000
23 // ----------- Private -----------
26 function makePoolRequests () {
27 logger
.info('Making pool requests to friends.')
29 PoolRequestsDB
.find({}, { type: 1, request: 1 }, function (err
, pool_requests
) {
32 if (pool_requests
.length
=== 0) return
39 async
.each(pool_requests
, function (pool_request
, callback_each
) {
40 if (pool_request
.type
=== 'add') {
41 requests
.add
.push(pool_request
.request
)
42 } else if (pool_request
.type
=== 'remove') {
43 requests
.remove
.push(pool_request
.request
)
45 throw new Error('Unkown pool request type.')
50 makePoolRequest('add', requests
.add
)
51 makePoolRequest('remove', requests
.remove
)
52 logger
.info('Pool requests to friends sent.')
57 function updatePodsScore (good_pods
, bad_pods
) {
58 logger
.info('Updating %d good pods and %d bad pods scores.', good_pods
.length
, bad_pods
.length
)
60 PodsDB
.update({ _id: { $in: good_pods
} }, { $inc: { score: PODS_SCORE
.BONUS
} }, { multi: true }).exec()
61 PodsDB
.update({ _id: { $in: bad_pods
} }, { $inc: { score: PODS_SCORE
.MALUS
} }, { multi: true }, function (err
) {
67 function removeBadPods () {
68 PodsDB
.remove({ score: 0 }, function (err
, result
) {
71 var number_removed
= result
.result
.n
72 if (number_removed
!== 0) logger
.info('Removed %d pod.', number_removed
)
76 function makePoolRequest (type
, requests
) {
77 logger
.debug('Make pool requests scheduled.')
78 PodsDB
.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err
, pods
) {
90 params
.path
= '/api/' + global
.API_VERSION
+ '/remotevideos/add'
91 } else if (type
=== 'remove') {
92 params
.path
= '/api/' + global
.API_VERSION
+ '/remotevideos/remove'
94 throw new Error('Unkown pool request type.')
100 utils
.makeMultipleRetryRequest(params
, pods
, callbackEachPodFinished
, callbackAllPodsFinished
)
102 function callbackEachPodFinished (err
, response
, body
, url
, pod
, callback_each_pod_finished
) {
103 if (err
|| response
.statusCode
!== 200) {
104 bad_pods
.push(pod
._id
)
105 logger
.error('Error sending secure request to %s pod.', url
, { error: err
})
107 good_pods
.push(pod
._id
)
110 return callback_each_pod_finished()
113 function callbackAllPodsFinished (err
) {
115 logger
.error('There was some errors when sending the video meta data.', { error: err
})
118 updatePodsScore(good_pods
, bad_pods
)
119 PoolRequestsDB
.remove().exec()
124 // ----------- Public -----------
125 poolRequests
.activate = function () {
126 logger
.info('Pool requests activated.')
127 timer
= setInterval(makePoolRequests
, INTERVAL
)
130 poolRequests
.addToPoolRequests = function (id
, type
, request
) {
131 logger
.debug('Add request to the pool requests.', { id: id
, type: type
, request: request
})
133 PoolRequestsDB
.findOne({ id: id
}, function (err
, entity
) {
134 if (err
) logger
.error(err
)
137 if (entity
.type
=== type
) {
138 logger
.error(new Error('Cannot insert two same requests.'))
142 // Remove the request of the other type
143 PoolRequestsDB
.remove({ id: id
}, function (err
) {
144 if (err
) logger
.error(err
)
147 PoolRequestsDB
.create({ id: id
, type: type
, request: request
}, function (err
) {
148 if (err
) logger
.error(err
)
154 poolRequests
.deactivate = function () {
155 logger
.info('Pool requests deactivated.')
159 module
.exports
= poolRequests