diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-15 19:13:16 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-15 19:13:16 +0100 |
commit | d6a5b018b89f9d2569ca7435b0e270095c93cc17 (patch) | |
tree | c7f24df4853ad2de7934dfa926ef915222a38277 | |
parent | a7721e62c028f54c1d165b53ac1a7a60b6c3b82e (diff) | |
download | PeerTube-d6a5b018b89f9d2569ca7435b0e270095c93cc17.tar.gz PeerTube-d6a5b018b89f9d2569ca7435b0e270095c93cc17.tar.zst PeerTube-d6a5b018b89f9d2569ca7435b0e270095c93cc17.zip |
Server: retryer transaction wrapper refractoring
-rw-r--r-- | .codeclimate.yml | 1 | ||||
-rw-r--r-- | server/controllers/api/remote/videos.js | 34 | ||||
-rw-r--r-- | server/controllers/api/videos.js | 46 | ||||
-rw-r--r-- | server/helpers/utils.js | 20 |
4 files changed, 51 insertions, 50 deletions
diff --git a/.codeclimate.yml b/.codeclimate.yml index ddff519bf..2318cfa4b 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml | |||
@@ -19,5 +19,6 @@ exclude_paths: | |||
19 | - config/ | 19 | - config/ |
20 | - node_modules/ | 20 | - node_modules/ |
21 | - client | 21 | - client |
22 | - scripts/ | ||
22 | - server/tests/ | 23 | - server/tests/ |
23 | - .tmp/ | 24 | - .tmp/ |
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js index c45a86dbb..9d007246f 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.js | |||
@@ -66,19 +66,12 @@ function remoteVideos (req, res, next) { | |||
66 | 66 | ||
67 | // Handle retries on fail | 67 | // Handle retries on fail |
68 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { | 68 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { |
69 | utils.transactionRetryer( | 69 | const options = { |
70 | function (callback) { | 70 | arguments: [ videoToCreateData, fromPod ], |
71 | return addRemoteVideo(videoToCreateData, fromPod, callback) | 71 | errorMessage: 'Cannot insert the remote video with many retries.' |
72 | }, | 72 | } |
73 | function (err) { | ||
74 | if (err) { | ||
75 | logger.error('Cannot insert the remote video with many retries.', { error: err }) | ||
76 | } | ||
77 | 73 | ||
78 | // Do not return the error, continue the process | 74 | utils.retryWrapper(addRemoteVideo, options, finalCallback) |
79 | return finalCallback(null) | ||
80 | } | ||
81 | ) | ||
82 | } | 75 | } |
83 | 76 | ||
84 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | 77 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { |
@@ -182,19 +175,12 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
182 | 175 | ||
183 | // Handle retries on fail | 176 | // Handle retries on fail |
184 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { | 177 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { |
185 | utils.transactionRetryer( | 178 | const options = { |
186 | function (callback) { | 179 | arguments: [ fromPod, videoAttributesToUpdate ], |
187 | return updateRemoteVideo(videoAttributesToUpdate, fromPod, callback) | 180 | errorMessage: 'Cannot update the remote video with many retries' |
188 | }, | 181 | } |
189 | function (err) { | ||
190 | if (err) { | ||
191 | logger.error('Cannot update the remote video with many retries.', { error: err }) | ||
192 | } | ||
193 | 182 | ||
194 | // Do not return the error, continue the process | 183 | utils.retryWrapper(updateRemoteVideo, options, finalCallback) |
195 | return finalCallback(null) | ||
196 | } | ||
197 | ) | ||
198 | } | 184 | } |
199 | 185 | ||
200 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | 186 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { |
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 2c4af520e..9a50a29be 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js | |||
@@ -106,20 +106,17 @@ module.exports = router | |||
106 | // Wrapper to video add that retry the function if there is a database error | 106 | // Wrapper to video add that retry the function if there is a database error |
107 | // We need this because we run the transaction in SERIALIZABLE isolation that can fail | 107 | // We need this because we run the transaction in SERIALIZABLE isolation that can fail |
108 | function addVideoRetryWrapper (req, res, next) { | 108 | function addVideoRetryWrapper (req, res, next) { |
109 | utils.transactionRetryer( | 109 | const options = { |
110 | function (callback) { | 110 | arguments: [ req, res, req.files.videofile[0] ], |
111 | return addVideo(req, res, req.files.videofile[0], callback) | 111 | errorMessage: 'Cannot insert the video with many retries.' |
112 | }, | 112 | } |
113 | function (err) { | ||
114 | if (err) { | ||
115 | logger.error('Cannot insert the video with many retries.', { error: err }) | ||
116 | return next(err) | ||
117 | } | ||
118 | 113 | ||
119 | // TODO : include Location of the new video -> 201 | 114 | utils.retryWrapper(addVideo, options, function (err) { |
120 | return res.type('json').status(204).end() | 115 | if (err) return next(err) |
121 | } | 116 | |
122 | ) | 117 | // TODO : include Location of the new video -> 201 |
118 | return res.type('json').status(204).end() | ||
119 | }) | ||
123 | } | 120 | } |
124 | 121 | ||
125 | function addVideo (req, res, videoFile, callback) { | 122 | function addVideo (req, res, videoFile, callback) { |
@@ -241,20 +238,17 @@ function addVideo (req, res, videoFile, callback) { | |||
241 | } | 238 | } |
242 | 239 | ||
243 | function updateVideoRetryWrapper (req, res, next) { | 240 | function updateVideoRetryWrapper (req, res, next) { |
244 | utils.transactionRetryer( | 241 | const options = { |
245 | function (callback) { | 242 | arguments: [ req, res ], |
246 | return updateVideo(req, res, callback) | 243 | errorMessage: 'Cannot update the video with many retries.' |
247 | }, | 244 | } |
248 | function (err) { | ||
249 | if (err) { | ||
250 | logger.error('Cannot update the video with many retries.', { error: err }) | ||
251 | return next(err) | ||
252 | } | ||
253 | 245 | ||
254 | // TODO : include Location of the new video -> 201 | 246 | utils.retryWrapper(updateVideo, options, function (err) { |
255 | return res.type('json').status(204).end() | 247 | if (err) return next(err) |
256 | } | 248 | |
257 | ) | 249 | // TODO : include Location of the new video -> 201 |
250 | return res.type('json').status(204).end() | ||
251 | }) | ||
258 | } | 252 | } |
259 | 253 | ||
260 | function updateVideo (req, res, finalCallback) { | 254 | function updateVideo (req, res, finalCallback) { |
diff --git a/server/helpers/utils.js b/server/helpers/utils.js index a902850cd..fb4dd08cc 100644 --- a/server/helpers/utils.js +++ b/server/helpers/utils.js | |||
@@ -11,6 +11,7 @@ const utils = { | |||
11 | generateRandomString, | 11 | generateRandomString, |
12 | isTestInstance, | 12 | isTestInstance, |
13 | getFormatedObjects, | 13 | getFormatedObjects, |
14 | retryWrapper, | ||
14 | transactionRetryer | 15 | transactionRetryer |
15 | } | 16 | } |
16 | 17 | ||
@@ -48,6 +49,25 @@ function getFormatedObjects (objects, objectsTotal) { | |||
48 | } | 49 | } |
49 | } | 50 | } |
50 | 51 | ||
52 | // { arguments, errorMessage } | ||
53 | function retryWrapper (functionToRetry, options, finalCallback) { | ||
54 | const args = options.arguments ? options.arguments : [] | ||
55 | |||
56 | utils.transactionRetryer( | ||
57 | function (callback) { | ||
58 | return functionToRetry.apply(this, args.concat([ callback ])) | ||
59 | }, | ||
60 | function (err) { | ||
61 | if (err) { | ||
62 | logger.error(options.errorMessage, { error: err }) | ||
63 | } | ||
64 | |||
65 | // Do not return the error, continue the process | ||
66 | return finalCallback(null) | ||
67 | } | ||
68 | ) | ||
69 | } | ||
70 | |||
51 | function transactionRetryer (func, callback) { | 71 | function transactionRetryer (func, callback) { |
52 | retry({ | 72 | retry({ |
53 | times: 5, | 73 | times: 5, |