moar logs

This commit is contained in:
boufni95 2024-02-23 16:59:16 +01:00
parent 6c72588e53
commit 454b8cb6d8

View file

@ -1,4 +1,5 @@
import { DataSource, EntityManager, EntityTarget } from "typeorm"
import { getLogger } from "../helpers/logger"
export type TX<T> = (entityManager: EntityManager | DataSource) => Promise<T>
export type TxOperation<T> = {
@ -10,15 +11,17 @@ export default class {
DB: DataSource | EntityManager
pendingTx: boolean
transactionsQueue: { op: TxOperation<any>, res: (v: any) => void, rej: (message: string) => void }[] = []
log = getLogger({})
constructor(DB: DataSource | EntityManager) {
this.DB = DB
}
PushToQueue<T>(op: TxOperation<T>) {
if (!this.pendingTx) {
this.log("executing item immediately")
return this.execQueueItem(op)
}
this.log("holding item in queue")
return new Promise<T>((res, rej) => {
this.transactionsQueue.push({ op, res, rej })
})
@ -28,12 +31,16 @@ export default class {
this.pendingTx = false
const next = this.transactionsQueue.pop()
if (!next) {
this.log("no more items in queue")
return
}
try {
this.log("executing next item in queue")
const res = await this.execQueueItem(next.op)
this.log("resolving next item in queue")
next.res(res)
} catch (err: any) {
this.log("rejecting next item in queue")
next.rej(err.message)
}
}
@ -44,17 +51,21 @@ export default class {
}
this.pendingTx = true
if (op.dbTx) {
this.log("executing item transaction")
return this.doTransaction(op.exec)
}
this.log("executing item operation")
return this.doOperation(op.exec)
}
async doOperation<T>(exec: TX<T>) {
try {
const res = await exec(this.DB)
this.log("executing item operation done")
this.execNextInQueue()
return res
} catch (err) {
this.log("executing item operation failed")
this.execNextInQueue()
throw err
}
@ -65,9 +76,11 @@ export default class {
return this.DB.transaction(async tx => {
try {
const res = await exec(tx)
this.log("executing item transaction done")
this.execNextInQueue()
return res
} catch (err) {
this.log("executing item transaction failed")
this.execNextInQueue()
throw err
}