]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/database-utils.ts
Implement video transcoding on server side
[github/Chocobozzz/PeerTube.git] / server / helpers / database-utils.ts
index b842ab9ece3d5eed0085cf2a426c67d7644c00ac..987e42eb074a0441fab5f2c43aa6d646ea0054c6 100644 (file)
@@ -1,69 +1,41 @@
 // TODO: import from ES6 when retry typing file will include errorFilter function
-import retry = require('async/retry')
+import * as retry from 'async/retry'
+import * as Promise from 'bluebird'
 
-const db = require('../initializers/database')
 import { logger } from './logger'
 
-function commitTransaction (t, callback) {
-  return t.commit().asCallback(callback)
-}
-
-function rollbackTransaction (err, t, callback) {
-  // Try to rollback transaction
-  if (t) {
-    // Do not catch err, report the original one
-    t.rollback().asCallback(function () {
-      return callback(err)
-    })
-  } else {
-    return callback(err)
-  }
-}
-
-// { arguments, errorMessage }
-function retryTransactionWrapper (functionToRetry, options, finalCallback) {
+type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
+function retryTransactionWrapper (functionToRetry: (... args) => Promise<any>, options: RetryTransactionWrapperOptions) {
   const args = options.arguments ? options.arguments : []
 
-  transactionRetryer(
-    function (callback) {
-      return functionToRetry.apply(this, args.concat([ callback ]))
-    },
-    function (err) {
-      if (err) {
-        logger.error(options.errorMessage, { error: err })
-      }
-
-      // Do not return the error, continue the process
-      return finalCallback(null)
-    }
-  )
+  return transactionRetryer(callback => {
+    functionToRetry.apply(this, args)
+        .then(result => callback(null, result))
+        .catch(err => callback(err))
+  })
+  .catch(err => {
+    // Do not throw the error, continue the process
+    logger.error(options.errorMessage, err)
+  })
 }
 
-function transactionRetryer (func, callback) {
-  retry({
-    times: 5,
+function transactionRetryer (func: Function) {
+  return new Promise((res, rej) => {
+    retry({
+      times: 5,
 
-    errorFilter: function (err) {
-      const willRetry = (err.name === 'SequelizeDatabaseError')
-      logger.debug('Maybe retrying the transaction function.', { willRetry })
-      return willRetry
-    }
-  }, func, callback)
-}
-
-function startSerializableTransaction (callback) {
-  db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
-    // We force to return only two parameters
-    return callback(err, t)
+      errorFilter: err => {
+        const willRetry = (err.name === 'SequelizeDatabaseError')
+        logger.debug('Maybe retrying the transaction function.', { willRetry })
+        return willRetry
+      }
+    }, func, err => err ? rej(err) : res())
   })
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  commitTransaction,
   retryTransactionWrapper,
-  rollbackTransaction,
-  startSerializableTransaction,
   transactionRetryer
 }