Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): catch errors in the billing batch interval (#5191)

Gergő Móricz34934737268a
diff --git a/.github/workflows/test-server.yml b/.github/workflows/test-server.yml
index ee3d8a3fb..42336783c 100644
--- a/.github/workflows/test-server.yml
+++ b/.github/workflows/test-server.yml
@@ -245,6 +245,11 @@ jobs:
run: pnpm exec vitest run src/services/autumn/__tests__/autumn.service.test.ts --reporter=default
working-directory: apps/api
+ - name: Run billing batch regression tests
+ if: matrix.engine == 'fetch' && matrix.proxy == 'no-proxy' && matrix.nuq == 'postgres'
+ run: pnpm exec vitest run src/services/billing/__tests__/batch_billing.test.ts --reporter=default
+ working-directory: apps/api
+
- name: Run safeFetch SSRF guard regression tests
if: matrix.engine == 'fetch' && matrix.proxy == 'no-proxy' && matrix.nuq == 'postgres'
run: pnpm exec vitest run src/scraper/scrapeURL/engines/utils/safeFetch.proxy.test.ts --reporter=default
diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
index 38912a340..69432224a 100644
--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts
+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
@@ -118,7 +118,10 @@ vi.mock("../../queue-service", () => ({
getRedisConnection: () => redis,
}));
-import { processBillingBatch } from "../batch_billing";
+import {
+ processBillingBatch,
+ startBillingBatchProcessing,
+} from "../batch_billing";
function makeOp(overrides: Record<string, unknown> = {}) {
// `org_id: undefined` in an override drops the key entirely, which is the
@@ -397,3 +400,31 @@ describe("processBillingBatch", () => {
expect(queue).toHaveLength(0);
});
});
+
+describe("startBillingBatchProcessing", () => {
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it("logs a Redis error on a tick and keeps processing on the next one", async () => {
+ vi.useFakeTimers();
+ const loadingError = new Error(
+ "LOADING Redis is loading the dataset in memory",
+ );
+ redis.llen.mockRejectedValueOnce(loadingError);
+
+ startBillingBatchProcessing();
+
+ await vi.advanceTimersByTimeAsync(15000);
+ expect(logger.error).toHaveBeenCalledWith(
+ "Error in periodic billing batch processing",
+ { error: loadingError },
+ );
+ expect(redis.set).not.toHaveBeenCalled();
+
+ queue = [makeOp()];
+ await vi.advanceTimersByTimeAsync(15000);
+ expect(billTeam7).toHaveBeenCalledTimes(1);
+ expect(queue).toHaveLength(0);
+ });
+});
diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts
index 203c2cc6a..0fbb35c6d 100644
--- a/apps/api/src/services/billing/batch_billing.ts
+++ b/apps/api/src/services/billing/batch_billing.ts
@@ -412,9 +412,15 @@ export function startBillingBatchProcessing() {
logger.info("🔄 Starting periodic billing batch processing");
batchInterval = setInterval(async () => {
- const queueLength = await getRedisConnection().llen(BATCH_KEY);
- logger.info(`Checking billing batch queue (${queueLength} items pending)`);
- await processBillingBatch();
+ try {
+ const queueLength = await getRedisConnection().llen(BATCH_KEY);
+ logger.info(
+ `Checking billing batch queue (${queueLength} items pending)`,
+ );
+ await processBillingBatch();
+ } catch (error) {
+ logger.error("Error in periodic billing batch processing", { error });
+ }
}, BATCH_TIMEOUT);
// Unref to not keep process alive