feat: implement graceful shutdown handling
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user