91 lines
3.6 KiB
TypeScript
91 lines
3.6 KiB
TypeScript
import type { EnvRecord } from "@hivegrid/config";
|
|
import { describePluginRuntimeProductionGate } from "@hivegrid/plugin-runtime";
|
|
|
|
import {
|
|
PLUGIN_CAPABILITY_USE_OUTBOX_QUEUE,
|
|
handlePluginCapabilityUseJob,
|
|
type PluginCapabilityUseLogger,
|
|
type PluginCapabilityUseStore,
|
|
} from "./plugin-capability-use.js";
|
|
|
|
export type PluginCapabilityUseBootstrapEnv = EnvRecord & {
|
|
WORKER_QUEUE_GROUP?: string;
|
|
PLUGIN_CAPABILITY_USE_ENABLED?: string;
|
|
PLUGIN_CAPABILITY_USE_CONCURRENCY?: string;
|
|
};
|
|
|
|
export interface PluginCapabilityUseWorkerHandle {
|
|
readonly name: string;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export interface PluginCapabilityUseBootstrapDeps {
|
|
readonly logger?: PluginCapabilityUseLogger;
|
|
readonly store?: PluginCapabilityUseStore;
|
|
readonly createWorkerImpl?: (
|
|
queueName: string,
|
|
processor: (job: { data: unknown }) => Promise<unknown>,
|
|
options: { concurrency?: number }
|
|
) => PluginCapabilityUseWorkerHandle;
|
|
}
|
|
|
|
export type PluginCapabilityUseBootstrapResult =
|
|
| { readonly status: "disabled"; readonly reason: string }
|
|
| { readonly status: "started"; readonly handle: PluginCapabilityUseWorkerHandle };
|
|
|
|
const DEFAULT_CONCURRENCY = 1;
|
|
|
|
export class PluginCapabilityUseRequiredBootstrapError extends Error {
|
|
constructor(readonly reason: string) {
|
|
super(`Plugin capability-use persistence is required but disabled: ${reason}`);
|
|
this.name = "PluginCapabilityUseRequiredBootstrapError";
|
|
}
|
|
}
|
|
|
|
export function isPluginCapabilityUseRequired(env: PluginCapabilityUseBootstrapEnv, workerGroup = env.WORKER_QUEUE_GROUP): boolean {
|
|
return workerGroup === "general" && env.NODE_ENV === "production" && describePluginRuntimeProductionGate(env) === null;
|
|
}
|
|
|
|
export function bootstrapPluginCapabilityUse(
|
|
env: PluginCapabilityUseBootstrapEnv,
|
|
deps: PluginCapabilityUseBootstrapDeps = {}
|
|
): PluginCapabilityUseBootstrapResult {
|
|
const logger = deps.logger;
|
|
const required = isPluginCapabilityUseRequired(env);
|
|
if (env.WORKER_QUEUE_GROUP && env.WORKER_QUEUE_GROUP !== "general") {
|
|
return { status: "disabled", reason: "non-general worker group" };
|
|
}
|
|
if (env.PLUGIN_CAPABILITY_USE_ENABLED === "false") {
|
|
return disabledOrRequired("PLUGIN_CAPABILITY_USE_ENABLED=false", required);
|
|
}
|
|
if (!deps.store) {
|
|
return disabledOrRequired("no capability-use store wired", required);
|
|
}
|
|
|
|
const store = deps.store;
|
|
const concurrency = parsePositiveInt(env.PLUGIN_CAPABILITY_USE_CONCURRENCY, DEFAULT_CONCURRENCY);
|
|
const processor = (job: { data: unknown }): Promise<unknown> => handlePluginCapabilityUseJob(job.data, { store, logger });
|
|
const createWorkerImpl = deps.createWorkerImpl ?? defaultCreateWorker;
|
|
const handle = createWorkerImpl(PLUGIN_CAPABILITY_USE_OUTBOX_QUEUE, processor, { concurrency });
|
|
|
|
logger?.info?.({ component: "plugin-capability-use", queue: PLUGIN_CAPABILITY_USE_OUTBOX_QUEUE, concurrency }, "plugin capability-use worker started");
|
|
return { status: "started", handle };
|
|
}
|
|
|
|
function disabledOrRequired(reason: string, required: boolean): { readonly status: "disabled"; readonly reason: string } {
|
|
if (required) {
|
|
throw new PluginCapabilityUseRequiredBootstrapError(reason);
|
|
}
|
|
return { status: "disabled", reason };
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
const defaultCreateWorker: NonNullable<PluginCapabilityUseBootstrapDeps["createWorkerImpl"]> = () => {
|
|
throw new Error("bootstrapPluginCapabilityUse requires deps.createWorkerImpl in this build; the worker entry must wire the BullMQ Worker constructor.");
|
|
};
|