aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-08 17:29:45 +0200
committerChocobozzz <me@florianbigard.com>2021-06-08 17:41:48 +0200
commit28dfb44b145c537aba07ae73cb1287f25532022a (patch)
tree4aee64a049b396c8689f29e5ca8a8094dd3baf54 /server/helpers
parent75e12406e271e3aaf1f7c394a63ce570091db480 (diff)
downloadPeerTube-28dfb44b145c537aba07ae73cb1287f25532022a.tar.gz
PeerTube-28dfb44b145c537aba07ae73cb1287f25532022a.tar.zst
PeerTube-28dfb44b145c537aba07ae73cb1287f25532022a.zip
Try to speed up AP update transaction
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/database-utils.ts25
1 files changed, 17 insertions, 8 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index 7befa2c49..240b18033 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -58,7 +58,7 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) {
58 58
59 errorFilter: err => { 59 errorFilter: err => {
60 const willRetry = (err.name === 'SequelizeDatabaseError') 60 const willRetry = (err.name === 'SequelizeDatabaseError')
61 logger.debug('Maybe retrying the transaction function.', { willRetry, err }) 61 logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] })
62 return willRetry 62 return willRetry
63 } 63 }
64 }, 64 },
@@ -68,6 +68,8 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) {
68 }) 68 })
69} 69}
70 70
71// ---------------------------------------------------------------------------
72
71function updateInstanceWithAnother <M, T extends U, U extends Model<M>> (instanceToUpdate: T, baseInstance: U) { 73function updateInstanceWithAnother <M, T extends U, U extends Model<M>> (instanceToUpdate: T, baseInstance: U) {
72 const obj = baseInstance.toJSON() 74 const obj = baseInstance.toJSON()
73 75
@@ -82,12 +84,6 @@ function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
82 }) 84 })
83} 85}
84 86
85function afterCommitIfTransaction (t: Transaction, fn: Function) {
86 if (t) return t.afterCommit(() => fn())
87
88 return fn()
89}
90
91function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> ( 87function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> (
92 fromDatabase: T[], 88 fromDatabase: T[],
93 newModels: T[], 89 newModels: T[],
@@ -111,6 +107,18 @@ function setAsUpdated (table: string, id: number, transaction?: Transaction) {
111 107
112// --------------------------------------------------------------------------- 108// ---------------------------------------------------------------------------
113 109
110function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) {
111 return sequelizeTypescript.transaction(t => fn(t))
112}
113
114function afterCommitIfTransaction (t: Transaction, fn: Function) {
115 if (t) return t.afterCommit(() => fn())
116
117 return fn()
118}
119
120// ---------------------------------------------------------------------------
121
114export { 122export {
115 resetSequelizeInstance, 123 resetSequelizeInstance,
116 retryTransactionWrapper, 124 retryTransactionWrapper,
@@ -118,5 +126,6 @@ export {
118 updateInstanceWithAnother, 126 updateInstanceWithAnother,
119 afterCommitIfTransaction, 127 afterCommitIfTransaction,
120 deleteNonExistingModels, 128 deleteNonExistingModels,
121 setAsUpdated 129 setAsUpdated,
130 runInReadCommittedTransaction
122} 131}