107 lines
3.8 KiB
TypeScript
107 lines
3.8 KiB
TypeScript
/**
|
|
* Bootstrap helper that starts a dedicated BullMQ worker subscribed to
|
|
* the `outbox.search-index` queue inside the worker-general process.
|
|
*
|
|
* The worker is gated on:
|
|
* - WORKER_QUEUE_GROUP === "general" (other groups never touch search)
|
|
* - MEILI_URL set (otherwise the indexer cannot connect)
|
|
* - SEARCH_INDEXER_ENABLED !== "false" (escape hatch)
|
|
*
|
|
* Returns `{ status: "disabled" }` when any precondition fails. Tests
|
|
* inject a fake `createWorkerImpl` to assert subscription semantics
|
|
* without spinning up a BullMQ Worker.
|
|
*/
|
|
|
|
import type { EnvRecord } from "@toolctrl/config";
|
|
import { createSearchClient, type SearchClient } from "@toolctrl/search";
|
|
import {
|
|
runSearchIndexerJob,
|
|
SEARCH_INDEX_OUTBOX_QUEUE,
|
|
type SearchIndexerDependencies,
|
|
type SearchIndexerLogger,
|
|
} from "./search-indexer.js";
|
|
|
|
export type SearchIndexerBootstrapEnv = EnvRecord & {
|
|
MEILI_URL?: string;
|
|
MEILI_API_KEY?: string;
|
|
SEARCH_INDEXER_ENABLED?: string;
|
|
WORKER_QUEUE_GROUP?: string;
|
|
};
|
|
|
|
export interface SearchIndexerWorkerHandle {
|
|
readonly name: string;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export interface SearchIndexerBootstrapDeps {
|
|
readonly logger?: SearchIndexerLogger;
|
|
readonly bulkPagers?: SearchIndexerDependencies["bulkPagers"];
|
|
readonly searchClient?: SearchClient;
|
|
/** Override the BullMQ worker constructor so tests can inject a fake. */
|
|
readonly createWorkerImpl?: (
|
|
queueName: string,
|
|
processor: (job: { data: unknown }) => Promise<unknown>,
|
|
options: { concurrency?: number }
|
|
) => SearchIndexerWorkerHandle;
|
|
}
|
|
|
|
export type SearchIndexerBootstrapResult =
|
|
| { readonly status: "disabled"; readonly reason: string }
|
|
| { readonly status: "started"; readonly handle: SearchIndexerWorkerHandle };
|
|
|
|
export function bootstrapSearchIndexer(
|
|
env: SearchIndexerBootstrapEnv,
|
|
deps: SearchIndexerBootstrapDeps = {}
|
|
): SearchIndexerBootstrapResult {
|
|
const logger = deps.logger;
|
|
|
|
if (env.WORKER_QUEUE_GROUP && env.WORKER_QUEUE_GROUP !== "general") {
|
|
return { status: "disabled", reason: "non-general worker group" };
|
|
}
|
|
if (env.SEARCH_INDEXER_ENABLED === "false") {
|
|
return { status: "disabled", reason: "SEARCH_INDEXER_ENABLED=false" };
|
|
}
|
|
if (!env.MEILI_URL) {
|
|
logger?.info?.({ component: "search-indexer" }, "search-indexer disabled: MEILI_URL unset");
|
|
return { status: "disabled", reason: "MEILI_URL unset" };
|
|
}
|
|
|
|
const client =
|
|
deps.searchClient ?? createSearchClient({ url: env.MEILI_URL, apiKey: env.MEILI_API_KEY ?? undefined });
|
|
|
|
const concurrency = parsePositiveInt(env.SEARCH_INDEXER_CONCURRENCY, 5);
|
|
|
|
const processor = async (job: { data: unknown }): Promise<unknown> => {
|
|
return runSearchIndexerJob(job.data, {
|
|
searchClient: client,
|
|
bulkPagers: deps.bulkPagers,
|
|
logger,
|
|
});
|
|
};
|
|
|
|
const createWorkerImpl = deps.createWorkerImpl ?? defaultCreateWorker;
|
|
const handle = createWorkerImpl(SEARCH_INDEX_OUTBOX_QUEUE, processor, { concurrency });
|
|
|
|
logger?.info?.(
|
|
{ component: "search-indexer", queue: SEARCH_INDEX_OUTBOX_QUEUE, concurrency },
|
|
"search-indexer worker started"
|
|
);
|
|
return { status: "started", handle };
|
|
}
|
|
|
|
function parsePositiveInt(value: unknown, fallback: number): number {
|
|
if (typeof value !== "string" || value.length === 0) return fallback;
|
|
const parsed = Number.parseInt(value, 10);
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
}
|
|
|
|
// In production this is replaced by a real BullMQ Worker via createWorkerImpl
|
|
// in the worker entry point. Calling it directly throws — tests must inject
|
|
// the dependency or the bootstrap is short-circuited via SEARCH_INDEXER_ENABLED.
|
|
const defaultCreateWorker: NonNullable<SearchIndexerBootstrapDeps["createWorkerImpl"]> = () => {
|
|
throw new Error(
|
|
"bootstrapSearchIndexer requires deps.createWorkerImpl in this build; the worker entry must wire the BullMQ Worker constructor."
|
|
);
|
|
};
|
|
|