@brickkit/redis-world 0.12.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/setup.js +0 -2
- package/dist/cli-BEHfAdNK.js +65 -0
- package/dist/cli-BEHfAdNK.js.map +1 -0
- package/dist/cli.d.ts +10 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1 -49
- package/dist/index.d.ts +5 -119
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -234
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +5 -81
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +4 -3
- package/dist/keys-CqKp_Amk.js +104 -0
- package/dist/keys-CqKp_Amk.js.map +1 -0
- package/dist/{shared-Bhvu4aZA.js → world-783VJfKL.js} +354 -247
- package/dist/world-783VJfKL.js.map +1 -0
- package/dist/world-BDW_evdb.d.ts +242 -0
- package/dist/world-BDW_evdb.d.ts.map +1 -0
- package/package.json +1 -1
- package/dist/cli.js.map +0 -1
- package/dist/env-DMwDVTiZ.js +0 -18
- package/dist/env-DMwDVTiZ.js.map +0 -1
- package/dist/reaper-SOJDwnBw.d.ts +0 -45
- package/dist/reaper-SOJDwnBw.d.ts.map +0 -1
- package/dist/shared-Bhvu4aZA.js.map +0 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["delay"],"sources":["../src/queue.ts","../src/index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { setTimeout as delay } from \"node:timers/promises\";\nimport {\n MessageId,\n parseQueueName,\n parseQueueRoute,\n ValidQueueName,\n type Queue as QueueInterface,\n type QueuePrefix,\n} from \"@workflow/world\";\nimport {\n createAppRouteUrl,\n createWorkflowBaseUrl,\n createWorkflowUrl,\n} from \"@workflow/utils\";\nimport type { Redis } from \"ioredis\";\nimport { monotonicFactory } from \"ulid\";\nimport type { RedisWorldConfig } from \"./config.js\";\nimport { initialMessageKey } from \"./run-id.js\";\nimport { debug, hasBinary, uint8ArrayReplacer } from \"./util.js\";\n\nexport interface QueueStats {\n waiting: number;\n active: number;\n delayed: number;\n /** Completed and failed delivery records are not retained. */\n completed: number;\n failed: number;\n}\n\ninterface Envelope {\n messageId: MessageId;\n queueName: ValidQueueName;\n message: string;\n attempt: number;\n deliveryCount: number;\n createdAt: number;\n dedupKey: string;\n headers: Record<string, string>;\n route?: string;\n}\n\n// Derived from the upstream reliable Redis list queue. Every transition is a\n// single script: a process crash cannot leave a message between structures.\nconst ENQUEUE = `\n if ARGV[1] ~= '' then\n local existing = redis.call('GET', KEYS[1])\n if existing then return existing end\n redis.call('SET', KEYS[1], ARGV[2])\n if tonumber(ARGV[3]) > 0 then redis.call('PEXPIRE', KEYS[1], ARGV[3]) end\n end\n if tonumber(ARGV[5]) > 0 then\n redis.call('ZADD', KEYS[3], ARGV[5], ARGV[4])\n else\n redis.call('LPUSH', KEYS[2], ARGV[4])\n end\n return ARGV[2]\n`;\nconst RESERVE = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return nil end\n local raw = redis.call('LINDEX', KEYS[1], -1)\n if not raw then return nil end\n local job = cjson.decode(raw)\n job.attempt = job.attempt + 1\n job.deliveryCount = job.deliveryCount + 1\n local claim = cjson.encode(job)\n redis.call('RPOP', KEYS[1])\n redis.call('LPUSH', KEYS[2], claim)\n return claim\n`;\nconst ACK = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return 0 end\n if redis.call('LREM', KEYS[1], 1, ARGV[1]) == 0 then return 0 end\n if ARGV[2] ~= '' and redis.call('GET', KEYS[2]) == ARGV[3] then\n redis.call('DEL', KEYS[2])\n end\n return 1\n`;\nconst DEFER = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return 0 end\n if redis.call('LREM', KEYS[1], 1, ARGV[1]) == 0 then return 0 end\n redis.call('ZADD', KEYS[2], ARGV[3], ARGV[2])\n return 1\n`;\nconst PROMOTE = `\n local due = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, 100)\n for _, raw in ipairs(due) do\n redis.call('LPUSH', KEYS[2], raw)\n redis.call('ZREM', KEYS[1], raw)\n end\n return #due\n`;\nconst RECLAIM = `\n if redis.call('EXISTS', KEYS[3]) == 1 then return 0 end\n local moved = 0\n for i = 1, 100 do\n local raw = redis.call('RPOPLPUSH', KEYS[1], KEYS[2])\n if not raw then break end\n moved = moved + 1\n end\n if redis.call('LLEN', KEYS[1]) == 0 then redis.call('SREM', KEYS[4], ARGV[1]) end\n return moved\n`;\nconst RENEW = `\n if redis.call('EXISTS', KEYS[1]) == 0 then return 0 end\n redis.call('PEXPIRE', KEYS[1], ARGV[1])\n return 1\n`;\nconst REGISTER = `\n redis.call('SET', KEYS[1], '1', 'PX', ARGV[2])\n redis.call('SADD', KEYS[2], ARGV[1])\n`;\nconst STATS = `\n local active = 0\n for _, owner in ipairs(redis.call('SMEMBERS', KEYS[3])) do\n active = active + redis.call('LLEN', ARGV[1] .. ':processing:' .. owner)\n end\n return {redis.call('LLEN', KEYS[1]), active, redis.call('ZCARD', KEYS[2])}\n`;\n\nfunction parseEnvelope(raw: string): Envelope {\n const value: unknown = JSON.parse(raw);\n if (\n value === null ||\n typeof value !== \"object\" ||\n !(\"messageId\" in value) ||\n !(\"queueName\" in value) ||\n !(\"message\" in value) ||\n typeof value.message !== \"string\" ||\n !(\"attempt\" in value) ||\n typeof value.attempt !== \"number\" ||\n !(\"deliveryCount\" in value) ||\n typeof value.deliveryCount !== \"number\" ||\n !(\"createdAt\" in value) ||\n typeof value.createdAt !== \"number\" ||\n !(\"dedupKey\" in value) ||\n typeof value.dedupKey !== \"string\" ||\n !(\"headers\" in value) ||\n value.headers === null ||\n typeof value.headers !== \"object\"\n ) {\n throw new TypeError(\"Invalid Redis queue envelope\");\n }\n const headers: Record<string, string> = {};\n for (const [key, header] of Object.entries(value.headers)) {\n if (typeof header !== \"string\") throw new TypeError(\"Invalid queue header\");\n headers[key] = header;\n }\n return {\n messageId: MessageId.parse(value.messageId),\n queueName: ValidQueueName.parse(value.queueName),\n message: value.message,\n attempt: value.attempt,\n deliveryCount: value.deliveryCount,\n createdAt: value.createdAt,\n dedupKey: value.dedupKey,\n headers,\n ...(\"route\" in value &&\n typeof value.route === \"string\" && { route: value.route }),\n };\n}\n\nfunction serializeQueueMessage(message: unknown): string {\n return hasBinary(message)\n ? JSON.stringify(message, uint8ArrayReplacer)\n : JSON.stringify(message);\n}\n\nfunction queueMessageReviver(_key: string, value: unknown): unknown {\n if (value !== null && typeof value === \"object\") {\n const tagged = value as { __type?: unknown; data?: unknown };\n if (tagged.__type === \"Uint8Array\" && typeof tagged.data === \"string\") {\n return new Uint8Array(Buffer.from(tagged.data, \"base64\"));\n }\n }\n return value;\n}\n\nfunction deserializeQueueMessage(text: string): unknown {\n // Queue messages carry no Date fields, so with the tag absent a plain parse\n // is exactly equivalent.\n return text.includes('\"__type\"')\n ? JSON.parse(text, queueMessageReviver)\n : JSON.parse(text);\n}\n\nfunction resolveBaseUrl(config: RedisWorldConfig): string {\n return (\n config.baseUrl ||\n process.env.WORKFLOW_BASE_URL ||\n `http://localhost:${process.env.PORT ?? \"3000\"}`\n );\n}\n\nexport function createQueue(\n redis: Redis,\n config: RedisWorldConfig,\n options: {\n /**\n * Whether a run has moved past `pending`. The first message of a run is not\n * queued again once it has, see `initialMessageKey`.\n */\n runHasStarted?: (runId: string) => Promise<boolean>;\n } = {},\n): QueueInterface & {\n start(): Promise<void>;\n close(): Promise<void>;\n getQueueStats(): Promise<QueueStats>;\n} {\n const generateMessageId = monotonicFactory();\n const root = `${config.keyPrefix || \"workflow:\"}queue:flows`;\n const ready = root;\n const delayed = `${root}:delayed`;\n const owners = `${root}:workers`;\n const processing = (owner: string) => `${root}:processing:${owner}`;\n const heartbeat = (owner: string) => `${root}:heartbeat:${owner}`;\n const heartbeatTtlMs = config.heartbeatTtlMs ?? 90_000;\n const concurrency = config.queueConcurrency ?? 10;\n const maxAttempts = config.maxAttempts ?? 73;\n const maxBackoffMs = config.maxBackoffMs ?? 30_000;\n const backoffDelayMs = config.backoffDelayMs ?? 1000;\n const httpTimeoutMs = config.httpTimeoutMs ?? 300_000;\n for (const [name, value, minimum] of [\n [\"heartbeatTtlMs\", heartbeatTtlMs, 300],\n [\"queueConcurrency\", concurrency, 1],\n [\"maxAttempts\", maxAttempts, 49],\n [\"maxBackoffMs\", maxBackoffMs, 1],\n [\"backoffDelayMs\", backoffDelayMs, 0],\n [\"httpTimeoutMs\", httpTimeoutMs, 1],\n ...(config.idempotencyTtlMs === undefined\n ? []\n : [[\"idempotencyTtlMs\", config.idempotencyTtlMs, 1]]),\n ] as const) {\n if (\n typeof value !== \"number\" ||\n typeof minimum !== \"number\" ||\n !Number.isSafeInteger(value) ||\n value < minimum ||\n value > 2_147_483_647\n ) {\n throw new RangeError(\n `${name} must be an integer between ${minimum} and 2147483647`,\n );\n }\n }\n const directHandlers = new Map<\n QueuePrefix,\n (req: Request) => Promise<Response>\n >();\n const routes = new Map<QueuePrefix, string>();\n let closed = false;\n let startPromise: Promise<void> | undefined;\n let loop: Promise<void> | undefined;\n let owner: string | undefined;\n const inflight = new Set<Promise<void>>();\n const lostOwners = new Set<string>();\n const abort = new AbortController();\n const sleep = () =>\n delay(50, undefined, { signal: abort.signal }).catch(() => {});\n\n const queue: QueueInterface[\"queue\"] = async (queueName, message, opts) => {\n if (closed) throw new Error(\"Queue is closed\");\n const { prefix } = parseQueueName(queueName);\n const messageId = MessageId.parse(`msg_${generateMessageId()}`);\n const idempotencyKey = opts?.idempotencyKey ?? initialMessageKey(message);\n if (\n !opts?.idempotencyKey &&\n idempotencyKey &&\n (await options.runHasStarted?.((message as { runId: string }).runId))\n ) {\n debug(\"run already started, not queueing its first message again\", {\n runId: (message as { runId: string }).runId,\n });\n return { messageId };\n }\n const dedupKey = idempotencyKey\n ? `${root}:idempotent:${idempotencyKey}`\n : \"\";\n const envelope: Envelope = {\n messageId,\n queueName,\n message: serializeQueueMessage(message),\n attempt: 0,\n deliveryCount: 0,\n createdAt: Date.now(),\n dedupKey,\n headers: opts?.headers ?? {},\n ...(routes.has(prefix) && { route: routes.get(prefix) }),\n };\n const delayMs = Math.max(0, (opts?.delaySeconds ?? 0) * 1000);\n if (!Number.isFinite(delayMs))\n throw new RangeError(\"delaySeconds must be finite\");\n const result = await redis.eval(\n ENQUEUE,\n 3,\n dedupKey || ready,\n ready,\n delayed,\n dedupKey,\n messageId,\n config.idempotencyTtlMs ?? 0,\n JSON.stringify(envelope),\n delayMs > 0 ? Date.now() + delayMs : 0,\n );\n return { messageId: MessageId.parse(result) };\n };\n\n function parseReschedule(text: string): number | undefined {\n try {\n const seconds: unknown = JSON.parse(text)?.timeoutSeconds;\n if (\n typeof seconds === \"number\" &&\n Number.isFinite(seconds) &&\n seconds >= 0\n )\n return seconds;\n } catch {\n /* A response without a timeout acknowledges the message. */\n }\n return undefined;\n }\n\n async function dispatch(job: Envelope): Promise<number | undefined> {\n const { prefix: queuePrefix } = parseQueueName(job.queueName);\n const messageId = job.messageId;\n const headers = new Headers(job.headers);\n headers.set(\"content-type\", \"application/json\");\n headers.set(\"x-vqs-queue-name\", job.queueName);\n headers.set(\"x-vqs-message-id\", messageId);\n // Reservation persists attempts before dispatch, including crash redeliveries.\n headers.set(\"x-vqs-message-attempt\", String(job.attempt));\n // Every delivery that reached a handler, reschedules included.\n headers.set(\"x-vqs-message-delivery-count\", String(job.deliveryCount));\n headers.set(\n \"x-vqs-message-created-at\",\n new Date(job.createdAt).toISOString(),\n );\n\n const deliveryRoute = routes.get(queuePrefix) ?? job.route;\n const directHandler = directHandlers.get(queuePrefix);\n\n let response: Response;\n if (directHandler) {\n // In process: the request a route serving `createQueueHandler(prefix, ...)`\n // would receive, without the network hop.\n const localBase = createWorkflowBaseUrl(\"http://localhost\");\n response = await directHandler(\n new Request(\n deliveryRoute\n ? createAppRouteUrl(localBase, deliveryRoute)\n : createWorkflowUrl(localBase, { type: \"flow\" }),\n { method: \"POST\", headers, body: job.message },\n ),\n );\n } else {\n const baseUrl = resolveBaseUrl(config);\n response = await fetch(\n deliveryRoute\n ? createAppRouteUrl(baseUrl, deliveryRoute)\n : createWorkflowUrl(baseUrl, { type: \"flow\" }),\n {\n method: \"POST\",\n headers,\n body: job.message,\n signal: AbortSignal.timeout(httpTimeoutMs),\n },\n );\n }\n\n const text = await response.text();\n\n if (!response.ok) throw new Error(`HTTP ${response.status}: ${text}`);\n return parseReschedule(text);\n }\n\n const createQueueHandler: QueueInterface[\"createQueueHandler\"] = (\n queueNamePrefix,\n handler,\n ) => {\n return async (req) => {\n const queueName = req.headers.get(\n \"x-vqs-queue-name\",\n ) as ValidQueueName | null;\n const messageId = req.headers.get(\"x-vqs-message-id\") as MessageId | null;\n const attemptStr = req.headers.get(\"x-vqs-message-attempt\");\n const deliveryCountStr = req.headers.get(\"x-vqs-message-delivery-count\");\n const createdAtStr = req.headers.get(\"x-vqs-message-created-at\");\n\n if (!queueName || !messageId || !attemptStr || !req.body) {\n return Response.json(\n { error: \"Missing required headers or body\" },\n { status: 400 },\n );\n }\n if (!queueName.startsWith(queueNamePrefix)) {\n return Response.json({ error: \"Unhandled queue\" }, { status: 400 });\n }\n\n const attempt = Number.parseInt(attemptStr, 10);\n const deliveryCount = deliveryCountStr\n ? Number.parseInt(deliveryCountStr, 10)\n : attempt;\n const createdAt = createdAtStr ? new Date(createdAtStr) : undefined;\n\n try {\n const body = deserializeQueueMessage(await req.text());\n const result = await handler(body, {\n attempt,\n deliveryCount,\n ...(createdAt && !Number.isNaN(createdAt.getTime()) && { createdAt }),\n queueName,\n messageId,\n });\n\n const timeoutSeconds = (\n result as { timeoutSeconds?: unknown } | null | undefined\n )?.timeoutSeconds;\n if (\n typeof timeoutSeconds === \"number\" &&\n Number.isFinite(timeoutSeconds)\n ) {\n return Response.json({ timeoutSeconds: Math.max(0, timeoutSeconds) });\n }\n return Response.json({ ok: true });\n } catch (error) {\n debug(\"queue handler error:\", error);\n return Response.json({ error: String(error) }, { status: 500 });\n }\n };\n };\n\n const getDeploymentId: QueueInterface[\"getDeploymentId\"] = async () =>\n \"redis\";\n\n async function settle(raw: string, claimOwner: string): Promise<void> {\n try {\n const job = parseEnvelope(raw);\n let seconds: number | undefined;\n let failed = false;\n try {\n seconds = await dispatch(job);\n } catch (error) {\n failed = true;\n debug(\n `Delivery ${job.messageId} attempt ${job.attempt} failed:`,\n error,\n );\n const curve =\n config.backoffType === \"fixed\"\n ? backoffDelayMs\n : backoffDelayMs * 2 ** Math.min(30, job.attempt - 1);\n seconds =\n (job.attempt >= maxAttempts\n ? maxBackoffMs\n : Math.min(maxBackoffMs, curve)) / 1000;\n if (job.attempt === maxAttempts)\n console.error(\n `Queue message ${job.messageId} exhausted maxAttempts; retaining it for capped retries`,\n );\n }\n if (seconds === undefined) {\n await redis.eval(\n ACK,\n 3,\n processing(claimOwner),\n job.dedupKey || ready,\n heartbeat(claimOwner),\n raw,\n job.dedupKey,\n job.messageId,\n );\n } else {\n // Only successful reschedules preserve the attempt. A crash always\n // leaves the reserved attempt in Redis, so reclaim counts as failure.\n const next = failed ? job : { ...job, attempt: job.attempt - 1 };\n await redis.eval(\n DEFER,\n 3,\n processing(claimOwner),\n delayed,\n heartbeat(claimOwner),\n raw,\n JSON.stringify(next),\n Date.now() + seconds * 1000,\n );\n }\n } catch (error) {\n // Keep the claim for reclaim on connection loss or malformed data.\n lostOwners.add(claimOwner);\n console.error(\"Queue settlement error:\", error);\n }\n }\n\n async function register(): Promise<string> {\n const next = randomUUID();\n await redis.eval(\n REGISTER,\n 2,\n heartbeat(next),\n owners,\n next,\n heartbeatTtlMs,\n );\n return next;\n }\n\n async function run(): Promise<void> {\n let renewedAt = Date.now();\n let maintainedAt = 0;\n while (!closed) {\n try {\n for (const lost of lostOwners) {\n await redis.del(heartbeat(lost));\n lostOwners.delete(lost);\n if (owner === lost) owner = undefined;\n }\n if (!owner || Date.now() - renewedAt >= heartbeatTtlMs / 3) {\n // Never resurrect an expired lease: its processing list may already\n // belong to the reclaimer. A fresh UUID fences all old settlements.\n if (\n !owner ||\n (await redis.eval(RENEW, 1, heartbeat(owner), heartbeatTtlMs)) !== 1\n )\n owner = await register();\n renewedAt = Date.now();\n }\n if (Date.now() - maintainedAt >= 100) {\n await redis.eval(PROMOTE, 2, delayed, ready, Date.now());\n for (const stale of await redis.smembers(owners)) {\n if (stale !== owner)\n await redis.eval(\n RECLAIM,\n 4,\n processing(stale),\n ready,\n heartbeat(stale),\n owners,\n stale,\n );\n }\n maintainedAt = Date.now();\n }\n for (\n let reserved = 0;\n reserved < concurrency &&\n !closed &&\n owner &&\n inflight.size < concurrency;\n reserved++\n ) {\n const claimOwner = owner;\n const raw = await redis.eval(\n RESERVE,\n 3,\n ready,\n processing(claimOwner),\n heartbeat(claimOwner),\n );\n if (typeof raw !== \"string\") break;\n const task = settle(raw, claimOwner);\n inflight.add(task);\n void task.finally(() => inflight.delete(task));\n }\n } catch (error) {\n if (!closed) debug(\"Queue polling error:\", error);\n }\n await sleep();\n }\n }\n\n return {\n queue,\n createQueueHandler,\n getDeploymentId,\n registerHandler(prefix, handler) {\n directHandlers.set(prefix, handler);\n },\n registerRoute(prefix, path) {\n routes.set(prefix, parseQueueRoute(path));\n },\n async getQueueStats() {\n const counts = await redis.eval(STATS, 3, ready, delayed, owners, root);\n if (\n !Array.isArray(counts) ||\n counts.length !== 3 ||\n counts.some((c) => typeof c !== \"number\")\n )\n throw new TypeError(\"Invalid queue statistics\");\n return {\n waiting: Number(counts[0]),\n active: Number(counts[1]),\n delayed: Number(counts[2]),\n completed: 0,\n failed: 0,\n };\n },\n start() {\n if (closed) return Promise.reject(new Error(\"Queue is closed\"));\n startPromise ??= (async () => {\n owner = await register();\n loop = run();\n })().catch((error) => {\n startPromise = undefined;\n throw error;\n });\n return startPromise;\n },\n async close() {\n if (closed) return;\n closed = true;\n abort.abort();\n await startPromise;\n await loop;\n // While handlers drain, keep the lease alive. close() must not permit a\n // live long-running handler to be reclaimed merely because shutdown began.\n const timer = setInterval(\n () => {\n if (owner)\n void redis\n .eval(RENEW, 1, heartbeat(owner), heartbeatTtlMs)\n .catch((error) => debug(\"Queue close heartbeat:\", error));\n },\n Math.floor(heartbeatTtlMs / 3),\n );\n try {\n await Promise.all(inflight);\n } finally {\n clearInterval(timer);\n }\n if (owner) {\n await redis.del(heartbeat(owner));\n await redis.eval(\n RECLAIM,\n 4,\n processing(owner),\n ready,\n heartbeat(owner),\n owners,\n owner,\n );\n }\n },\n };\n}\n","import {\n mintedSpecVersion,\n reenqueueActiveRuns,\n type World,\n} from \"@workflow/world\";\nimport { WorkflowRunNotFoundError } from \"@workflow/errors\";\nimport { Redis } from \"ioredis\";\nimport type { RedisWorldConfig } from \"./config.js\";\nimport { createQueue, type QueueStats } from \"./queue.js\";\nimport { createReaper, hasRetention, validateRetention } from \"./reaper.js\";\nimport { createRunId } from \"./run-id.js\";\nimport { createStorage } from \"./shared.js\";\nimport { createStreamer } from \"./streamer.js\";\n\nexport function createWorld(\n config: RedisWorldConfig = {\n redis:\n process.env.WORKFLOW_REDIS_URL ||\n process.env.REDIS_URL ||\n \"redis://localhost:6379\",\n\n queueConcurrency:\n Number.parseInt(\n process.env.WORKFLOW_REDIS_WORKER_CONCURRENCY || \"10\",\n 10,\n ) || 10,\n keyPrefix: process.env.WORKFLOW_REDIS_KEY_PREFIX || \"workflow:\",\n },\n): World & {\n start(): Promise<void>;\n getQueueStats(): Promise<QueueStats>;\n /** Run one retention sweep now; returns the runs deleted. 0 without `retention`. */\n sweepRetention(): Promise<number>;\n} {\n // Batches commands issued in the same event-loop tick into one write. Does\n // nothing for a single serial run, but collapses syscalls under concurrency:\n // measured 1.75x at 256 concurrent invocations, 1.15x at 32, a slight loss\n // at 1. Workers default to 10 and go higher under load, so default on.\n const autoPipelining = config.enableAutoPipelining ?? true;\n const redis =\n typeof config.redis === \"string\"\n ? new Redis(config.redis, { enableAutoPipelining: autoPipelining })\n : new Redis({ enableAutoPipelining: autoPipelining, ...config.redis });\n\n const keyPrefix = config.keyPrefix || \"workflow:\";\n validateRetention(config.retention);\n const reaper = hasRetention(config.retention)\n ? createReaper({ redis, keyPrefix, retention: config.retention })\n : undefined;\n\n const storage = createStorage(redis, keyPrefix, config.maxEventsPerRun);\n const queue = createQueue(redis, config, {\n runHasStarted: async (runId) => {\n try {\n const run = await storage.runs.get(runId, { resolveData: \"none\" });\n return run.status !== \"pending\";\n } catch (error) {\n if (WorkflowRunNotFoundError.is(error)) return false;\n throw error;\n }\n },\n });\n const streamer = createStreamer({ redis, keyPrefix });\n\n return {\n ...storage,\n ...streamer,\n ...queue,\n // Declares support for the CBOR/binary-safe queue transport so core\n // attaches runInput to workflow messages (resilient start). The queue\n // serializes payloads with a tagged-JSON transport that round-trips\n // Uint8Array values through Redis queue envelopes. A function, not a constant:\n // the version this world stamps follows `WORKFLOW_SEALED_LOG`.\n specVersion: mintedSpecVersion(),\n createRunId,\n async start() {\n await queue.start();\n reaper?.start();\n // Put runs that were in flight back on the queue, in case the queue lost\n // them (a Redis restart without persistence, a failover). Replay is\n // idempotent, so a duplicate is harmless; the dedup key makes several\n // workers restarting together enqueue one message per run, not one each.\n await reenqueueActiveRuns(\n storage.runs,\n (queueName, message, opts) =>\n queue.queue(queueName, message, {\n ...opts,\n idempotencyKey: `recover:${(message as { runId: string }).runId}`,\n }),\n \"world-redis\",\n );\n },\n async sweepRetention() {\n return reaper ? reaper.sweep() : 0;\n },\n async close() {\n await reaper?.stop();\n await queue.close();\n await streamer.close();\n await redis.quit();\n },\n };\n}\n\n// Re-export config and queue types for consumers\nexport type { RedisWorldConfig } from \"./config.js\";\nexport type { QueueStats } from \"./queue.js\";\nexport type { KeepRuns, RunRetention } from \"./reaper.js\";\n"],"mappings":";;;;;;;;;AA4CA,MAAM,UAAU;;;;;;;;;;;;;;AAchB,MAAM,UAAU;;;;;;;;;;;;AAYhB,MAAM,MAAM;;;;;;;;AAQZ,MAAM,QAAQ;;;;;;AAMd,MAAM,U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AAM,IAAI;EACnC;EACA,MAAM,QAAQ;GACZ,MAAM,QAAQ,KAAK;GACnB,MAAM,MAAM,MAAM;GAClB,MAAM,SAAS,MAAM;GACrB,MAAM,MAAM,KAAK;EACnB;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["delay"],"sources":["../src/queue.ts","../src/index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { setTimeout as delay } from \"node:timers/promises\";\nimport { MessageId, ValidQueueName } from \"@workflow/world\";\nimport type { Redis } from \"ioredis\";\nimport {\n checkInteger,\n type RedisWorldConfig,\n type ResolvedConfig,\n} from \"./config.js\";\nimport { keys } from \"./keys.js\";\nimport { debug } from \"./util.js\";\nimport {\n createWorldQueue,\n retryDelayMs,\n type Deliver,\n type Delivery,\n type QueueBackend,\n type WorldQueue,\n type WorldQueueOptions,\n} from \"./world-queue.js\";\n\nexport type { QueueStats } from \"./world-queue.js\";\n\ninterface Envelope extends Delivery {\n dedupKey: string;\n headers: Record<string, string>;\n}\n\n// Derived from the upstream reliable Redis list queue. Every transition is a\n// single script: a process crash cannot leave a message between structures.\nconst ENQUEUE = `\n if ARGV[1] ~= '' then\n local existing = redis.call('GET', KEYS[1])\n if existing then return existing end\n redis.call('SET', KEYS[1], ARGV[2])\n if tonumber(ARGV[3]) > 0 then redis.call('PEXPIRE', KEYS[1], ARGV[3]) end\n end\n if tonumber(ARGV[5]) > 0 then\n redis.call('ZADD', KEYS[3], ARGV[5], ARGV[4])\n else\n redis.call('LPUSH', KEYS[2], ARGV[4])\n end\n return ARGV[2]\n`;\nconst RESERVE = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return nil end\n local raw = redis.call('LINDEX', KEYS[1], -1)\n if not raw then return nil end\n local job = cjson.decode(raw)\n job.attempt = job.attempt + 1\n job.deliveryCount = job.deliveryCount + 1\n local claim = cjson.encode(job)\n redis.call('RPOP', KEYS[1])\n redis.call('LPUSH', KEYS[2], claim)\n return claim\n`;\nconst ACK = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return 0 end\n if redis.call('LREM', KEYS[1], 1, ARGV[1]) == 0 then return 0 end\n if ARGV[2] ~= '' and redis.call('GET', KEYS[2]) == ARGV[3] then\n redis.call('DEL', KEYS[2])\n end\n return 1\n`;\nconst DEFER = `\n if redis.call('EXISTS', KEYS[3]) == 0 then return 0 end\n if redis.call('LREM', KEYS[1], 1, ARGV[1]) == 0 then return 0 end\n redis.call('ZADD', KEYS[2], ARGV[3], ARGV[2])\n return 1\n`;\nconst PROMOTE = `\n local due = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, 100)\n for _, raw in ipairs(due) do\n redis.call('LPUSH', KEYS[2], raw)\n redis.call('ZREM', KEYS[1], raw)\n end\n return #due\n`;\nconst RECLAIM = `\n if redis.call('EXISTS', KEYS[3]) == 1 then return 0 end\n local moved = 0\n for i = 1, 100 do\n local raw = redis.call('RPOPLPUSH', KEYS[1], KEYS[2])\n if not raw then break end\n moved = moved + 1\n end\n if redis.call('LLEN', KEYS[1]) == 0 then redis.call('SREM', KEYS[4], ARGV[1]) end\n return moved\n`;\nconst RENEW = `\n if redis.call('EXISTS', KEYS[1]) == 0 then return 0 end\n redis.call('PEXPIRE', KEYS[1], ARGV[1])\n return 1\n`;\nconst REGISTER = `\n redis.call('SET', KEYS[1], '1', 'PX', ARGV[2])\n redis.call('SADD', KEYS[2], ARGV[1])\n`;\n// ARGV[1] .. owner is the owner's processing list: `keys().queue.processing(\"\")`.\nconst STATS = `\n local active = 0\n for _, owner in ipairs(redis.call('SMEMBERS', KEYS[3])) do\n active = active + redis.call('LLEN', ARGV[1] .. owner)\n end\n return {redis.call('LLEN', KEYS[1]), active, redis.call('ZCARD', KEYS[2])}\n`;\n\nfunction parseEnvelope(raw: string): Envelope {\n const value: unknown = JSON.parse(raw);\n if (\n value === null ||\n typeof value !== \"object\" ||\n !(\"messageId\" in value) ||\n !(\"queueName\" in value) ||\n !(\"message\" in value) ||\n typeof value.message !== \"string\" ||\n !(\"attempt\" in value) ||\n typeof value.attempt !== \"number\" ||\n !(\"deliveryCount\" in value) ||\n typeof value.deliveryCount !== \"number\" ||\n !(\"createdAt\" in value) ||\n typeof value.createdAt !== \"number\" ||\n !(\"dedupKey\" in value) ||\n typeof value.dedupKey !== \"string\" ||\n !(\"headers\" in value) ||\n value.headers === null ||\n typeof value.headers !== \"object\"\n ) {\n throw new TypeError(\"Invalid Redis queue envelope\");\n }\n const headers: Record<string, string> = {};\n for (const [key, header] of Object.entries(value.headers)) {\n if (typeof header !== \"string\") throw new TypeError(\"Invalid queue header\");\n headers[key] = header;\n }\n return {\n messageId: MessageId.parse(value.messageId),\n queueName: ValidQueueName.parse(value.queueName),\n message: value.message,\n attempt: value.attempt,\n deliveryCount: value.deliveryCount,\n createdAt: value.createdAt,\n dedupKey: value.dedupKey,\n headers,\n ...(\"route\" in value &&\n typeof value.route === \"string\" && { route: value.route }),\n };\n}\n\nexport function createQueue(\n redis: Redis,\n config: ResolvedConfig<RedisWorldConfig>,\n options: WorldQueueOptions = {},\n): WorldQueue {\n const queueKeys = keys(config.keyPrefix).queue;\n const { ready, delayed, workers: owners, processing, heartbeat } = queueKeys;\n const heartbeatTtlMs = config.heartbeatTtlMs ?? 90_000;\n checkInteger(\"heartbeatTtlMs\", heartbeatTtlMs, 300);\n const { queueConcurrency: concurrency, maxAttempts, maxBackoffMs } = config;\n let closed = false;\n let startPromise: Promise<void> | undefined;\n let loop: Promise<void> | undefined;\n let owner: string | undefined;\n const inflight = new Set<Promise<void>>();\n const lostOwners = new Set<string>();\n const abort = new AbortController();\n const sleep = () =>\n delay(50, undefined, { signal: abort.signal }).catch(() => {});\n\n async function enqueue(\n message: Parameters<QueueBackend[\"enqueue\"]>[0],\n opts: Parameters<QueueBackend[\"enqueue\"]>[1],\n ): Promise<MessageId> {\n const dedupKey = opts.idempotencyKey\n ? queueKeys.idempotent(opts.idempotencyKey)\n : \"\";\n const envelope: Envelope = {\n messageId: message.messageId,\n queueName: message.queueName,\n message: message.message,\n attempt: 0,\n deliveryCount: 0,\n createdAt: Date.now(),\n dedupKey,\n headers: message.headers ?? {},\n ...(message.route && { route: message.route }),\n };\n const delayMs = Math.max(0, (opts.delaySeconds ?? 0) * 1000);\n if (!Number.isFinite(delayMs))\n throw new RangeError(\"delaySeconds must be finite\");\n const result = await redis.eval(\n ENQUEUE,\n 3,\n dedupKey || ready,\n ready,\n delayed,\n dedupKey,\n message.messageId,\n config.idempotencyTtlMs ?? 0,\n JSON.stringify(envelope),\n delayMs > 0 ? Date.now() + delayMs : 0,\n );\n return MessageId.parse(result);\n }\n\n async function settle(\n raw: string,\n claimOwner: string,\n deliver: Deliver,\n ): Promise<void> {\n try {\n const job = parseEnvelope(raw);\n let seconds: number | undefined;\n let failed = false;\n try {\n // Reservation persists the attempt before delivery, so a crash\n // redelivery reports one more.\n seconds = await deliver(job);\n } catch (error) {\n failed = true;\n debug(\n `Delivery ${job.messageId} attempt ${job.attempt} failed:`,\n error,\n );\n seconds =\n (job.attempt >= maxAttempts\n ? maxBackoffMs\n : retryDelayMs(job.attempt, config)) / 1000;\n if (job.attempt === maxAttempts)\n console.error(\n `Queue message ${job.messageId} exhausted maxAttempts; retaining it for capped retries`,\n );\n }\n if (seconds === undefined) {\n await redis.eval(\n ACK,\n 3,\n processing(claimOwner),\n job.dedupKey || ready,\n heartbeat(claimOwner),\n raw,\n job.dedupKey,\n job.messageId,\n );\n } else {\n // Only successful reschedules preserve the attempt. A crash always\n // leaves the reserved attempt in Redis, so reclaim counts as failure.\n const next = failed ? job : { ...job, attempt: job.attempt - 1 };\n await redis.eval(\n DEFER,\n 3,\n processing(claimOwner),\n delayed,\n heartbeat(claimOwner),\n raw,\n JSON.stringify(next),\n Date.now() + seconds * 1000,\n );\n }\n } catch (error) {\n // Keep the claim for reclaim on connection loss or malformed data.\n lostOwners.add(claimOwner);\n console.error(\"Queue settlement error:\", error);\n }\n }\n\n async function register(): Promise<string> {\n const next = randomUUID();\n await redis.eval(\n REGISTER,\n 2,\n heartbeat(next),\n owners,\n next,\n heartbeatTtlMs,\n );\n return next;\n }\n\n async function run(deliver: Deliver): Promise<void> {\n let renewedAt = Date.now();\n let maintainedAt = 0;\n let failing = false;\n while (!closed) {\n try {\n for (const lost of lostOwners) {\n await redis.del(heartbeat(lost));\n lostOwners.delete(lost);\n if (owner === lost) owner = undefined;\n }\n if (!owner || Date.now() - renewedAt >= heartbeatTtlMs / 3) {\n // Never resurrect an expired lease: its processing list may already\n // belong to the reclaimer. A fresh UUID fences all old settlements.\n if (\n !owner ||\n (await redis.eval(RENEW, 1, heartbeat(owner), heartbeatTtlMs)) !== 1\n )\n owner = await register();\n renewedAt = Date.now();\n }\n if (Date.now() - maintainedAt >= 100) {\n await redis.eval(PROMOTE, 2, delayed, ready, Date.now());\n for (const stale of await redis.smembers(owners)) {\n if (stale !== owner)\n await redis.eval(\n RECLAIM,\n 4,\n processing(stale),\n ready,\n heartbeat(stale),\n owners,\n stale,\n );\n }\n maintainedAt = Date.now();\n }\n for (\n let reserved = 0;\n reserved < concurrency &&\n !closed &&\n owner &&\n inflight.size < concurrency;\n reserved++\n ) {\n const claimOwner = owner;\n const raw = await redis.eval(\n RESERVE,\n 3,\n ready,\n processing(claimOwner),\n heartbeat(claimOwner),\n );\n if (typeof raw !== \"string\") break;\n const task = settle(raw, claimOwner, deliver);\n inflight.add(task);\n void task.finally(() => inflight.delete(task));\n }\n failing = false;\n } catch (error) {\n // Warn once per outage: a queue that lost Redis otherwise looks idle.\n if (!closed && !failing) console.warn(\"Queue polling failed:\", error);\n else if (!closed) debug(\"Queue polling error:\", error);\n failing = true;\n }\n await sleep();\n }\n }\n\n const backend: QueueBackend = {\n enqueue,\n async getQueueStats() {\n const counts = await redis.eval(\n STATS,\n 3,\n ready,\n delayed,\n owners,\n processing(\"\"),\n );\n if (\n !Array.isArray(counts) ||\n counts.length !== 3 ||\n counts.some((c) => typeof c !== \"number\")\n )\n throw new TypeError(\"Invalid queue statistics\");\n return {\n waiting: Number(counts[0]),\n active: Number(counts[1]),\n delayed: Number(counts[2]),\n completed: 0,\n failed: 0,\n };\n },\n start(deliver) {\n if (closed) return Promise.reject(new Error(\"Queue is closed\"));\n startPromise ??= (async () => {\n owner = await register();\n loop = run(deliver);\n })().catch((error) => {\n startPromise = undefined;\n throw error;\n });\n return startPromise;\n },\n async close() {\n if (closed) return;\n closed = true;\n abort.abort();\n await startPromise;\n await loop;\n // While handlers drain, keep the lease alive. close() must not permit a\n // live long-running handler to be reclaimed merely because shutdown began.\n const timer = setInterval(\n () => {\n if (owner)\n void redis\n .eval(RENEW, 1, heartbeat(owner), heartbeatTtlMs)\n .catch((error) => debug(\"Queue close heartbeat:\", error));\n },\n Math.floor(heartbeatTtlMs / 3),\n );\n try {\n await Promise.all(inflight);\n } finally {\n clearInterval(timer);\n }\n if (owner) {\n await redis.del(heartbeat(owner));\n await redis.eval(\n RECLAIM,\n 4,\n processing(owner),\n ready,\n heartbeat(owner),\n owners,\n owner,\n );\n }\n },\n };\n return createWorldQueue(backend, config, { ...options, debug });\n}\n","import { configFromEnv, type RedisWorldConfig } from \"./config.js\";\nimport { createQueue } from \"./queue.js\";\nimport { debug } from \"./util.js\";\nimport { createRedisBackedWorld, type RedisBackedWorld } from \"./world.js\";\n\n/** `config` is laid over `configFromEnv()`: an option left out comes from the\n * `WORKFLOW_REDIS_*` variables, then the defaults. */\nexport function createWorld(\n config: Partial<RedisWorldConfig> = {},\n): RedisBackedWorld {\n return createRedisBackedWorld(config, {\n env: configFromEnv(),\n createQueue,\n label: \"world-redis\",\n debug,\n });\n}\n\nexport type { RedisWorldConfig } from \"./config.js\";\nexport type { QueueStats } from \"./queue.js\";\nexport type { KeepRuns, RunRetention } from \"./reaper.js\";\n"],"mappings":";;;;;;AA8BA,MAAM,UAAU;;;;;;;;;;;;;;AAchB,MAAM,UAAU;;;;;;;;;;;;AAYhB,MAAM,MAAM;;;;;;;;AAQZ,MAAM,QAAQ;;;;;;AAMd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ACpB,EAAA,CAAG,CAAC,CAAC,OAAO,UAAU;IACpB,eAAe,KAAA;IACf,MAAM;GACR,CAAC;GACD,OAAO;EACT;EACA,MAAM,QAAQ;GACZ,IAAI,QAAQ;GACZ,SAAS;GACT,MAAM,MAAM;GACZ,MAAM;GACN,MAAM;GAGN,MAAM,QAAQ,kBACN;IACJ,IAAI,OACF,MACG,KAAK,OAAO,GAAG,UAAU,KAAK,GAAG,cAAc,CAAC,CAChD,OAAO,UAAU,MAAM,0BAA0B,KAAK,CAAC;GAC9D,GACA,KAAK,MAAM,iBAAiB,CAAC,CAC/B;GACA,IAAI;IACF,MAAM,QAAQ,IAAI,QAAQ;GAC5B,UAAU;IACR,cAAc,KAAK;GACrB;GACA,IAAI,OAAO;IACT,MAAM,MAAM,IAAI,UAAU,KAAK,CAAC;IAChC,MAAM,MAAM,KACV,SACA,GACA,WAAW,KAAK,GAChB,OACA,UAAU,KAAK,GACf,QACA,KACF;GACF;EACF;CAE4B,GAAG,QAAQ;EAAE,GAAG;EAAS;CAAM,CAAC;AAChE;;;;AC9ZA,SAAgB,YACd,SAAoC,CAAC,GACnB;CAClB,OAAO,uBAAuB,QAAQ;EACpC,KAAK,cAAc;EACnB;EACA,OAAO;EACP;CACF,CAAC;AACH"}
|
package/dist/internal.d.ts
CHANGED
|
@@ -1,89 +1,13 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import {
|
|
3
|
-
import { Redis } from "ioredis";
|
|
4
|
-
/**
|
|
5
|
-
* The run id the SDK asks the world for when a run starts. Without options it is
|
|
6
|
-
* a fresh ULID. With `runKey` and `runKeyTime` it is the same ULID every time, so
|
|
7
|
-
* starting the same run twice creates one run: the second start finds it already
|
|
8
|
-
* there.
|
|
9
|
-
*
|
|
10
|
-
* The id is a real ULID. Its time is `runKeyTime`, not the clock, because the SDK
|
|
11
|
-
* seeds a workflow's clock from the time in its run id. Pass the time the run is
|
|
12
|
-
* for, such as when the event it handles happened, and the same time on every
|
|
13
|
-
* attempt.
|
|
14
|
-
*
|
|
15
|
-
* @param options - The options given to `start()`. `runKey` is a non-empty string
|
|
16
|
-
* that names the run (at most 1024 characters), and `runKeyTime` is milliseconds
|
|
17
|
-
* since the epoch.
|
|
18
|
-
* @returns The id without its `wrun_` prefix, which the SDK adds.
|
|
19
|
-
* @throws When `runKey` is not a usable string, or comes without a valid `runKeyTime`.
|
|
20
|
-
*/
|
|
21
|
-
export declare function createRunId(options?: Readonly<Record<string, unknown>>): string;
|
|
22
|
-
/**
|
|
23
|
-
* The idempotency key for the message that starts a run, which is the only
|
|
24
|
-
* message that carries the run's input. `start()` sends no key of its own, so a
|
|
25
|
-
* second start of the same run would otherwise queue a second first delivery.
|
|
26
|
-
*
|
|
27
|
-
* The key only covers a first message that is still queued or running. Once the
|
|
28
|
-
* run has started, the queue does not enqueue the message at all (see
|
|
29
|
-
* `runHasStarted` in `createQueue`): the runtime runs a run's first step from the
|
|
30
|
-
* first message, in parallel with `run_created`, so a late second one would run
|
|
31
|
-
* that step's code again.
|
|
32
|
-
*
|
|
33
|
-
* @param message - The queue payload.
|
|
34
|
-
* @returns A key for a run's first message, and `undefined` for every other message.
|
|
35
|
-
*/
|
|
36
|
-
export declare function initialMessageKey(message: unknown): string | undefined;
|
|
37
|
-
interface StreamerConfig {
|
|
38
|
-
redis: Redis;
|
|
39
|
-
keyPrefix: string;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a streamer implementation using Redis Streams.
|
|
43
|
-
*
|
|
44
|
-
* Uses Redis auto-generated stream entry IDs (timestamp-sequence format)
|
|
45
|
-
* instead of custom IDs, since Redis Streams require IDs in
|
|
46
|
-
* `<millisecondsTime>-<sequenceNumber>` format.
|
|
47
|
-
*
|
|
48
|
-
* Key layout (every key is scoped to the run that owns the stream, so two runs
|
|
49
|
-
* can use the same stream name without sharing chunks):
|
|
50
|
-
* - `<prefix>stream:<runId>:<name>` Redis Stream of the chunks plus the eof marker
|
|
51
|
-
* - `<prefix>streams:by_run:<runId>` Set of the run's stream names
|
|
52
|
-
* - `<prefix>stream_notify` pub/sub channel carrying live chunks
|
|
53
|
-
*/
|
|
54
|
-
export declare function createStreamer(config: StreamerConfig): Streamer & {
|
|
55
|
-
close(): Promise<void>;
|
|
56
|
-
};
|
|
1
|
+
import { _ as RunRetention, a as QueueStats, c as WorldQueueOptions, d as RedisBackedWorldConfig, g as KeepRuns, h as resolveConfig, i as QueueBackend, l as createWorldQueue, m as configFromEnv, n as createRedisBackedWorld, o as QueuedMessage, p as ResolvedConfig, r as Deliver, s as WorldQueue, t as RedisBackedWorld, u as retryDelayMs } from "./world-BDW_evdb.js";
|
|
2
|
+
import { setupRedis } from "./cli.js";
|
|
57
3
|
/**
|
|
58
4
|
* Creates a namespaced debug logger.
|
|
59
5
|
* Logs to stderr to avoid corrupting CLI JSON output on stdout.
|
|
60
6
|
*
|
|
61
7
|
* Control via WORKFLOW_DEBUG environment variable:
|
|
62
|
-
* WORKFLOW_DEBUG=1
|
|
63
|
-
* WORKFLOW_DEBUG=
|
|
64
|
-
* WORKFLOW_DEBUG=mysql-world,redis-world // Multiple namespaces
|
|
8
|
+
* WORKFLOW_DEBUG=1 // Enable all
|
|
9
|
+
* WORKFLOW_DEBUG=redis-world // Named namespaces, comma-separated
|
|
65
10
|
*/
|
|
66
11
|
export declare function createDebugLogger(namespace: string): (...args: unknown[]) => void;
|
|
67
|
-
|
|
68
|
-
* Load an env file when present without making a `.env` file mandatory.
|
|
69
|
-
*
|
|
70
|
-
* Node 24 provides the parser natively, so published worlds do not need a
|
|
71
|
-
* runtime dependency solely to load local development configuration.
|
|
72
|
-
*/
|
|
73
|
-
export declare function loadOptionalEnvFile(path?: string): void;
|
|
74
|
-
/**
|
|
75
|
-
* JSON replacer that encodes Uint8Array as a tagged object with base64 data.
|
|
76
|
-
*/
|
|
77
|
-
export declare function uint8ArrayReplacer(_key: string, value: unknown): unknown;
|
|
78
|
-
/**
|
|
79
|
-
* Is there a `Uint8Array` anywhere in the graph?
|
|
80
|
-
*
|
|
81
|
-
* Must stay exact: `JSON.stringify` turns a bare `Uint8Array` into
|
|
82
|
-
* `{"0":1,...}` without complaint, so a miss is data corruption, not a slow
|
|
83
|
-
* path. The walk allocates nothing, which is why it beats the replacer over
|
|
84
|
-
* the same nodes.
|
|
85
|
-
*/
|
|
86
|
-
export declare function hasBinary(value: unknown): boolean;
|
|
87
|
-
export declare function createStorage(redis: Redis, keyPrefix: string, maxEventsPerRun?: number): Storage;
|
|
88
|
-
export { type KeepRuns, type RunRetention, createReaper, hasRetention, validateRetention };
|
|
12
|
+
export { type Deliver, type KeepRuns, type QueueBackend, type QueueStats, type QueuedMessage, type RedisBackedWorld, type RedisBackedWorldConfig, type ResolvedConfig, type RunRetention, type WorldQueue, type WorldQueueOptions, configFromEnv, createRedisBackedWorld, createWorldQueue, resolveConfig, retryDelayMs, setupRedis };
|
|
89
13
|
//# sourceMappingURL=internal.d.ts.map
|
package/dist/internal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.d.ts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"internal.d.ts","names":[],"sources":["../src/internal/debug.ts"],"mappings":";;;;;;;;;;wBAQgB,kBAAkB,wBAAiB"}
|
package/dist/internal.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as
|
|
3
|
-
|
|
1
|
+
import { i as resolveConfig, r as configFromEnv } from "./keys-CqKp_Amk.js";
|
|
2
|
+
import { t as setupRedis } from "./cli-BEHfAdNK.js";
|
|
3
|
+
import { a as createDebugLogger, n as createWorldQueue, r as retryDelayMs, t as createRedisBackedWorld } from "./world-783VJfKL.js";
|
|
4
|
+
export { configFromEnv, createDebugLogger, createRedisBackedWorld, createWorldQueue, resolveConfig, retryDelayMs, setupRedis };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
/**
|
|
3
|
+
* The config the `WORKFLOW_REDIS_*` variables give. `createWorld` lays the
|
|
4
|
+
* config it is passed over this, so an option left out still comes from env.
|
|
5
|
+
*/
|
|
6
|
+
function configFromEnv() {
|
|
7
|
+
return {
|
|
8
|
+
redis: process.env.WORKFLOW_REDIS_URL || process.env.REDIS_URL || "redis://localhost:6379",
|
|
9
|
+
queueConcurrency: Number.parseInt(process.env.WORKFLOW_REDIS_WORKER_CONCURRENCY ?? "", 10) || void 0,
|
|
10
|
+
keyPrefix: process.env.WORKFLOW_REDIS_KEY_PREFIX || void 0
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const DEFAULTS = {
|
|
14
|
+
queueConcurrency: 10,
|
|
15
|
+
keyPrefix: "workflow:",
|
|
16
|
+
backoffType: "exponential",
|
|
17
|
+
backoffDelayMs: 1e3,
|
|
18
|
+
maxBackoffMs: 3e4,
|
|
19
|
+
httpTimeoutMs: 3e5,
|
|
20
|
+
maxAttempts: 73,
|
|
21
|
+
enableAutoPipelining: true
|
|
22
|
+
};
|
|
23
|
+
/** Fills the shared defaults and checks the shared limits, for every queue. */
|
|
24
|
+
function resolveConfig(config) {
|
|
25
|
+
const resolved = { ...config };
|
|
26
|
+
for (const [key, value] of Object.entries(DEFAULTS)) if (resolved[key] === void 0 || resolved[key] === "") resolved[key] = value;
|
|
27
|
+
for (const [name, minimum] of [
|
|
28
|
+
["queueConcurrency", 1],
|
|
29
|
+
["maxAttempts", 49],
|
|
30
|
+
["maxBackoffMs", 1],
|
|
31
|
+
["backoffDelayMs", 0],
|
|
32
|
+
["httpTimeoutMs", 1],
|
|
33
|
+
["idempotencyTtlMs", 1]
|
|
34
|
+
]) checkInteger(name, resolved[name], minimum);
|
|
35
|
+
return resolved;
|
|
36
|
+
}
|
|
37
|
+
/** Throws unless `value` is an integer from `minimum` to Node's timer limit; `undefined` passes. */
|
|
38
|
+
function checkInteger(name, value, minimum) {
|
|
39
|
+
if (value !== void 0 && (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum || value > 2147483647)) throw new RangeError(`${name} must be an integer between ${minimum} and 2147483647`);
|
|
40
|
+
}
|
|
41
|
+
// src/keys.ts
|
|
42
|
+
/**
|
|
43
|
+
* The world's Redis key layout, the one place storage, streams, the queue and
|
|
44
|
+
* the reaper build key names from. A key added here that belongs to a run must
|
|
45
|
+
* also go in `ownedByRun`, or the reaper leaks it.
|
|
46
|
+
*/
|
|
47
|
+
function keys(prefix) {
|
|
48
|
+
const queue = `${prefix}queue:flows`;
|
|
49
|
+
const k = {
|
|
50
|
+
runKey: (id) => `${prefix}run:${id}`,
|
|
51
|
+
runAttributesKey: (id) => `${prefix}run:attrs:${id}`,
|
|
52
|
+
runMetaKey: (id) => `${prefix}run:meta:${id}`,
|
|
53
|
+
runsIndexKey: () => `${prefix}runs:index`,
|
|
54
|
+
runsByNameKey: (name) => `${prefix}runs:by_name:${name}`,
|
|
55
|
+
runsByStatusKey: (status) => `${prefix}runs:by_status:${status}`,
|
|
56
|
+
eventsKey: (runId) => `${prefix}events:data:${runId}`,
|
|
57
|
+
eventsByCorrelationKey: (runId, correlationId) => `${prefix}events:by_correlation:${runId}:${correlationId}`,
|
|
58
|
+
resumeClaimKey: (runId, resumeId) => `${prefix}events:resume:${runId}:${resumeId}`,
|
|
59
|
+
stepKey: (runId, stepId) => `${prefix}step:${runId}:${stepId}`,
|
|
60
|
+
stepsIndexKey: (runId) => `${prefix}steps:by_run:${runId}`,
|
|
61
|
+
stepStatusKey: (runId, stepId) => `${prefix}step:status:${runId}:${stepId}`,
|
|
62
|
+
hookKey: (hookId) => `${prefix}hook:${hookId}`,
|
|
63
|
+
hooksByTokenKey: (token) => `${prefix}hooks:by_token:${token}`,
|
|
64
|
+
hooksIndexKey: (runId) => `${prefix}hooks:by_run:${runId}`,
|
|
65
|
+
hooksTokenMapKey: (runId) => `${prefix}hooks:tokens:${runId}`,
|
|
66
|
+
streamKey: (runId, name) => `${prefix}stream:${runId}:${name}`,
|
|
67
|
+
streamsByRunKey: (runId) => `${prefix}streams:by_run:${runId}`,
|
|
68
|
+
streamChannel: `${prefix}stream_notify`,
|
|
69
|
+
reaperLockKey: `${prefix}reaper:lock`,
|
|
70
|
+
/** The list queue's keys. */
|
|
71
|
+
queue: {
|
|
72
|
+
ready: queue,
|
|
73
|
+
delayed: `${queue}:delayed`,
|
|
74
|
+
workers: `${queue}:workers`,
|
|
75
|
+
processing: (owner) => `${queue}:processing:${owner}`,
|
|
76
|
+
heartbeat: (owner) => `${queue}:heartbeat:${owner}`,
|
|
77
|
+
idempotent: (key) => `${queue}:idempotent:${key}`,
|
|
78
|
+
/** Every queue key, for SCAN/KEYS. */
|
|
79
|
+
pattern: `${prefix}queue:*`
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* Every key a run owns except its run, meta and index entries, which the
|
|
83
|
+
* reaper deletes last. Resume claims are not indexed: scan `resumeClaimKey(runId, "*")`.
|
|
84
|
+
*/
|
|
85
|
+
ownedByRun(runId, ids) {
|
|
86
|
+
return [
|
|
87
|
+
k.eventsKey(runId),
|
|
88
|
+
k.runAttributesKey(runId),
|
|
89
|
+
k.stepsIndexKey(runId),
|
|
90
|
+
k.hooksIndexKey(runId),
|
|
91
|
+
k.hooksTokenMapKey(runId),
|
|
92
|
+
k.streamsByRunKey(runId),
|
|
93
|
+
...[...ids.correlationIds].map((id) => k.eventsByCorrelationKey(runId, id)),
|
|
94
|
+
...ids.stepIds.flatMap((id) => [k.stepKey(runId, id), k.stepStatusKey(runId, id)]),
|
|
95
|
+
...ids.hookIds.map(k.hookKey),
|
|
96
|
+
...ids.streamNames.map((name) => k.streamKey(runId, name))
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
return k;
|
|
101
|
+
}
|
|
102
|
+
export { resolveConfig as i, checkInteger as n, configFromEnv as r, keys as t };
|
|
103
|
+
|
|
104
|
+
//# sourceMappingURL=keys-CqKp_Amk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys-CqKp_Amk.js","names":[],"sources":["../src/config.ts","../src/keys.ts"],"sourcesContent":["import type { RedisOptions } from \"ioredis\";\nimport type { RunRetention } from \"./reaper.js\";\n\n/** The options every Redis-backed world shares, whatever its queue. */\nexport interface RedisBackedWorldConfig {\n /**\n * Redis connection URL or connection options\n * Examples:\n * - 'redis://localhost:6379'\n * - 'rediss://username:password@host:6380/0'\n * - { host: 'localhost', port: 6379 }\n */\n redis: string | RedisOptions;\n\n /**\n * Number of concurrent workers processing jobs\n * Default: 10\n */\n queueConcurrency?: number;\n\n /**\n * Optional key prefix for all Redis keys\n * Useful for multi-tenancy or namespace isolation\n * Default: 'workflow:'\n */\n keyPrefix?: string;\n\n /**\n * Backoff strategy for retries: 'fixed' replays at the same interval,\n * 'exponential' doubles the delay on each attempt.\n * Default: 'exponential'\n */\n backoffType?: \"fixed\" | \"exponential\";\n\n /**\n * Base delay for backoff (ms). For exponential backoff this is multiplied\n * by 2^(attempt - 1), up to `maxBackoffMs`.\n * Default: 1000\n */\n backoffDelayMs?: number;\n\n /**\n * The longest a retry is delayed (ms). With the default number of attempts an\n * uncapped exponential backoff would wait hours between late attempts.\n * Default: 30000 (30s)\n */\n maxBackoffMs?: number;\n\n /**\n * Base URL the queue worker uses to dispatch jobs back to the user's HTTP\n * server, which must serve `world.createQueueHandler(...)` at\n * `/.well-known/workflow/v1/flow` (steps travel on the same route). A route\n * registered with `world.registerRoute`, or an in-process handler registered\n * with `world.registerHandler`, takes precedence.\n * Default: process.env.WORKFLOW_BASE_URL || `http://localhost:${process.env.PORT ?? 3000}`\n */\n baseUrl?: string;\n\n /**\n * Per-job HTTP request timeout (ms). Long-running workflow steps may need a\n * higher value.\n * Default: 300_000 (5 minutes)\n */\n httpTimeoutMs?: number;\n\n /** Maximum events a run may accumulate, reported as `EventResult.maxEvents`.\n * Default: `WORKFLOW_MAX_EVENTS` || 25_000 */\n maxEventsPerRun?: number;\n\n /**\n * Batch commands issued in the same event-loop tick into one write. A win\n * once several runs are in flight (~1.75x at 256 concurrent) and a marginal\n * loss for a single serial caller.\n * Default: true\n */\n enableAutoPipelining?: boolean;\n\n /**\n * How long finished runs are kept, per terminal status: `age` in seconds,\n * `count` of newest runs to keep. A reaper on each worker deletes the run, its event log, steps, hooks\n * and streams once it passes. A status without a policy is kept forever.\n * Default: undefined (nothing expires)\n *\n * @example\n * retention: {\n * completed: { age: 24 * 60 * 60, count: 1000 },\n * failed: { age: 7 * 24 * 60 * 60 },\n * }\n */\n retention?: RunRetention;\n\n /**\n * Delivery budget per message (see each world for what happens past it).\n * At least 49: the runtime records a run as failed (`MAX_DELIVERIES_EXCEEDED`)\n * on delivery 49, and a message retired earlier strands its run in `running`.\n * Default: 73\n */\n maxAttempts?: number;\n\n /**\n * Optional TTL (ms) for queue deduplication when `idempotencyKey`\n * is supplied to `queue()`. By default no TTL is applied: the dedup key\n * lives until the job is acknowledged, so duplicate enqueues are dropped\n * for the entire lifetime of the pending/delayed job (the semantics the\n * workflow runtime relies on when re-enqueuing pending steps on replay).\n * Only set this if you need the dedup guard to expire on wall-clock time;\n * it must be much larger than the longest expected queue delay/backlog,\n * otherwise duplicate step executions become possible.\n * Default: undefined (dedup key released on job completion)\n */\n idempotencyTtlMs?: number;\n}\n\nexport interface RedisWorldConfig extends RedisBackedWorldConfig {\n /** Worker liveness TTL in milliseconds. Default 90000; minimum 300.\n * Set above the longest event-loop pause. Renewed every TTL / 3. */\n heartbeatTtlMs?: number;\n\n /**\n * Delivery budget before retries continue at maxBackoffMs. Messages are never\n * silently retired. At least 49. Default: 73\n */\n maxAttempts?: number;\n}\n\n/**\n * The config the `WORKFLOW_REDIS_*` variables give. `createWorld` lays the\n * config it is passed over this, so an option left out still comes from env.\n */\nexport function configFromEnv() {\n return {\n redis:\n process.env.WORKFLOW_REDIS_URL ||\n process.env.REDIS_URL ||\n \"redis://localhost:6379\",\n queueConcurrency:\n Number.parseInt(\n process.env.WORKFLOW_REDIS_WORKER_CONCURRENCY ?? \"\",\n 10,\n ) || undefined,\n keyPrefix: process.env.WORKFLOW_REDIS_KEY_PREFIX || undefined,\n };\n}\n\nconst DEFAULTS = {\n queueConcurrency: 10,\n keyPrefix: \"workflow:\",\n backoffType: \"exponential\",\n backoffDelayMs: 1000,\n maxBackoffMs: 30_000,\n httpTimeoutMs: 300_000,\n maxAttempts: 73,\n enableAutoPipelining: true,\n} as const;\n\n/** A world config with every shared default filled in. */\nexport type ResolvedConfig<C extends RedisBackedWorldConfig> = C & {\n -readonly [K in keyof typeof DEFAULTS]-?: NonNullable<\n RedisBackedWorldConfig[K]\n >;\n};\n\n/** Fills the shared defaults and checks the shared limits, for every queue. */\nexport function resolveConfig<C extends RedisBackedWorldConfig>(\n config: C,\n): ResolvedConfig<C> {\n const resolved: Record<string, unknown> = { ...(config as object) };\n for (const [key, value] of Object.entries(DEFAULTS))\n if (resolved[key] === undefined || resolved[key] === \"\")\n resolved[key] = value;\n for (const [name, minimum] of [\n [\"queueConcurrency\", 1],\n [\"maxAttempts\", 49],\n [\"maxBackoffMs\", 1],\n [\"backoffDelayMs\", 0],\n [\"httpTimeoutMs\", 1],\n [\"idempotencyTtlMs\", 1],\n ] as const)\n checkInteger(name, resolved[name], minimum);\n return resolved as unknown as ResolvedConfig<C>;\n}\n\n/** Throws unless `value` is an integer from `minimum` to Node's timer limit; `undefined` passes. */\nexport function checkInteger(name: string, value: unknown, minimum: number) {\n if (\n value !== undefined &&\n (typeof value !== \"number\" ||\n !Number.isSafeInteger(value) ||\n value < minimum ||\n value > 2_147_483_647)\n )\n throw new RangeError(\n `${name} must be an integer between ${minimum} and 2147483647`,\n );\n}\n","/**\n * The world's Redis key layout, the one place storage, streams, the queue and\n * the reaper build key names from. A key added here that belongs to a run must\n * also go in `ownedByRun`, or the reaper leaks it.\n */\nexport function keys(prefix: string) {\n const queue = `${prefix}queue:flows`;\n const k = {\n runKey: (id: string) => `${prefix}run:${id}`,\n runAttributesKey: (id: string) => `${prefix}run:attrs:${id}`,\n // `<status>|<specVersion>`, read by scripts that must check status atomically.\n runMetaKey: (id: string) => `${prefix}run:meta:${id}`,\n runsIndexKey: () => `${prefix}runs:index`,\n runsByNameKey: (name: string) => `${prefix}runs:by_name:${name}`,\n runsByStatusKey: (status: string) => `${prefix}runs:by_status:${status}`,\n\n eventsKey: (runId: string) => `${prefix}events:data:${runId}`,\n // A correlation id is only unique within its run.\n eventsByCorrelationKey: (runId: string, correlationId: string) =>\n `${prefix}events:by_correlation:${runId}:${correlationId}`,\n resumeClaimKey: (runId: string, resumeId: string) =>\n `${prefix}events:resume:${runId}:${resumeId}`,\n\n stepKey: (runId: string, stepId: string) =>\n `${prefix}step:${runId}:${stepId}`,\n stepsIndexKey: (runId: string) => `${prefix}steps:by_run:${runId}`,\n // `<status>|<attempt>`, kept beside the step so a script can check it atomically.\n stepStatusKey: (runId: string, stepId: string) =>\n `${prefix}step:status:${runId}:${stepId}`,\n\n hookKey: (hookId: string) => `${prefix}hook:${hookId}`,\n hooksByTokenKey: (token: string) => `${prefix}hooks:by_token:${token}`,\n hooksIndexKey: (runId: string) => `${prefix}hooks:by_run:${runId}`,\n hooksTokenMapKey: (runId: string) => `${prefix}hooks:tokens:${runId}`,\n\n // Run IDs (`wrun_<ulid>`) never contain ':', so the first ':' after the run\n // ID always starts the stream name, even if the name contains colons itself.\n streamKey: (runId: string, name: string) =>\n `${prefix}stream:${runId}:${name}`,\n streamsByRunKey: (runId: string) => `${prefix}streams:by_run:${runId}`,\n streamChannel: `${prefix}stream_notify`,\n\n reaperLockKey: `${prefix}reaper:lock`,\n /** The list queue's keys. */\n queue: {\n ready: queue,\n delayed: `${queue}:delayed`,\n workers: `${queue}:workers`,\n processing: (owner: string) => `${queue}:processing:${owner}`,\n heartbeat: (owner: string) => `${queue}:heartbeat:${owner}`,\n idempotent: (key: string) => `${queue}:idempotent:${key}`,\n /** Every queue key, for SCAN/KEYS. */\n pattern: `${prefix}queue:*`,\n },\n\n /**\n * Every key a run owns except its run, meta and index entries, which the\n * reaper deletes last. Resume claims are not indexed: scan `resumeClaimKey(runId, \"*\")`.\n */\n ownedByRun(\n runId: string,\n ids: {\n correlationIds: Iterable<string>;\n stepIds: string[];\n hookIds: string[];\n streamNames: string[];\n },\n ): string[] {\n return [\n k.eventsKey(runId),\n k.runAttributesKey(runId),\n k.stepsIndexKey(runId),\n k.hooksIndexKey(runId),\n k.hooksTokenMapKey(runId),\n k.streamsByRunKey(runId),\n ...[...ids.correlationIds].map((id) =>\n k.eventsByCorrelationKey(runId, id),\n ),\n ...ids.stepIds.flatMap((id) => [\n k.stepKey(runId, id),\n k.stepStatusKey(runId, id),\n ]),\n ...ids.hookIds.map(k.hookKey),\n ...ids.streamNames.map((name) => k.streamKey(runId, name)),\n ];\n },\n };\n return k;\n}\n"],"mappings":";;;;;AAiIA,SAAgB,gBAAgB;CAC9B,OAAO;EACL,OACE,QAAQ,IAAI,sBACZ,QAAQ,IAAI,aACZ;EACF,kBACE,OAAO,SACL,QAAQ,IAAI,qCAAqC,IACjD,EACF,KAAK,KAAA;EACP,WAAW,QAAQ,IAAI,6BAA6B,KAAA;CACtD;AACF;AAEA,MAAM,WAAW;CACf,kBAAkB;CAClB,WAAW;CACX,aAAa;CACb,gBAAgB;CAChB,cAAc;CACd,eAAe;CACf,aAAa;CACb,sBAAsB;AACxB;;AAUA,SAAgB,cACd,QACmB;CACnB,MAAM,WAAoC,EAAE,GAAI,OAAkB;CAClE,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,GAChD,IAAI,SAAS,SAAS,KAAA,KAAa,SAAS,SAAS,IACnD,SAAS,OAAO;CACpB,KAAK,MAAM,CAAC,MAAM,YAAY;EAC5B,CAAC,oBAAoB,CAAC;EACtB,CAAC,eAAe,EAAE;EAClB,CAAC,gBAAgB,CAAC;EAClB,CAAC,kBAAkB,CAAC;EACpB,CAAC,iBAAiB,CAAC;EACnB,CAAC,oBAAoB,CAAC;CACxB,GACE,aAAa,MAAM,SAAS,OAAO,OAAO;CAC5C,OAAO;AACT;;AAGA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