]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/database-utils.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / database-utils.ts
CommitLineData
4d4e5cd4 1import * as retry from 'async/retry'
20494f12 2import * as Bluebird from 'bluebird'
06215f15 3import { Model } from 'sequelize-typescript'
65fcc311 4import { logger } from './logger'
2284f202 5import { Transaction } from 'sequelize'
4145c1c6 6
90d4bb81
C
7function retryTransactionWrapper <T, A, B, C> (
8 functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>,
9 arg1: A,
10 arg2: B,
11 arg3: C
12): Promise<T>
13
14function retryTransactionWrapper <T, A, B> (
15 functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>,
16 arg1: A,
17 arg2: B
18): Promise<T>
19
20function retryTransactionWrapper <T, A> (
21 functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>,
22 arg1: A
23): Promise<T>
24
2baea0c7
C
25function retryTransactionWrapper <T> (
26 functionToRetry: () => Promise<T> | Bluebird<T>
27): Promise<T>
28
0f91ae62 29function retryTransactionWrapper <T> (
90d4bb81
C
30 functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
31 ...args: any[]
0f91ae62 32): Promise<T> {
0f91ae62 33 return transactionRetryer<T>(callback => {
2baea0c7 34 functionToRetry.apply(null, args)
0f91ae62 35 .then((result: T) => callback(null, result))
6fcd19ba 36 .catch(err => callback(err))
075f16ca 37 })
6fcd19ba 38 .catch(err => {
1e11f67b 39 logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
20494f12 40 throw err
6fcd19ba 41 })
4df023f2
C
42}
43
0f91ae62
C
44function transactionRetryer <T> (func: (err: any, data: T) => any) {
45 return new Promise<T>((res, rej) => {
90d4bb81
C
46 retry(
47 {
48 times: 5,
49
50 errorFilter: err => {
51 const willRetry = (err.name === 'SequelizeDatabaseError')
52 logger.debug('Maybe retrying the transaction function.', { willRetry, err })
53 return willRetry
54 }
55 },
56 func,
57 (err, data) => err ? rej(err) : res(data)
58 )
4df023f2
C
59 })
60}
61
c39ea24b 62function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) {
a5625b41
C
63 const obj = baseInstance.toJSON()
64
65 for (const key of Object.keys(obj)) {
1735c825 66 instanceToUpdate[key] = obj[key]
a5625b41
C
67 }
68}
69
06215f15
C
70function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
71 Object.keys(savedFields).forEach(key => {
1735c825 72 instance[key] = savedFields[key]
06215f15
C
73 })
74}
75
2284f202
C
76function afterCommitIfTransaction (t: Transaction, fn: Function) {
77 if (t) return t.afterCommit(() => fn())
78
79 return fn()
80}
81
d7a25329
C
82function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Model<T>> (
83 fromDatabase: T[],
84 newModels: T[],
85 t: Transaction
86) {
87 return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
88 .map(f => f.destroy({ transaction: t }))
89}
90
4df023f2
C
91// ---------------------------------------------------------------------------
92
65fcc311 93export {
06215f15 94 resetSequelizeInstance,
65fcc311 95 retryTransactionWrapper,
a5625b41 96 transactionRetryer,
2284f202 97 updateInstanceWithAnother,
d7a25329
C
98 afterCommitIfTransaction,
99 deleteNonExistingModels
65fcc311 100}