diff options
Diffstat (limited to 'server/controllers/api/remote')
-rw-r--r-- | server/controllers/api/remote/videos.js | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js index d02da4463..6d768eae8 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.js | |||
@@ -10,6 +10,7 @@ const secureMiddleware = middlewares.secure | |||
10 | const videosValidators = middlewares.validators.remote.videos | 10 | const videosValidators = middlewares.validators.remote.videos |
11 | const signatureValidators = middlewares.validators.remote.signature | 11 | const signatureValidators = middlewares.validators.remote.signature |
12 | const logger = require('../../../helpers/logger') | 12 | const logger = require('../../../helpers/logger') |
13 | const utils = require('../../../helpers/utils') | ||
13 | 14 | ||
14 | const router = express.Router() | 15 | const router = express.Router() |
15 | 16 | ||
@@ -37,11 +38,11 @@ function remoteVideos (req, res, next) { | |||
37 | 38 | ||
38 | switch (request.type) { | 39 | switch (request.type) { |
39 | case 'add': | 40 | case 'add': |
40 | addRemoteVideo(data, fromPod, callbackEach) | 41 | addRemoteVideoRetryWrapper(data, fromPod, callbackEach) |
41 | break | 42 | break |
42 | 43 | ||
43 | case 'update': | 44 | case 'update': |
44 | updateRemoteVideo(data, fromPod, callbackEach) | 45 | updateRemoteVideoRetryWrapper(data, fromPod, callbackEach) |
45 | break | 46 | break |
46 | 47 | ||
47 | case 'remove': | 48 | case 'remove': |
@@ -63,13 +64,30 @@ function remoteVideos (req, res, next) { | |||
63 | return res.type('json').status(204).end() | 64 | return res.type('json').status(204).end() |
64 | } | 65 | } |
65 | 66 | ||
67 | // Handle retries on fail | ||
68 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { | ||
69 | utils.transactionRetryer( | ||
70 | function (callback) { | ||
71 | return addRemoteVideo(videoToCreateData, fromPod, callback) | ||
72 | }, | ||
73 | function (err) { | ||
74 | if (err) { | ||
75 | logger.error('Cannot insert the remote video with many retries.', { error: err }) | ||
76 | return finalCallback(err) | ||
77 | } | ||
78 | |||
79 | return finalCallback() | ||
80 | } | ||
81 | ) | ||
82 | } | ||
83 | |||
66 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | 84 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { |
67 | logger.debug('Adding remote video "%s".', videoToCreateData.name) | 85 | logger.debug('Adding remote video "%s".', videoToCreateData.remoteId) |
68 | 86 | ||
69 | waterfall([ | 87 | waterfall([ |
70 | 88 | ||
71 | function startTransaction (callback) { | 89 | function startTransaction (callback) { |
72 | db.sequelize.transaction().asCallback(function (err, t) { | 90 | db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) { |
73 | return callback(err, t) | 91 | return callback(err, t) |
74 | }) | 92 | }) |
75 | }, | 93 | }, |
@@ -103,6 +121,7 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
103 | authorId: author.id, | 121 | authorId: author.id, |
104 | duration: videoToCreateData.duration, | 122 | duration: videoToCreateData.duration, |
105 | createdAt: videoToCreateData.createdAt, | 123 | createdAt: videoToCreateData.createdAt, |
124 | // FIXME: updatedAt does not seems to be considered by Sequelize | ||
106 | updatedAt: videoToCreateData.updatedAt | 125 | updatedAt: videoToCreateData.updatedAt |
107 | } | 126 | } |
108 | 127 | ||
@@ -142,7 +161,8 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
142 | 161 | ||
143 | ], function (err, t) { | 162 | ], function (err, t) { |
144 | if (err) { | 163 | if (err) { |
145 | logger.error('Cannot insert the remote video.') | 164 | // This is just a debug because we will retry the insert |
165 | logger.debug('Cannot insert the remote video.', { error: err }) | ||
146 | 166 | ||
147 | // Abort transaction? | 167 | // Abort transaction? |
148 | if (t) t.rollback() | 168 | if (t) t.rollback() |
@@ -157,8 +177,25 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
157 | }) | 177 | }) |
158 | } | 178 | } |
159 | 179 | ||
180 | // Handle retries on fail | ||
181 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { | ||
182 | utils.transactionRetryer( | ||
183 | function (callback) { | ||
184 | return updateRemoteVideo(videoAttributesToUpdate, fromPod, callback) | ||
185 | }, | ||
186 | function (err) { | ||
187 | if (err) { | ||
188 | logger.error('Cannot update the remote video with many retries.', { error: err }) | ||
189 | return finalCallback(err) | ||
190 | } | ||
191 | |||
192 | return finalCallback() | ||
193 | } | ||
194 | ) | ||
195 | } | ||
196 | |||
160 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | 197 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { |
161 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.name) | 198 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId) |
162 | 199 | ||
163 | waterfall([ | 200 | waterfall([ |
164 | 201 | ||
@@ -208,7 +245,8 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | |||
208 | 245 | ||
209 | ], function (err, t) { | 246 | ], function (err, t) { |
210 | if (err) { | 247 | if (err) { |
211 | logger.error('Cannot update the remote video.') | 248 | // This is just a debug because we will retry the insert |
249 | logger.debug('Cannot update the remote video.', { error: err }) | ||
212 | 250 | ||
213 | // Abort transaction? | 251 | // Abort transaction? |
214 | if (t) t.rollback() | 252 | if (t) t.rollback() |
@@ -238,7 +276,7 @@ function reportAbuseRemoteVideo (reportData, fromPod, callback) { | |||
238 | if (err || !video) { | 276 | if (err || !video) { |
239 | if (!err) err = new Error('video not found') | 277 | if (!err) err = new Error('video not found') |
240 | 278 | ||
241 | logger.error('Cannot load video from host and remote id.', { error: err }) | 279 | logger.error('Cannot load video from id.', { error: err, id: reportData.videoRemoteId }) |
242 | return callback(err) | 280 | return callback(err) |
243 | } | 281 | } |
244 | 282 | ||
@@ -260,7 +298,7 @@ function fetchVideo (podHost, remoteId, callback) { | |||
260 | if (err || !video) { | 298 | if (err || !video) { |
261 | if (!err) err = new Error('video not found') | 299 | if (!err) err = new Error('video not found') |
262 | 300 | ||
263 | logger.error('Cannot load video from host and remote id.', { error: err }) | 301 | logger.error('Cannot load video from host and remote id.', { error: err, podHost, remoteId }) |
264 | return callback(err) | 302 | return callback(err) |
265 | } | 303 | } |
266 | 304 | ||