fix(settings): сохранять результат операций базы при вторичных сбоях

This commit is contained in:
Denozordec committed 2026-10-03 16:43:52 +07:00
1 parent 738c98b9d3
commit 92b65a30ce
15 files changed
+159 -32

No files matched your search

+4 -2
View File
@@ -3,13 +3,13 @@ type Client = {
release: (destroy?: boolean) => void
}
/** Nonblocking session lock: a second destructive operation is rejected rather than queued. */
export async function withTrafficFlowPurgeLock<T>(connect: () => Promise<Client>, key: number, run: () => Promise<T>): Promise<T> {
export async function withOperationLock<T>(connect: () => Promise<Client>, key: number, run: () => Promise<T>, busyMessage = "Сброс NetFlow уже выполняется"): Promise<T> {
const client = await connect()
let owned = false, destroy = false
try {
destroy = true
const result = await client.query("SELECT pg_try_advisory_lock($1) AS acquired", [key])
if (result.rows[0]?.acquired === false) { destroy = false; throw new Error("Сброс NetFlow уже выполняется") }
if (result.rows[0]?.acquired === false) { destroy = false; throw new Error(busyMessage) }
if (result.rows[0]?.acquired !== true) throw new Error("Блокировка сброса NetFlow не подтверждена")
owned = true; destroy = false
return await run()
@@ -24,3 +24,5 @@ export async function withTrafficFlowPurgeLock<T>(connect: () => Promise<Client>
client.release(destroy)
}
}
export const withTrafficFlowPurgeLock = withOperationLock