aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/database-utils.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/database-utils.js
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/helpers/database-utils.js')
-rw-r--r--server/helpers/database-utils.js72
1 files changed, 0 insertions, 72 deletions
diff --git a/server/helpers/database-utils.js b/server/helpers/database-utils.js
deleted file mode 100644
index c72d19429..000000000
--- a/server/helpers/database-utils.js
+++ /dev/null
@@ -1,72 +0,0 @@
1'use strict'
2
3const retry = require('async/retry')
4
5const db = require('../initializers/database')
6const logger = require('./logger')
7
8const utils = {
9 commitTransaction,
10 retryTransactionWrapper,
11 rollbackTransaction,
12 startSerializableTransaction,
13 transactionRetryer
14}
15
16function commitTransaction (t, callback) {
17 return t.commit().asCallback(callback)
18}
19
20function rollbackTransaction (err, t, callback) {
21 // Try to rollback transaction
22 if (t) {
23 // Do not catch err, report the original one
24 t.rollback().asCallback(function () {
25 return callback(err)
26 })
27 } else {
28 return callback(err)
29 }
30}
31
32// { arguments, errorMessage }
33function retryTransactionWrapper (functionToRetry, options, finalCallback) {
34 const args = options.arguments ? options.arguments : []
35
36 utils.transactionRetryer(
37 function (callback) {
38 return functionToRetry.apply(this, args.concat([ callback ]))
39 },
40 function (err) {
41 if (err) {
42 logger.error(options.errorMessage, { error: err })
43 }
44
45 // Do not return the error, continue the process
46 return finalCallback(null)
47 }
48 )
49}
50
51function transactionRetryer (func, callback) {
52 retry({
53 times: 5,
54
55 errorFilter: function (err) {
56 const willRetry = (err.name === 'SequelizeDatabaseError')
57 logger.debug('Maybe retrying the transaction function.', { willRetry })
58 return willRetry
59 }
60 }, func, callback)
61}
62
63function startSerializableTransaction (callback) {
64 db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
65 // We force to return only two parameters
66 return callback(err, t)
67 })
68}
69
70// ---------------------------------------------------------------------------
71
72module.exports = utils