feat: implement graceful shutdown handling

This commit is contained in:
syntaxbullet
2026-01-06 17:21:50 +01:00
parent 278ef4b6b0
commit 6ead0c0393
8 changed files with 190 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
import { DrizzleClient } from "./DrizzleClient";
import type { Transaction } from "./types";
import { isShuttingDown, incrementTransactions, decrementTransactions } from "./shutdown";
export const withTransaction = async <T>(
callback: (tx: Transaction) => Promise<T>,
@@ -8,8 +9,17 @@ export const withTransaction = async <T>(
if (tx) {
return await callback(tx);
} else {
return await DrizzleClient.transaction(async (newTx) => {
return await callback(newTx);
});
if (isShuttingDown()) {
throw new Error("System is shutting down, no new transactions allowed.");
}
incrementTransactions();
try {
return await DrizzleClient.transaction(async (newTx) => {
return await callback(newTx);
});
} finally {
decrementTransactions();
}
}
};