Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/worker/src/connection.ts
T
2026-07-08 16:15:37 +02:00

95 lines
3.4 KiB
TypeScript

import type { EnvRecord } from "@hivegrid/config";
import { parseRedisUrl, type ConnectionOptions, type WorkerQueueGroup } from "@hivegrid/queue";
/**
* Per-group default Redis connection pool size. BullMQ uses ioredis under
* the hood — `family`/`tls`/pool tuning happen at ioredis level. We expose
* `pool` as an opt-in field BullMQ does not consume so the value can be
* read by integrations (custom factories, ioredis-cluster wrappers); the
* default fields stay 1:1 compatible with BullMQ's `WorkerOptions.connection`.
*
* Rationale per group:
* - general : 4 — mixed I/O, modest connection demand
* - mail : 4 — SMTP/HTTP, moderate
* - import : 2 — long-running but low connection turnover
* - webhook : 8 — high parallelism (concurrency 20+) needs more sockets
* - plugin : 4 — sandbox calls are short-lived; Redis is mostly BullMQ
* command traffic plus sliding-window rate limits
*/
export const DEFAULT_GROUP_POOL_SIZE: Readonly<Record<WorkerQueueGroup, number>> = {
general: 4,
mail: 4,
import: 2,
webhook: 8,
plugin: 4,
};
const POOL_ENV_VAR = "WORKER_REDIS_POOL_SIZE";
export type WorkerConnectionOptions = ConnectionOptions & {
/**
* Human-readable name BullMQ/ioredis tags the connection with. Visible in
* `CLIENT LIST` so operators can attribute Redis traffic to a worker
* group + PID. Format: `worker-${group}-${pid}`.
*/
connectionName: string;
/**
* Advisory pool size (not consumed by BullMQ directly). Read by callers
* that wrap BullMQ in a custom connection factory.
*/
pool: number;
};
export type CreateWorkerConnectionOptions = {
env?: EnvRecord;
pid?: number;
};
/**
* Builds the BullMQ connection options for a single worker process.
* Resolves the Redis URL from BULLMQ_REDIS_URL/REDIS_URL via the queue
* package's shared parser, then layers on a connection name + pool size
* derived from the group.
*
* Throws if neither BULLMQ_REDIS_URL nor REDIS_URL is configured — a
* worker without a Redis target can't function and should fail loud.
*/
export function createWorkerConnection(group: WorkerQueueGroup, options: CreateWorkerConnectionOptions = {}): WorkerConnectionOptions {
const env = options.env ?? (process.env as EnvRecord);
const pid = options.pid ?? process.pid;
const redisUrl = pickRedisUrl(env);
if (!redisUrl) {
throw new Error("Worker Redis URL is required: set BULLMQ_REDIS_URL or REDIS_URL.");
}
const base = parseRedisUrl(redisUrl.name, redisUrl.value);
return {
...base,
connectionName: `worker-${group}-${pid}`,
pool: resolvePoolSize(group, env),
};
}
function resolvePoolSize(group: WorkerQueueGroup, env: EnvRecord): number {
const raw = env[POOL_ENV_VAR];
if (typeof raw === "string" && raw.length > 0) {
const parsed = Number.parseInt(raw, 10);
if (Number.isFinite(parsed) && parsed > 0) {
return parsed;
}
}
return DEFAULT_GROUP_POOL_SIZE[group];
}
function pickRedisUrl(env: EnvRecord): { name: "BULLMQ_REDIS_URL" | "REDIS_URL"; value: string } | undefined {
const bullmq = trim(env.BULLMQ_REDIS_URL);
if (bullmq) return { name: "BULLMQ_REDIS_URL", value: bullmq };
const redis = trim(env.REDIS_URL);
if (redis) return { name: "REDIS_URL", value: redis };
return undefined;
}
function trim(value: string | undefined): string | undefined {
const trimmed = value?.trim();
return trimmed ? trimmed : undefined;
}