Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Tenant plan lookup wiring for BullMQ priority resolution.
|
||||
*
|
||||
* The queue package's `resolveJobPriorityForTenant` accepts a pluggable
|
||||
* `TenantPlanLookup`. This module registers one that reads from the
|
||||
* subscription cache (two-tier, L1 in-process + L2 SharedStore) so each
|
||||
* enqueue is a cheap synchronous-ish lookup instead of a DB hit.
|
||||
*
|
||||
* Plan-tier mapping is intentionally pluggable: the boilerplate has no
|
||||
* hard-coded `enterprise|pro|free` field on tenants. Operators wire their
|
||||
* own classifier via `setSubscriptionToPlanTier`. Until that is set, this
|
||||
* lookup returns `null` for every tenant, which the resolver then maps to
|
||||
* NORMAL. Internal jobs (`tenantId: null`) still get CRITICAL.
|
||||
*
|
||||
* Bootstrap:
|
||||
* ```ts
|
||||
* import { wireTenantPlanLookup } from "~/utils/services/.server/tenantPlanLookup.server";
|
||||
* wireTenantPlanLookup();
|
||||
* ```
|
||||
* Call once during web boot — idempotent.
|
||||
*/
|
||||
import type { TenantPlanIdentifier } from "@toolctrl/queue";
|
||||
import { setTenantPlanLookup } from "@toolctrl/queue";
|
||||
import type { TenantSubscriptionWithDetails } from "@toolctrl/db/subscriptions.server";
|
||||
import { getTenantSubscription } from "@toolctrl/db/subscriptions.server";
|
||||
import { getSubscriptionCache } from "./subscriptionCache.server";
|
||||
import { createScopedLogger } from "~/utils/logger.server";
|
||||
|
||||
const log = createScopedLogger("tenant-plan-lookup");
|
||||
|
||||
export type SubscriptionPlanClassifier = (
|
||||
subscription: TenantSubscriptionWithDetails | null,
|
||||
) => TenantPlanIdentifier | null;
|
||||
|
||||
let classifier: SubscriptionPlanClassifier = () => null;
|
||||
let wired = false;
|
||||
|
||||
/**
|
||||
* Replace the active classifier. Operators wire their own mapping
|
||||
* (e.g. by stripe product id → "enterprise"|"pro"|"free"). Passing the
|
||||
* default no-op classifier disables plan-based priority and falls back
|
||||
* to NORMAL for all tenants.
|
||||
*/
|
||||
export function setSubscriptionToPlanTier(next: SubscriptionPlanClassifier): void {
|
||||
classifier = next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cached lookup used by both the queue resolver and any caller that wants
|
||||
* a cheap plan tier for the given tenant.
|
||||
*/
|
||||
export async function getCachedTenantPlan(tenantId: string): Promise<TenantPlanIdentifier | null> {
|
||||
const cache = getSubscriptionCache();
|
||||
let cached = await cache.get(tenantId);
|
||||
if (cached === null) {
|
||||
try {
|
||||
const fresh = await getTenantSubscription(tenantId);
|
||||
await cache.set(tenantId, fresh ?? null);
|
||||
cached = fresh ?? null;
|
||||
} catch (error) {
|
||||
log.warn(
|
||||
{ tenantId, error: error instanceof Error ? error.message : String(error) },
|
||||
"getCachedTenantPlan: subscription fetch failed; returning null",
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return classifier(cached);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the lookup on the queue package. Idempotent — calling twice is
|
||||
* a no-op so per-request bootstraps don't churn the registration.
|
||||
*/
|
||||
export function wireTenantPlanLookup(): void {
|
||||
if (wired) return;
|
||||
wired = true;
|
||||
setTenantPlanLookup((tenantId) => getCachedTenantPlan(tenantId));
|
||||
log.info({}, "tenant plan lookup wired into queue priority resolver");
|
||||
}
|
||||
|
||||
/** Test seam — drop the wiring + classifier. */
|
||||
export function __resetTenantPlanLookupForTests(): void {
|
||||
classifier = () => null;
|
||||
wired = false;
|
||||
setTenantPlanLookup(null);
|
||||
}
|
||||
Reference in new issue
Block a user