1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
;(function () {
'use strict'
var async = require('async')
var pluck = require('lodash-node/compat/collection/pluck')
var constants = require('../initializers/constants')
var logger = require('../helpers/logger')
var Pods = require('../models/pods')
var PoolRequests = require('../models/poolRequests')
var utils = require('../helpers/utils')
var Videos = require('../models/videos')
var timer = null
var poolRequests = {
activate: activate,
deactivate: deactivate,
forceSend: forceSend
}
function deactivate () {
logger.info('Pool requests deactivated.')
clearInterval(timer)
}
function forceSend () {
logger.info('Force pool requests sending.')
makePoolRequests()
}
function activate () {
logger.info('Pool requests activated.')
timer = setInterval(makePoolRequests, constants.INTERVAL)
}
// ---------------------------------------------------------------------------
module.exports = poolRequests
// ---------------------------------------------------------------------------
function makePoolRequest (type, requests, callback) {
if (!callback) callback = function () {}
Pods.list(function (err, pods) {
if (err) throw err
var params = {
encrypt: true,
sign: true,
method: 'POST',
path: null,
data: requests
}
if (type === 'add') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
} else if (type === 'remove') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
} else {
throw new Error('Unkown pool request type.')
}
var bad_pods = []
var good_pods = []
utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
if (err || (response.statusCode !== 200 && response.statusCode !== 204)) {
bad_pods.push(pod._id)
logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
} else {
good_pods.push(pod._id)
}
return callback_each_pod_finished()
}
function callbackAllPodsFinished (err) {
if (err) return callback(err)
updatePodsScore(good_pods, bad_pods)
callback(null)
}
})
}
function makePoolRequests () {
logger.info('Making pool requests to friends.')
PoolRequests.list(function (err, pool_requests) {
if (err) throw err
if (pool_requests.length === 0) return
var requests = {
add: {
ids: [],
requests: []
},
remove: {
ids: [],
requests: []
}
}
async.each(pool_requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') {
requests.add.requests.push(pool_request.request)
requests.add.ids.push(pool_request._id)
} else if (pool_request.type === 'remove') {
requests.remove.requests.push(pool_request.request)
requests.remove.ids.push(pool_request._id)
} else {
throw new Error('Unkown pool request type.')
}
callback_each()
}, function () {
// Send the add requests
if (requests.add.requests.length !== 0) {
makePoolRequest('add', requests.add.requests, function (err) {
if (err) logger.error('Errors when sent add pool requests.', { error: err })
PoolRequests.removeRequests(requests.add.ids)
})
}
// Send the remove requests
if (requests.remove.requests.length !== 0) {
makePoolRequest('remove', requests.remove.requests, function (err) {
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
PoolRequests.removeRequests(requests.remove.ids)
})
}
})
})
}
function removeBadPods () {
Pods.findBadPods(function (err, pods) {
if (err) throw err
if (pods.length === 0) return
var urls = pluck(pods, 'url')
var ids = pluck(pods, '_id')
Videos.removeAllRemotesOf(urls, function (err, r) {
if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err })
var videos_removed = r.result.n
logger.info('Removed %d videos.', videos_removed)
Pods.removeAllByIds(ids, function (err, r) {
if (err) logger.error('Cannot remove bad pods.', { error: err })
var pods_removed = r.result.n
logger.info('Removed %d pods.', pods_removed)
})
})
})
}
function updatePodsScore (good_pods, bad_pods) {
logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS)
Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
if (err) throw err
removeBadPods()
})
}
})()
|