import { createSharedStore, type SharedStore } from "@hivegrid/shared-store"; /** * Singleton SharedStore for the worker process. The worker shares the * same Redis instance with the API and web but uses its own namespace * so keys never collide. Memoized so that re-imports during test or * bundler cycles do not open new Redis connections. */ let cachedStore: SharedStore | null = null; export function resolveWorkerSharedStoreRedisUrl(env: NodeJS.ProcessEnv = process.env): string | undefined { return env.REDIS_URL?.trim() || env.BULLMQ_REDIS_URL?.trim() || undefined; } let factory: () => SharedStore = () => createSharedStore({ namespace: "worker", redisUrl: resolveWorkerSharedStoreRedisUrl() }); export function getSharedStore(): SharedStore { if (cachedStore === null) { cachedStore = factory(); } return cachedStore; } export async function closeSharedStore(): Promise { const store = cachedStore; cachedStore = null; await store?.close(); } export function __setSharedStoreFactoryForTests(next: () => SharedStore): void { factory = next; cachedStore = null; } export function __resetSharedStoreForTests(): void { factory = () => createSharedStore({ namespace: "worker", redisUrl: resolveWorkerSharedStoreRedisUrl() }); cachedStore = null; }