Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,8 +1,15 @@
import { createTwoTierCache, type TwoTierCache } from "@hivegrid/shared-store";
import { createTwoTierCache, type SharedStore, type TwoTierCache } from "@hivegrid/shared-store";
import { getSharedStore } from "~/shared-store.server";
import { clearCacheKey, clearCacheKeyPrefix } from "~/utils/cache.server";
import { createScopedLogger } from "~/utils/logger.server";
export type TenantSecuritySnapshot = {
id: string;
name: string;
slug: string;
deactivatedReason: string | null;
};
/**
* Tenant cache (two-tier). The existing in-process `cachified` LRU in
* `tenants.db.server.ts` stays the L1 read path; this module owns the
@@ -15,9 +22,9 @@ import { createScopedLogger } from "~/utils/logger.server";
*/
const log = createScopedLogger("tenant-cache");
const TTL_SECONDS_DEFAULT = 5 * 60;
export const TENANT_SNAPSHOT_SAFETY_TTL_SECONDS = 30;
let cacheSingleton: TwoTierCache<{ id: string } | null> | null = null;
let cacheSingleton: TwoTierCache<TenantSecuritySnapshot> | null = null;
let subscribeDisposer: (() => Promise<void>) | null = null;
function pubsubEnabledFromEnv(): boolean {
@@ -26,39 +33,70 @@ function pubsubEnabledFromEnv(): boolean {
return raw.toLowerCase() !== "false" && raw !== "0";
}
function ttlSecondsFromEnv(): number {
const raw = process.env.CACHE_L1_TTL_SECONDS;
if (!raw) return TTL_SECONDS_DEFAULT;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || parsed <= 0) return TTL_SECONDS_DEFAULT;
return Math.floor(parsed);
}
export function getTenantCache(): TwoTierCache<{ id: string } | null> {
if (cacheSingleton) return cacheSingleton;
cacheSingleton = createTwoTierCache<{ id: string } | null>({
store: getSharedStore(),
export function createTenantSecurityCache(store: SharedStore, options: { now?: () => number } = {}): TwoTierCache<TenantSecuritySnapshot> {
return createTwoTierCache<TenantSecuritySnapshot>({
store,
namespace: "tenant",
ttlSeconds: ttlSecondsFromEnv(),
ttlSeconds: TENANT_SNAPSHOT_SAFETY_TTL_SECONDS,
l1Now: options.now,
pubsubEnabled: pubsubEnabledFromEnv(),
logger: { warn: (obj, msg) => log.warn(obj, msg), error: (obj, msg) => log.error(obj, msg) },
});
}
export function getTenantCache(): TwoTierCache<TenantSecuritySnapshot> {
if (cacheSingleton) return cacheSingleton;
cacheSingleton = createTenantSecurityCache(getSharedStore());
return cacheSingleton;
}
export async function loadTenantSecuritySnapshot(
identifier: string,
getFreshValue: () => Promise<TenantSecuritySnapshot | null>
): Promise<TenantSecuritySnapshot | null> {
const cache = getTenantCache();
try {
const cached = await cache.get(identifier);
if (cached !== null) return cached;
} catch (error) {
log.warn(
{ op: "tenant-security-cache.read", err: { name: error instanceof Error ? error.name : "Error" } },
"Tenant security cache read failed; falling back to the database."
);
}
const fresh = await getFreshValue();
if (fresh !== null) {
try {
await cache.set(identifier, fresh);
} catch (error) {
log.warn(
{ op: "tenant-security-cache.write", err: { name: error instanceof Error ? error.name : "Error" } },
"Tenant security cache write failed; continuing with the database result."
);
}
}
return fresh;
}
/**
* Publish a tenant-invalidation. Call after any tenant mutation in the
* web app (settings update, logo upload, member add/remove). The local
* cachified entries are also cleared as a defence-in-depth in case the
* caller forgot to call clearCacheKey().
*/
export async function publishTenantInvalidation(tenantId: string): Promise<void> {
clearCacheKey(`tenant:${tenantId}`);
clearCacheKey(`tenantSimple:${tenantId}`);
clearCacheKey(`tenantIdOrSlug:${tenantId}`);
await getTenantCache().invalidate(tenantId).catch(() => {
/* logged inside helper */
});
export async function publishTenantInvalidation(...identifiers: string[]): Promise<void> {
const uniqueIdentifiers = [...new Set(identifiers.filter((identifier) => identifier.length > 0))];
const cache = getTenantCache();
for (const identifier of uniqueIdentifiers) {
clearCacheKey(`tenant:${identifier}`);
clearCacheKey(`tenantSimple:${identifier}`);
clearCacheKey(`tenantIdOrSlug:${identifier}`);
cache.deleteLocal(identifier);
}
// Tenant deactivation and slug changes are authorization/routing changes.
// Propagate failures so the mutating request cannot report success while a
// peer replica may still serve an old snapshot.
await Promise.all(uniqueIdentifiers.map((identifier) => cache.l2.invalidate(identifier)));
}
export async function subscribeTenantCache(): Promise<() => Promise<void>> {