Refractor retry transaction function
[oweals/peertube.git] / server / helpers / database-utils.ts
index f761cfb89813b928e9c671214b89530eef1e0a37..9b861a88ccd3c417bf963ae659979ab57c28482b 100644 (file)
@@ -1,45 +1,71 @@
-// TODO: import from ES6 when retry typing file will include errorFilter function
 import * as retry from 'async/retry'
-import * as Promise from 'bluebird'
-
+import * as Bluebird from 'bluebird'
+import { Model } from 'sequelize-typescript'
 import { logger } from './logger'
 
-type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
-function retryTransactionWrapper (functionToRetry: (... args) => Promise<any>, options: RetryTransactionWrapperOptions) {
-  const args = options.arguments ? options.arguments : []
+function retryTransactionWrapper <T, A, B, C> (
+  functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>,
+  arg1: A,
+  arg2: B,
+  arg3: C
+): Promise<T>
+
+function retryTransactionWrapper <T, A, B> (
+  functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>,
+  arg1: A,
+  arg2: B
+): Promise<T>
 
-  return transactionRetryer(
-    function (callback) {
-      functionToRetry.apply(this, args)
-        .then(result => callback(null, result))
+function retryTransactionWrapper <T, A> (
+  functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>,
+  arg1: A
+): Promise<T>
+
+function retryTransactionWrapper <T> (
+  functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
+  ...args: any[]
+): Promise<T> {
+  return transactionRetryer<T>(callback => {
+    functionToRetry.apply(this, args)
+        .then((result: T) => callback(null, result))
         .catch(err => callback(err))
-    }
-  )
+  })
   .catch(err => {
-    // Do not throw the error, continue the process
-    logger.error(options.errorMessage, { error: err })
+    logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { err })
+    throw err
   })
 }
 
-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, function (err) {
-      err ? rej(err) : res()
-    })
+function transactionRetryer <T> (func: (err: any, data: T) => any) {
+  return new Promise<T>((res, rej) => {
+    retry(
+      {
+        times: 5,
+
+        errorFilter: err => {
+          const willRetry = (err.name === 'SequelizeDatabaseError')
+          logger.debug('Maybe retrying the transaction function.', { willRetry, err })
+          return willRetry
+        }
+      },
+      func,
+      (err, data) => err ? rej(err) : res(data)
+    )
   })
 }
 
+function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) {
+  const obj = baseInstance.toJSON()
+
+  for (const key of Object.keys(obj)) {
+    instanceToUpdate.set(key, obj[key])
+  }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   retryTransactionWrapper,
-  transactionRetryer
+  transactionRetryer,
+  updateInstanceWithAnother
 }