Skip to content

Commit 5781783

Browse files
authored
fix(engine): default to paid placement on billing errors (#2604)
* chore(billing): improve logs to distinguish between failure modes * fix(engine): default to paid placement on billing errors * chore(engine): set plan type according to paying field when missing
1 parent cca10c2 commit 5781783

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

apps/webapp/app/services/platform.v3.server.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export async function getCurrentPlan(orgId: string) {
205205
firstDayOfNextMonth.setUTCHours(0, 0, 0, 0);
206206

207207
if (!result.success) {
208-
logger.error("Error getting current plan", { orgId, error: result.error });
208+
logger.error("Error getting current plan - no success", { orgId, error: result.error });
209209
return undefined;
210210
}
211211

@@ -221,7 +221,7 @@ export async function getCurrentPlan(orgId: string) {
221221

222222
return { ...result, usage };
223223
} catch (e) {
224-
logger.error("Error getting current plan", { orgId, error: e });
224+
logger.error("Error getting current plan - caught error", { orgId, error: e });
225225
return undefined;
226226
}
227227
}
@@ -232,13 +232,13 @@ export async function getLimits(orgId: string) {
232232
try {
233233
const result = await client.currentPlan(orgId);
234234
if (!result.success) {
235-
logger.error("Error getting limits", { orgId, error: result.error });
235+
logger.error("Error getting limits - no success", { orgId, error: result.error });
236236
return undefined;
237237
}
238238

239239
return result.v3Subscription?.plan?.limits;
240240
} catch (e) {
241-
logger.error("Error getting limits", { orgId, error: e });
241+
logger.error("Error getting limits - caught error", { orgId, error: e });
242242
return undefined;
243243
}
244244
}
@@ -279,12 +279,12 @@ export async function getPlans() {
279279
try {
280280
const result = await client.plans();
281281
if (!result.success) {
282-
logger.error("Error getting plans", { error: result.error });
282+
logger.error("Error getting plans - no success", { error: result.error });
283283
return undefined;
284284
}
285285
return result;
286286
} catch (e) {
287-
logger.error("Error getting plans", { error: e });
287+
logger.error("Error getting plans - caught error", { error: e });
288288
return undefined;
289289
}
290290
}
@@ -362,12 +362,12 @@ export async function getUsage(organizationId: string, { from, to }: { from: Dat
362362
try {
363363
const result = await client.usage(organizationId, { from, to });
364364
if (!result.success) {
365-
logger.error("Error getting usage", { error: result.error });
365+
logger.error("Error getting usage - no success", { error: result.error });
366366
return undefined;
367367
}
368368
return result;
369369
} catch (e) {
370-
logger.error("Error getting usage", { error: e });
370+
logger.error("Error getting usage - caught error", { error: e });
371371
return undefined;
372372
}
373373
}
@@ -396,12 +396,12 @@ export async function getUsageSeries(organizationId: string, params: UsageSeries
396396
try {
397397
const result = await client.usageSeries(organizationId, params);
398398
if (!result.success) {
399-
logger.error("Error getting usage series", { error: result.error });
399+
logger.error("Error getting usage series - no success", { error: result.error });
400400
return undefined;
401401
}
402402
return result;
403403
} catch (e) {
404-
logger.error("Error getting usage series", { error: e });
404+
logger.error("Error getting usage series - caught error", { error: e });
405405
return undefined;
406406
}
407407
}
@@ -420,12 +420,12 @@ export async function reportInvocationUsage(
420420
additionalData,
421421
});
422422
if (!result.success) {
423-
logger.error("Error reporting invocation", { error: result.error });
423+
logger.error("Error reporting invocation - no success", { error: result.error });
424424
return undefined;
425425
}
426426
return result;
427427
} catch (e) {
428-
logger.error("Error reporting invocation", { error: e });
428+
logger.error("Error reporting invocation - caught error", { error: e });
429429
return undefined;
430430
}
431431
}
@@ -448,14 +448,14 @@ export async function getEntitlement(
448448
try {
449449
const result = await client.getEntitlement(organizationId);
450450
if (!result.success) {
451-
logger.error("Error getting entitlement", { error: result.error });
451+
logger.error("Error getting entitlement - no success", { error: result.error });
452452
return {
453453
hasAccess: true as const,
454454
};
455455
}
456456
return result;
457457
} catch (e) {
458-
logger.error("Error getting entitlement", { error: e });
458+
logger.error("Error getting entitlement - caught error", { error: e });
459459
return {
460460
hasAccess: true as const,
461461
};

apps/webapp/app/v3/runEngine.server.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defaultMachine, getCurrentPlan } from "~/services/platform.v3.server";
55
import { singleton } from "~/utils/singleton";
66
import { allMachines } from "./machinePresets.server";
77
import { meter, tracer } from "./tracer.server";
8+
import { logger } from "~/services/logger.server";
89

910
export const engine = singleton("RunEngine", createRunEngine);
1011

@@ -120,23 +121,37 @@ function createRunEngine() {
120121
getCurrentPlan: async (orgId: string) => {
121122
const plan = await getCurrentPlan(orgId);
122123

124+
// This only happens when there's no billing service running or on errors
123125
if (!plan) {
126+
logger.warn("engine.getCurrentPlan: no plan", { orgId });
124127
return {
125-
isPaying: false,
126-
type: "free",
128+
isPaying: true,
129+
type: "paid", // default to paid
127130
};
128131
}
129132

133+
// This shouldn't happen
130134
if (!plan.v3Subscription) {
135+
logger.warn("engine.getCurrentPlan: no v3 subscription", { orgId });
131136
return {
132137
isPaying: false,
133138
type: "free",
134139
};
135140
}
136141

142+
// Neither should this
143+
if (!plan.v3Subscription.plan) {
144+
logger.warn("engine.getCurrentPlan: no v3 subscription plan", { orgId });
145+
return {
146+
isPaying: plan.v3Subscription.isPaying,
147+
type: plan.v3Subscription.isPaying ? "paid" : "free",
148+
};
149+
}
150+
151+
// This is the normal case when the billing service is running
137152
return {
138153
isPaying: plan.v3Subscription.isPaying,
139-
type: plan.v3Subscription.plan?.type ?? "free",
154+
type: plan.v3Subscription.plan.type,
140155
};
141156
},
142157
},

0 commit comments

Comments
 (0)