aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-10-02 15:39:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-10-02 15:39:09 +0200
commita6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch)
tree03204a408d56311692c3528bedcf95d2455e94f2 /server/models/request.js
parent052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff)
parentc4403b29ad4db097af528a7f04eea07e0ed320d0 (diff)
downloadPeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz
PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst
PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/models/request.js')
-rw-r--r--server/models/request.js43
1 files changed, 32 insertions, 11 deletions
diff --git a/server/models/request.js b/server/models/request.js
index 4d521919a..2d1c5af15 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -14,19 +14,22 @@ const Pod = mongoose.model('Pod')
14const Video = mongoose.model('Video') 14const Video = mongoose.model('Video')
15 15
16let timer = null 16let timer = null
17let lastRequestTimestamp = 0
17 18
18// --------------------------------------------------------------------------- 19// ---------------------------------------------------------------------------
19 20
20const RequestSchema = mongoose.Schema({ 21const RequestSchema = mongoose.Schema({
21 request: mongoose.Schema.Types.Mixed, 22 request: mongoose.Schema.Types.Mixed,
22 to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ] 23 to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ]
23}) 24})
24 25
25RequestSchema.statics = { 26RequestSchema.statics = {
26 activate, 27 activate,
27 deactivate, 28 deactivate,
28 flush, 29 flush,
29 forceSend 30 forceSend,
31 list,
32 remainingMilliSeconds
30} 33}
31 34
32RequestSchema.pre('save', function (next) { 35RequestSchema.pre('save', function (next) {
@@ -53,12 +56,19 @@ mongoose.model('Request', RequestSchema)
53 56
54function activate () { 57function activate () {
55 logger.info('Requests scheduler activated.') 58 logger.info('Requests scheduler activated.')
56 timer = setInterval(makeRequests.bind(this), constants.INTERVAL) 59 lastRequestTimestamp = Date.now()
60
61 const self = this
62 timer = setInterval(function () {
63 lastRequestTimestamp = Date.now()
64 makeRequests.call(self)
65 }, constants.REQUESTS_INTERVAL)
57} 66}
58 67
59function deactivate () { 68function deactivate () {
60 logger.info('Requests scheduler deactivated.') 69 logger.info('Requests scheduler deactivated.')
61 clearInterval(timer) 70 clearInterval(timer)
71 timer = null
62} 72}
63 73
64function flush () { 74function flush () {
@@ -72,6 +82,16 @@ function forceSend () {
72 makeRequests.call(this) 82 makeRequests.call(this)
73} 83}
74 84
85function list (callback) {
86 this.find({ }, callback)
87}
88
89function remainingMilliSeconds () {
90 if (timer === null) return -1
91
92 return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp)
93}
94
75// --------------------------------------------------------------------------- 95// ---------------------------------------------------------------------------
76 96
77// Make a requests to friends of a certain type 97// Make a requests to friends of a certain type
@@ -91,7 +111,13 @@ function makeRequest (toPod, requestsToMake, callback) {
91 // The function fire some useful callbacks 111 // The function fire some useful callbacks
92 requests.makeSecureRequest(params, function (err, res) { 112 requests.makeSecureRequest(params, function (err, res) {
93 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { 113 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
94 logger.error('Error sending secure request to %s pod.', toPod.url, { error: err || new Error('Status code not 20x') }) 114 logger.error(
115 'Error sending secure request to %s pod.',
116 toPod.url,
117 {
118 error: err || new Error('Status code not 20x : ' + res.statusCode)
119 }
120 )
95 121
96 return callback(false) 122 return callback(false)
97 } 123 }
@@ -148,19 +174,14 @@ function makeRequests () {
148 return callbackEach() 174 return callbackEach()
149 } 175 }
150 176
151 // Maybe the pod is not our friend anymore so simply remove them 177 // Maybe the pod is not our friend anymore so simply remove it
152 if (!toPod) { 178 if (!toPod) {
179 logger.info('Removing %d requests of unexisting pod %s.', requestToMake.ids.length, toPodId)
153 removePodOf.call(self, requestToMake.ids, toPodId) 180 removePodOf.call(self, requestToMake.ids, toPodId)
154 return callbackEach() 181 return callbackEach()
155 } 182 }
156 183
157 makeRequest(toPod, requestToMake.datas, function (success) { 184 makeRequest(toPod, requestToMake.datas, function (success) {
158 if (err) {
159 logger.error('Errors when sent request to %s.', toPod.url, { error: err })
160 // Do not stop the process just for one error
161 return callbackEach()
162 }
163
164 if (success === true) { 185 if (success === true) {
165 logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) 186 logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids })
166 187