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