Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
This commit is contained in:
parent
5fe7e89831
commit
6fcd19ba73
88 changed files with 1980 additions and 2505 deletions
|
@ -1,70 +1,45 @@
|
|||
import * as Sequelize from 'sequelize'
|
||||
// TODO: import from ES6 when retry typing file will include errorFilter function
|
||||
import * as retry from 'async/retry'
|
||||
import * as Promise from 'bluebird'
|
||||
|
||||
import { database as db } from '../initializers/database'
|
||||
import { logger } from './logger'
|
||||
|
||||
function commitTransaction (t: Sequelize.Transaction, callback: (err: Error) => void) {
|
||||
return t.commit().asCallback(callback)
|
||||
}
|
||||
|
||||
function rollbackTransaction (err: Error, t: Sequelize.Transaction, callback: (err: Error) => void) {
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
|
||||
function retryTransactionWrapper (functionToRetry: Function, options: RetryTransactionWrapperOptions, finalCallback: Function) {
|
||||
function retryTransactionWrapper (functionToRetry: (... args) => Promise<any>, options: RetryTransactionWrapperOptions) {
|
||||
const args = options.arguments ? options.arguments : []
|
||||
|
||||
transactionRetryer(
|
||||
return 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)
|
||||
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, { error: err })
|
||||
})
|
||||
}
|
||||
|
||||
function transactionRetryer (func: Function, callback: (err: Error) => void) {
|
||||
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: (err: Error, t: Sequelize.Transaction) => void) {
|
||||
db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) {
|
||||
// We force to return only two parameters
|
||||
return callback(err, t)
|
||||
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()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
commitTransaction,
|
||||
retryTransactionWrapper,
|
||||
rollbackTransaction,
|
||||
startSerializableTransaction,
|
||||
transactionRetryer
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue