diff --git a/.github/workflows/test-server.yml b/.github/workflows/test-server.yml index 66156261..2c52dd94 100644 --- a/.github/workflows/test-server.yml +++ b/.github/workflows/test-server.yml @@ -89,4 +89,14 @@ jobs: - name: Run snippet tests run: | npm run test:snips - working-directory: ./apps/api \ No newline at end of file + working-directory: ./apps/api + - name: Kill instances + if: always() + run: pkill -9 node + # - uses: actions/upload-artifact@v4 + # if: always() + # with: + # name: Logs + # path: | + # ./apps/api/api.log + # ./apps/api/worker.log \ No newline at end of file diff --git a/apps/api/src/__tests__/snips/lib.ts b/apps/api/src/__tests__/snips/lib.ts index fb1f6cff..cfd7515b 100644 --- a/apps/api/src/__tests__/snips/lib.ts +++ b/apps/api/src/__tests__/snips/lib.ts @@ -236,10 +236,16 @@ export async function search(body: SearchRequestInput): Promise { // ========================================= export async function creditUsage(): Promise<{ remaining_credits: number }> { - return (await request(TEST_URL) - .get("/v1/team/credit-usage") - .set("Authorization", `Bearer ${process.env.TEST_API_KEY}`) - .set("Content-Type", "application/json")).body.data; + const req = (await request(TEST_URL) + .get("/v1/team/credit-usage") + .set("Authorization", `Bearer ${process.env.TEST_API_KEY}`) + .set("Content-Type", "application/json")); + + if (req.status !== 200) { + throw req.body; + } + + return req.body.data; } export async function tokenUsage(): Promise<{ remaining_tokens: number }> { diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index d86a2511..a32a437e 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -96,15 +96,12 @@ export async function getACUC( let isExtract = mode === RateLimiterMode.Extract || mode === RateLimiterMode.ExtractStatus; - let rpcName = isExtract - ? "auth_credit_usage_chunk_extract" - : "auth_credit_usage_chunk_test_22_credit_pack_n_extract"; while (retries < maxRetries) { const client = Math.random() > (2/3) ? supabase_rr_service : supabase_service; ({ data, error } = await client.rpc( - rpcName, - { input_key: api_key }, + "auth_credit_usage_chunk_23_tally", + { input_key: api_key, i_is_extract: isExtract, tally_untallied_credits: true }, { get: true }, )); @@ -114,6 +111,7 @@ export async function getACUC( logger.warn( `Failed to retrieve authentication and credit usage data after ${retries}, trying again...`, + { error } ); retries++; if (retries === maxRetries) { diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts index 8acf3aef..b72e514c 100644 --- a/apps/api/src/services/billing/batch_billing.ts +++ b/apps/api/src/services/billing/batch_billing.ts @@ -10,8 +10,8 @@ import { getACUC, setCachedACUC } from "../../controllers/auth"; const BATCH_KEY = "billing_batch"; const BATCH_LOCK_KEY = "billing_batch_lock"; const BATCH_SIZE = 50; // Batch size for processing -const BATCH_TIMEOUT = 30000; // 15 seconds processing interval -const LOCK_TIMEOUT = 60000; // 30 seconds lock timeout +const BATCH_TIMEOUT = 15000; // 15 seconds processing interval +const LOCK_TIMEOUT = 30000; // 30 seconds lock timeout // Define interfaces for billing operations interface BillingOperation { @@ -281,7 +281,7 @@ async function supaBillTeam( _logger.info(`Batch billing team ${team_id} for ${credits} credits`); // Perform the actual database operation - const { data, error } = await supabase_service.rpc("bill_team_w_extract_3", { + const { data, error } = await supabase_service.rpc("bill_team_4_tally", { _team_id: team_id, sub_id: subscription_id ?? null, fetch_subscription: subscription_id === undefined,