fix: await flushDirtyEntries during shutdown to prevent usage data loss
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Remove fire-and-forget SIGTERM/SIGINT handlers from usage.ts (race condition with pool.end() in index.ts). Instead, await flushDirtyEntries() in the index.ts shutdown orchestrator between stopping the server and closing the DB pool.
This commit is contained in:
parent
b964b98a8b
commit
2b4fa0c690
3 changed files with 41 additions and 4 deletions
10
src/index.ts
10
src/index.ts
|
|
@ -18,7 +18,7 @@ import { recoverRouter } from "./routes/recover.js";
|
|||
import { emailChangeRouter } from "./routes/email-change.js";
|
||||
import { billingRouter } from "./routes/billing.js";
|
||||
import { authMiddleware } from "./middleware/auth.js";
|
||||
import { usageMiddleware, loadUsageData } from "./middleware/usage.js";
|
||||
import { usageMiddleware, loadUsageData, flushDirtyEntries } from "./middleware/usage.js";
|
||||
import { getUsageStats } from "./middleware/usage.js";
|
||||
import { pdfRateLimitMiddleware, getConcurrencyStats } from "./middleware/pdfRateLimit.js";
|
||||
import { initBrowser, closeBrowser } from "./services/browser.js";
|
||||
|
|
@ -406,6 +406,14 @@ async function start() {
|
|||
});
|
||||
});
|
||||
|
||||
// 1.5. Flush dirty usage entries while DB pool is still alive
|
||||
try {
|
||||
await flushDirtyEntries();
|
||||
logger.info("Usage data flushed");
|
||||
} catch (err) {
|
||||
logger.error({ err }, "Error flushing usage data during shutdown");
|
||||
}
|
||||
|
||||
// 2. Close Puppeteer browser pool
|
||||
try {
|
||||
await closeBrowser();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue