chore: update verify cache workflow to only run for paid products

This commit is contained in:
John Yeo
2026-01-05 14:24:57 +00:00
parent 992cafb0e3
commit 29ea681749
10 changed files with 35 additions and 13 deletions

View File

@@ -32,12 +32,14 @@ export const getSentryTags = ({
messageId,
path,
method,
alert = false,
}: {
ctx: AutumnContext;
customerId?: string;
messageId?: string;
path?: string;
method?: string;
alert?: boolean;
}) => {
if (!ctx) return;
return {
@@ -52,5 +54,6 @@ export const getSentryTags = ({
method: method,
email: ctx.user?.email,
alert: alert ? "true" : "false",
};
};

View File

@@ -32,9 +32,10 @@ export const searchStripeMeter = async ({
}) => {
const allStripeMeters = [];
let hasMore = true;
let startingAfter;
let startingAfter: string | undefined;
const start = performance.now();
// Get max 200 meters
while (hasMore) {
const response: any = await stripeCli.billing.meters.list({
limit: 100,

View File

@@ -82,6 +82,12 @@ export const rateLimitMiddleware = async (c: Context<HonoEnv>, next: Next) => {
const ctx = c.get("ctx");
try {
if (
process.env.NODE_ENV === "development" &&
ctx.org?.id === process.env.TESTS_ORG_ID
) {
return await next();
}
// 1. Determine rate limit type based on endpoint
const rateLimitType = getRateLimitType(c);

View File

@@ -1,9 +1,9 @@
// Suppress BullMQ eviction policy warnings BEFORE any imports
// Skip OpenTelemetry instrumentation in development for faster startup
await import("./sentry.js");
if (process.env.NODE_ENV !== "development") {
await import("./instrumentation.js");
await import("./sentry.js");
}
import cluster from "node:cluster";

View File

@@ -541,7 +541,7 @@ export const createFullCusProduct = async ({
}
await queueVerifyCacheConsistencyWorkflow({
newCustomerProductId: cusProdId,
newCustomerProduct: fullCusProduct,
previousFullCustomer: attachParams.customer as FullCustomer,
logger,
source: "createFullCusProduct",

View File

@@ -192,7 +192,7 @@ export const updateOneTimeCusProduct = async ({
});
await queueVerifyCacheConsistencyWorkflow({
newCustomerProductId: existingCusProduct.id,
newCustomerProduct: existingCusProduct,
previousFullCustomer: attachParams.customer as FullCustomer,
logger,
source: "updateOneTimeCusProduct",

View File

@@ -94,12 +94,14 @@ export const checkForMisingBalance = async ({
const threshold = grantedBalanceIncrease.mul(0.995);
if (grantedBalanceIncrease.gt(0) && usageIncrease.gte(threshold)) {
const errMessage = `[RACE CONDITION DETECTED] Usage increase (${usageIncrease}), granted balance increase (${grantedBalanceIncrease.toNumber()}), feature (${feature.name}), customer (${fullCustomer.id})`;
const errMessage = `[RACE CONDITION] Usage increase (${usageIncrease}), granted balance increase (${grantedBalanceIncrease.toNumber()}), feature (${feature.name}), customer (${fullCustomer.id})`;
Sentry.captureException(new Error(errMessage), {
console.log("CAPTURING SENTRY EXCEPTION");
Sentry.captureException(errMessage, {
tags: getSentryTags({
ctx,
customerId: fullCustomer.id || "",
alert: true,
}),
});

View File

@@ -1,15 +1,20 @@
import type { FullCustomer } from "../../../../../shared";
import {
cusProductToPrices,
type FullCusProduct,
type FullCustomer,
isFreeProduct,
} from "@autumn/shared";
import type { Logger } from "../../../external/logtail/logtailUtils";
import { JobName } from "../../JobName";
import { runHatchetWorkflow } from "../../queueUtils";
export const queueVerifyCacheConsistencyWorkflow = async ({
newCustomerProductId,
newCustomerProduct,
previousFullCustomer,
logger,
source,
}: {
newCustomerProductId: string;
newCustomerProduct: FullCusProduct;
previousFullCustomer: FullCustomer;
logger: Logger;
source: string;
@@ -18,13 +23,17 @@ export const queueVerifyCacheConsistencyWorkflow = async ({
`[${source}] Scheduling verify cache workflow for customer ${previousFullCustomer.id || previousFullCustomer.internal_id}`,
);
try {
// 1. Check if new customer product is not free
const newPrices = cusProductToPrices({ cusProduct: newCustomerProduct });
if (isFreeProduct({ prices: newPrices })) return;
await runHatchetWorkflow({
workflowName: JobName.VerifyCacheConsistency,
payload: {
orgId: previousFullCustomer.org_id,
env: previousFullCustomer.env,
customerId: previousFullCustomer.id || previousFullCustomer.internal_id,
newCustomerProductId,
newCustomerProductId: newCustomerProduct.id,
source,
previousFullCustomer: JSON.stringify(previousFullCustomer), // is there a better approach to this...?
},

View File

@@ -129,6 +129,7 @@ verifyCacheConsistencyWorkflow?.task({
tags: getSentryTags({
ctx: autumnContext,
customerId,
alert: true,
}),
});
}

View File

@@ -1,12 +1,12 @@
import { beforeAll, describe, test } from "bun:test";
import { type AppEnv, LegacyVersion, type Organization } from "@autumn/shared";
import chalk from "chalk";
import { addWeeks } from "date-fns";
import type Stripe from "stripe";
import { TestFeature } from "@tests/setup/v2Features.js";
import { attachAndExpectCorrect } from "@tests/utils/expectUtils/expectAttach.js";
import { advanceTestClock } from "@tests/utils/stripeUtils.js";
import ctx from "@tests/utils/testInitUtils/createTestContext.js";
import chalk from "chalk";
import { addWeeks } from "date-fns";
import type Stripe from "stripe";
import type { DrizzleCli } from "@/db/initDrizzle.js";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";