Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+90
@@ -0,0 +1,90 @@
|
||||
import type { EnvRecord } from "@toolctrl/config";
|
||||
import { describePluginRuntimeProductionGate } from "@toolctrl/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.");
|
||||
};
|
||||
Reference in new issue
Block a user