@pattern-stack/codegen 0.15.3 → 0.16.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/CHANGELOG.md +88 -0
- package/consumer-skills/integration/change-sources-and-sinks.md +1 -1
- package/dist/{chunk-GCYKMF22.js → chunk-24WXSC3C.js} +6 -6
- package/dist/{chunk-32DOFN3T.js → chunk-2WDX6I7T.js} +2 -2
- package/dist/{chunk-WWGYCIJX.js → chunk-43SBT72G.js} +2 -2
- package/dist/{chunk-FBGHYQIZ.js → chunk-5LXOJGO2.js} +6 -6
- package/dist/{chunk-32BMMV4H.js → chunk-5RT7JGKT.js} +5 -5
- package/dist/{chunk-3NMCDN7L.js → chunk-5TK7MEN4.js} +2 -2
- package/dist/chunk-5TK7MEN4.js.map +1 -0
- package/dist/{chunk-4H3PETLM.js → chunk-AYC2HEAL.js} +12 -9
- package/dist/chunk-AYC2HEAL.js.map +1 -0
- package/dist/{chunk-27ETSJ2X.js → chunk-COGHTKXY.js} +2 -2
- package/dist/{chunk-IYNSRIGR.js → chunk-CRBVI4GE.js} +5 -5
- package/dist/{chunk-J7JMVS2B.js → chunk-CZQUOIDY.js} +4 -4
- package/dist/{chunk-O37C3YE6.js → chunk-DGYTSCKN.js} +14 -8
- package/dist/chunk-DGYTSCKN.js.map +1 -0
- package/dist/{chunk-L7BNNRGI.js → chunk-DLG62MQY.js} +26 -6
- package/dist/chunk-DLG62MQY.js.map +1 -0
- package/dist/{chunk-TNXH7BJS.js → chunk-E45CSC33.js} +2 -2
- package/dist/{chunk-4JLJYWJC.js → chunk-H6FO2ZDJ.js} +99 -11
- package/dist/chunk-H6FO2ZDJ.js.map +1 -0
- package/dist/{chunk-5Y7W3XR6.js → chunk-IT6FRTEW.js} +30 -11
- package/dist/chunk-IT6FRTEW.js.map +1 -0
- package/dist/{chunk-RC23QROE.js → chunk-JM3T27ZW.js} +78 -4
- package/dist/chunk-JM3T27ZW.js.map +1 -0
- package/dist/{chunk-Z7PQCAVK.js → chunk-LQ6PYFU6.js} +4 -4
- package/dist/chunk-MYQIQ27N.js +118 -0
- package/dist/chunk-MYQIQ27N.js.map +1 -0
- package/dist/{chunk-YTN6BKWA.js → chunk-NXNVTXKG.js} +5 -5
- package/dist/{chunk-RDVTWIYY.js → chunk-QSJ3J4HE.js} +5 -5
- package/dist/{chunk-4MVGAMUA.js → chunk-RUSUZZAF.js} +4 -4
- package/dist/{chunk-4RFHUZXU.js → chunk-T4YJRD22.js} +4 -4
- package/dist/{chunk-7YGORYZD.js → chunk-T6C4LFLC.js} +4 -4
- package/dist/{chunk-OGIZXGPY.js → chunk-TDEHU73T.js} +4 -4
- package/dist/{chunk-YLPAPPLW.js → chunk-TIZXQU26.js} +36 -9
- package/dist/chunk-TIZXQU26.js.map +1 -0
- package/dist/{chunk-EOLLMEAH.js → chunk-TKVTEUBD.js} +3 -3
- package/dist/chunk-TKVTEUBD.js.map +1 -0
- package/dist/{chunk-YPWODKD5.js → chunk-W2UIDI3R.js} +5 -5
- package/dist/chunk-W4HOHZVF.js +1 -0
- package/dist/{chunk-DCCZB4UC.js → chunk-XWBK3XJK.js} +4 -4
- package/dist/{chunk-SR7F3TJY.js → chunk-YK5JEVLX.js} +4 -4
- package/dist/{chunk-BIO6F7YI.js → chunk-ZPL74UQN.js} +4 -2
- package/dist/{chunk-BIO6F7YI.js.map → chunk-ZPL74UQN.js.map} +1 -1
- package/dist/runtime/base-classes/index.js +22 -22
- package/dist/runtime/subsystems/analytics/analytics.module.js +2 -2
- package/dist/runtime/subsystems/analytics/index.js +4 -4
- package/dist/runtime/subsystems/auth/auth.module.js +1 -1
- package/dist/runtime/subsystems/auth/index.js +7 -7
- package/dist/runtime/subsystems/bridge/bridge-delivery-handler.js +3 -3
- package/dist/runtime/subsystems/bridge/bridge-delivery.drizzle-backend.js +1 -1
- package/dist/runtime/subsystems/bridge/bridge-outbox-drain-hook.d.ts +2 -1
- package/dist/runtime/subsystems/bridge/bridge-outbox-drain-hook.js +6 -5
- package/dist/runtime/subsystems/bridge/bridge.module.js +16 -15
- package/dist/runtime/subsystems/bridge/event-flow.service.js +3 -3
- package/dist/runtime/subsystems/bridge/index.js +16 -15
- package/dist/runtime/subsystems/cache/cache.drizzle-backend.js +2 -2
- package/dist/runtime/subsystems/cache/cache.module.js +3 -3
- package/dist/runtime/subsystems/cache/index.js +5 -5
- package/dist/runtime/subsystems/events/event-bus.drizzle-backend.d.ts +20 -0
- package/dist/runtime/subsystems/events/event-bus.drizzle-backend.js +4 -3
- package/dist/runtime/subsystems/events/event-bus.memory-backend.js +2 -2
- package/dist/runtime/subsystems/events/events.module.d.ts +14 -0
- package/dist/runtime/subsystems/events/events.module.js +6 -5
- package/dist/runtime/subsystems/events/index.js +12 -11
- package/dist/runtime/subsystems/index.js +88 -87
- package/dist/runtime/subsystems/integration/build-change-source.js +2 -2
- package/dist/runtime/subsystems/integration/detection-config.schema.d.ts +23 -15
- package/dist/runtime/subsystems/integration/detection-config.schema.js +1 -1
- package/dist/runtime/subsystems/integration/execute-integration.use-case.js +2 -2
- package/dist/runtime/subsystems/integration/index.js +17 -17
- package/dist/runtime/subsystems/integration/integration-cursor-store.drizzle-backend.js +2 -2
- package/dist/runtime/subsystems/integration/integration-run-recorder.drizzle-backend.js +2 -2
- package/dist/runtime/subsystems/integration/integration.module.js +4 -4
- package/dist/runtime/subsystems/integration/webhook-change-source.d.ts +36 -6
- package/dist/runtime/subsystems/integration/webhook-change-source.js +1 -1
- package/dist/runtime/subsystems/jobs/index.d.ts +2 -1
- package/dist/runtime/subsystems/jobs/index.js +42 -30
- package/dist/runtime/subsystems/jobs/job-orchestrator.bullmq-backend.js +6 -5
- package/dist/runtime/subsystems/jobs/job-orchestrator.bullmq-backend.js.map +1 -1
- package/dist/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.d.ts +2 -1
- package/dist/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.js +4 -3
- package/dist/runtime/subsystems/jobs/job-orchestrator.memory-backend.js +3 -3
- package/dist/runtime/subsystems/jobs/job-run-service.drizzle-backend.js +3 -3
- package/dist/runtime/subsystems/jobs/job-run-service.memory-backend.js +2 -2
- package/dist/runtime/subsystems/jobs/job-worker.bullmq-backend.js +3 -3
- package/dist/runtime/subsystems/jobs/job-worker.d.ts +28 -0
- package/dist/runtime/subsystems/jobs/job-worker.js +4 -3
- package/dist/runtime/subsystems/jobs/job-worker.module.js +11 -10
- package/dist/runtime/subsystems/jobs/jobs-domain.module.d.ts +12 -7
- package/dist/runtime/subsystems/jobs/jobs-domain.module.js +9 -8
- package/dist/runtime/subsystems/jobs/jobs-domain.tokens.d.ts +13 -1
- package/dist/runtime/subsystems/jobs/jobs-domain.tokens.js +3 -1
- package/dist/runtime/subsystems/jobs/pg-notify.d.ts +85 -0
- package/dist/runtime/subsystems/jobs/pg-notify.js +14 -0
- package/dist/runtime/subsystems/jobs/pg-notify.js.map +1 -0
- package/dist/runtime/subsystems/observability/index.js +4 -4
- package/dist/runtime/subsystems/observability/observability.module.js +4 -4
- package/dist/runtime/subsystems/observability/observability.service.js +3 -3
- package/dist/runtime/subsystems/storage/index.js +4 -4
- package/dist/runtime/subsystems/storage/storage.module.js +2 -2
- package/dist/src/cli/index.js +53 -15
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -11
- package/dist/src/index.js +9 -9
- package/package.json +1 -1
- package/runtime/subsystems/bridge/bridge-outbox-drain-hook.ts +27 -0
- package/runtime/subsystems/events/event-bus.drizzle-backend.ts +108 -4
- package/runtime/subsystems/events/events.module.ts +14 -0
- package/runtime/subsystems/integration/detection-config.schema.ts +64 -54
- package/runtime/subsystems/integration/webhook-change-source.ts +187 -133
- package/runtime/subsystems/jobs/index.ts +10 -0
- package/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.ts +29 -2
- package/runtime/subsystems/jobs/job-worker.module.ts +11 -0
- package/runtime/subsystems/jobs/job-worker.ts +98 -0
- package/runtime/subsystems/jobs/jobs-domain.module.ts +22 -7
- package/runtime/subsystems/jobs/jobs-domain.tokens.ts +13 -0
- package/runtime/subsystems/jobs/pg-notify.ts +216 -0
- package/templates/subsystem/events-config/codegen-config-events-block.ejs.t +14 -0
- package/templates/subsystem/jobs-config/codegen-config-jobs-block.ejs.t +13 -4
- package/dist/chunk-3NMCDN7L.js.map +0 -1
- package/dist/chunk-4H3PETLM.js.map +0 -1
- package/dist/chunk-4JLJYWJC.js.map +0 -1
- package/dist/chunk-5Y7W3XR6.js.map +0 -1
- package/dist/chunk-EOLLMEAH.js.map +0 -1
- package/dist/chunk-L7BNNRGI.js.map +0 -1
- package/dist/chunk-O37C3YE6.js.map +0 -1
- package/dist/chunk-RC23QROE.js.map +0 -1
- package/dist/chunk-UTN4GBPQ.js +0 -1
- package/dist/chunk-YLPAPPLW.js.map +0 -1
- /package/dist/{chunk-GCYKMF22.js.map → chunk-24WXSC3C.js.map} +0 -0
- /package/dist/{chunk-32DOFN3T.js.map → chunk-2WDX6I7T.js.map} +0 -0
- /package/dist/{chunk-WWGYCIJX.js.map → chunk-43SBT72G.js.map} +0 -0
- /package/dist/{chunk-FBGHYQIZ.js.map → chunk-5LXOJGO2.js.map} +0 -0
- /package/dist/{chunk-32BMMV4H.js.map → chunk-5RT7JGKT.js.map} +0 -0
- /package/dist/{chunk-27ETSJ2X.js.map → chunk-COGHTKXY.js.map} +0 -0
- /package/dist/{chunk-IYNSRIGR.js.map → chunk-CRBVI4GE.js.map} +0 -0
- /package/dist/{chunk-J7JMVS2B.js.map → chunk-CZQUOIDY.js.map} +0 -0
- /package/dist/{chunk-TNXH7BJS.js.map → chunk-E45CSC33.js.map} +0 -0
- /package/dist/{chunk-Z7PQCAVK.js.map → chunk-LQ6PYFU6.js.map} +0 -0
- /package/dist/{chunk-YTN6BKWA.js.map → chunk-NXNVTXKG.js.map} +0 -0
- /package/dist/{chunk-RDVTWIYY.js.map → chunk-QSJ3J4HE.js.map} +0 -0
- /package/dist/{chunk-4MVGAMUA.js.map → chunk-RUSUZZAF.js.map} +0 -0
- /package/dist/{chunk-4RFHUZXU.js.map → chunk-T4YJRD22.js.map} +0 -0
- /package/dist/{chunk-7YGORYZD.js.map → chunk-T6C4LFLC.js.map} +0 -0
- /package/dist/{chunk-OGIZXGPY.js.map → chunk-TDEHU73T.js.map} +0 -0
- /package/dist/{chunk-YPWODKD5.js.map → chunk-W2UIDI3R.js.map} +0 -0
- /package/dist/{chunk-UTN4GBPQ.js.map → chunk-W4HOHZVF.js.map} +0 -0
- /package/dist/{chunk-DCCZB4UC.js.map → chunk-XWBK3XJK.js.map} +0 -0
- /package/dist/{chunk-SR7F3TJY.js.map → chunk-YK5JEVLX.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../runtime/subsystems/jobs/job-worker.ts"],"sourcesContent":["/**\n * JobWorker — backend-agnostic tick loop for the job orchestration domain\n * (ADR-022, JOB-3).\n *\n * One worker instance per active pool. On `onModuleInit` it starts two\n * intervals: the poll loop (claim → process → repeat) and the stale-claim\n * sweeper. On `onModuleDestroy` / SIGTERM it drains in-flight work and\n * releases still-`running` rows back to `pending` so a replacement worker\n * can resume with step memoization intact.\n *\n * The claim query is the beating heart: `SELECT … FOR UPDATE SKIP LOCKED`\n * inside a single transaction. Multiple worker processes share the table\n * without serialising on row locks.\n */\n// TODO(logging-subsystem): swap to ILogger once ADR-028 lands\nimport { Inject, Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from '@nestjs/common';\nimport { ModuleRef } from '@nestjs/core';\nimport { and, asc, desc, eq, inArray, lt, lte, sql } from 'drizzle-orm';\nimport type { DrizzleClient } from '../../types/drizzle';\nimport { tokenKey } from '../token-key';\nimport { DRIZZLE } from '../../constants/tokens';\nimport { jobRuns, type JobRunRow } from './job-orchestration.schema';\nimport type { IJobOrchestrator, JobRun } from './job-orchestrator.protocol';\nimport type { IJobRunService } from './job-run-service.protocol';\nimport type { IJobStepService } from './job-step-service.protocol';\nimport {\n JOB_ORCHESTRATOR,\n JOB_RUN_SERVICE,\n JOB_STEP_SERVICE,\n} from './jobs-domain.tokens';\nimport {\n JOB_HANDLER_REGISTRY,\n JobHandlerBase,\n type JobContext,\n type JobHandlerMeta,\n type RetryPolicy,\n type SpawnChildOptions,\n type StepOptions,\n} from './job-handler.base';\nimport { JOBS_WAKE_CHANNEL, PgNotifyListener } from './pg-notify';\n\n/**\n * Options accepted by `JobWorker`. JOB-5 threads these through module\n * `.forRoot()` config; supplied here as a plain DI-constructor argument\n * so the worker compiles standalone.\n */\nexport interface JobWorkerOptions {\n /** Pool name this worker claims from. Matches `job.pool`. */\n pool: string;\n /** Max concurrent in-flight `processRun` calls. */\n concurrency: number;\n /** Poll interval in ms. Default 1000. */\n pollIntervalMs?: number;\n /** Stale sweep interval in ms. Default 60_000. */\n staleSweeperIntervalMs?: number;\n /**\n * Threshold beyond which a `running` row is presumed stranded by a\n * crashed worker. Default 5 min. Must be >= 2× max handler duration.\n */\n staleThresholdMs?: number;\n /** Max ms to wait for in-flight drain on SIGTERM. Default 30_000. */\n shutdownTimeoutMs?: number;\n /**\n * LISTEN-NOTIFY-1 — when true, hold a dedicated listener connection and\n * LISTEN on `codegen_jobs_wake`. A notification naming this worker's `pool`\n * triggers an immediate (debounced) claim cycle, so an enqueue is claimed in\n * milliseconds instead of waiting for the next `pollIntervalMs` tick. Polling\n * continues unchanged as the fallback heartbeat. Default false.\n */\n listenNotify?: boolean;\n}\n\n// ADR-037: namespaced `Symbol.for(...)` (via `tokenKey()`) — matches by value\n// across runtime copies.\nexport const JOB_WORKER_OPTIONS = Symbol.for(tokenKey('jobs', 'worker-options'));\n\nconst DEFAULT_POLL_INTERVAL_MS = 1_000;\nconst DEFAULT_STALE_SWEEPER_INTERVAL_MS = 60_000;\nconst DEFAULT_STALE_THRESHOLD_MS = 5 * 60_000;\nconst DEFAULT_SHUTDOWN_TIMEOUT_MS = 30_000;\n\nconst TERMINAL_STATUSES: JobRunRow['status'][] = [\n 'completed',\n 'failed',\n 'timed_out',\n 'canceled',\n];\n\n// ─── Pure helpers (exported for unit tests) ────────────────────────────────\n\n/**\n * Backoff delay in ms for the Nth attempt (1-indexed). Supports both\n * policy modes. Exponential is capped at `Number.MAX_SAFE_INTEGER` so\n * pathological attempt counts don't overflow.\n */\nexport function computeBackoff(policy: RetryPolicy, attempts: number): number {\n const base = Math.max(policy.baseMs, 0);\n if (policy.backoff === 'fixed') {\n return base;\n }\n // exponential: baseMs * 2^(attempts-1)\n const exponent = Math.max(attempts - 1, 0);\n if (exponent >= 53) return Number.MAX_SAFE_INTEGER; // 2^53 overflow guard\n const raw = base * Math.pow(2, exponent);\n if (!Number.isFinite(raw) || raw >= Number.MAX_SAFE_INTEGER) {\n return Number.MAX_SAFE_INTEGER;\n }\n return raw;\n}\n\n/**\n * Decide whether an error should be retried under the given policy.\n * Matches `nonRetryableErrors` by `.name` OR `.code`. Returns\n * - `'retry'` if attempts remain and the error isn't blacklisted,\n * - `'fail'` otherwise (terminal failure).\n */\nexport function classifyError(\n err: unknown,\n policy: RetryPolicy | undefined,\n currentAttempts: number,\n): 'retry' | 'fail' {\n if (!policy) return 'fail';\n const errObj = err as { name?: string; code?: string } | undefined;\n const name = errObj?.name;\n const code = errObj?.code;\n const nonRetryable = policy.nonRetryableErrors ?? [];\n if (nonRetryable.some((n) => n === name || n === code)) return 'fail';\n if (currentAttempts + 1 >= policy.attempts) return 'fail';\n return 'retry';\n}\n\n/**\n * Build the raw claim-candidate select. Exported so tests can inspect\n * `.toSQL()` without spinning up the full worker. Matches JOB-3 §4 and\n * ADR-022 \"Claim query (Drizzle backend)\".\n */\nexport function buildClaimQuery(db: DrizzleClient, pool: string) {\n return db\n .select({ id: jobRuns.id })\n .from(jobRuns)\n .where(\n and(\n eq(jobRuns.status, 'pending'),\n eq(jobRuns.pool, pool),\n lte(jobRuns.runAt, new Date()),\n ),\n )\n .orderBy(desc(jobRuns.priority), asc(jobRuns.runAt))\n .limit(1)\n .for('update', { skipLocked: true });\n}\n\n/**\n * Build the stale-claim sweep candidate select. `FOR UPDATE SKIP LOCKED`\n * per OQ-2 resolution (2026-04-19): per-worker sweeper, safe without\n * leader election because the update is self-gating.\n */\nexport function buildStaleSweepQuery(\n db: DrizzleClient,\n staleThresholdMs: number,\n) {\n const threshold = new Date(Date.now() - staleThresholdMs);\n return db\n .select({ id: jobRuns.id })\n .from(jobRuns)\n .where(\n and(\n eq(jobRuns.status, 'running'),\n lt(jobRuns.claimedAt, threshold),\n ),\n )\n .for('update', { skipLocked: true });\n}\n\n// ─── Error serialisation ───────────────────────────────────────────────────\n\nfunction serialiseError(err: unknown, attempt: number, retryable: boolean) {\n const e = err as { message?: string; stack?: string; code?: string } | undefined;\n return {\n message: (e?.message ?? String(err)) as string,\n stack: e?.stack,\n retryable,\n attempt,\n };\n}\n\n// ─── JobWorker ─────────────────────────────────────────────────────────────\n\n@Injectable()\nexport class JobWorker implements OnModuleInit, OnModuleDestroy {\n private readonly logger = new Logger(JobWorker.name);\n private shuttingDown = false;\n private readonly inFlight = new Set<Promise<void>>();\n private pollTimer: ReturnType<typeof setInterval> | null = null;\n private sweeperTimer: ReturnType<typeof setInterval> | null = null;\n private sigtermHandled = false;\n private readonly sigtermHandler: () => void;\n\n private readonly pollIntervalMs: number;\n private readonly staleSweeperIntervalMs: number;\n private readonly staleThresholdMs: number;\n private readonly shutdownTimeoutMs: number;\n\n // LISTEN-NOTIFY-1 — dedicated listener + debounce state. `null` when\n // `listenNotify` is off (the common case); polling is the only driver then.\n private readonly listenNotifyEnabled: boolean;\n private notifyListener: PgNotifyListener | null = null;\n /** True while a wake-driven claim cycle is in flight (debounce gate). */\n private wakeDraining = false;\n /** A notify arrived mid-cycle → re-check once when the cycle ends. */\n private wakeRecheckPending = false;\n\n constructor(\n @Inject(DRIZZLE) private readonly db: DrizzleClient,\n @Inject(JOB_ORCHESTRATOR) private readonly orchestrator: IJobOrchestrator,\n @Inject(JOB_RUN_SERVICE) private readonly runService: IJobRunService,\n @Inject(JOB_STEP_SERVICE) private readonly stepService: IJobStepService,\n @Inject(JOB_WORKER_OPTIONS) private readonly options: JobWorkerOptions,\n private readonly moduleRef: ModuleRef,\n ) {\n this.pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n this.staleSweeperIntervalMs =\n options.staleSweeperIntervalMs ?? DEFAULT_STALE_SWEEPER_INTERVAL_MS;\n this.staleThresholdMs = options.staleThresholdMs ?? DEFAULT_STALE_THRESHOLD_MS;\n this.shutdownTimeoutMs =\n options.shutdownTimeoutMs ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;\n this.listenNotifyEnabled = options.listenNotify ?? false;\n\n this.sigtermHandler = () => {\n if (this.sigtermHandled) return;\n this.sigtermHandled = true;\n void this.onModuleDestroy();\n };\n void this.runService; // reserved for future scope-aware cancellation paths\n }\n\n // ============================================================================\n // Lifecycle\n // ============================================================================\n\n onModuleInit(): void {\n this.pollTimer = setInterval(() => {\n void this.pollAndProcess();\n }, this.pollIntervalMs);\n this.sweeperTimer = setInterval(() => {\n void this.sweepStaleClaims();\n }, this.staleSweeperIntervalMs);\n process.on('SIGTERM', this.sigtermHandler);\n\n // LISTEN-NOTIFY-1 — start the wake listener ALONGSIDE the poll timer (never\n // instead). A notify for this worker's pool drives an immediate claim cycle;\n // the interval timer above stays the durability heartbeat. Listener startup\n // is fire-and-forget: a connect failure self-heals via the listener's own\n // backoff, and until it's up the poll loop is the sole driver.\n if (this.listenNotifyEnabled) {\n // The DRIZZLE provider wraps a `pg.Pool`, exposed by drizzle as `$client`.\n const pool = (this.db as unknown as { $client?: unknown }).$client;\n if (!pool || typeof (pool as { connect?: unknown }).connect !== 'function') {\n this.logger.warn(\n `listen_notify enabled but the Drizzle client exposes no pg Pool ` +\n `($client.connect missing) — falling back to interval polling only.`,\n );\n } else {\n this.notifyListener = new PgNotifyListener({\n channel: JOBS_WAKE_CHANNEL,\n pool: pool as { connect(): Promise<never> },\n label: `jobs:${this.options.pool}`,\n onNotify: (payload) => this.onWake(payload),\n });\n void this.notifyListener.start();\n }\n }\n }\n\n /**\n * Wake handler — a `codegen_jobs_wake` notification arrived. Only payloads\n * naming THIS worker's pool are relevant (other pools have their own workers).\n * Debounced: if a claim cycle is already running we just flag a re-check so a\n * burst of N enqueues collapses to at most one extra cycle (D3).\n */\n private onWake(payload: string): void {\n if (this.shuttingDown) return;\n if (payload !== this.options.pool) return;\n if (this.wakeDraining) {\n this.wakeRecheckPending = true;\n return;\n }\n void this.drainOnWake();\n }\n\n /**\n * Claim-until-empty on a wake. Unlike the interval `pollAndProcess` (one\n * claim per tick), a wake drains greedily up to the concurrency ceiling so a\n * burst that arrived together is dispatched without waiting for N ticks. The\n * `wakeRecheckPending` flag coalesces notifies that land mid-drain.\n */\n private async drainOnWake(): Promise<void> {\n this.wakeDraining = true;\n try {\n do {\n this.wakeRecheckPending = false;\n // Claim while there's capacity; pollAndProcess no-ops at the ceiling.\n let progressed = true;\n while (\n progressed &&\n !this.shuttingDown &&\n this.inFlight.size < this.options.concurrency\n ) {\n const before = this.inFlight.size;\n await this.pollAndProcess();\n progressed = this.inFlight.size > before;\n }\n } while (this.wakeRecheckPending && !this.shuttingDown);\n } finally {\n this.wakeDraining = false;\n }\n }\n\n async onModuleDestroy(): Promise<void> {\n if (this.shuttingDown) {\n // Still drain, but don't tear intervals down twice.\n await this.drainInFlight();\n return;\n }\n this.shuttingDown = true;\n if (this.pollTimer) {\n clearInterval(this.pollTimer);\n this.pollTimer = null;\n }\n if (this.sweeperTimer) {\n clearInterval(this.sweeperTimer);\n this.sweeperTimer = null;\n }\n process.removeListener('SIGTERM', this.sigtermHandler);\n\n // LISTEN-NOTIFY-1 — release the listener connection so the process can exit\n // cleanly. Best-effort; a failure here doesn't block the drain.\n if (this.notifyListener) {\n try {\n await this.notifyListener.stop();\n } catch (err) {\n this.logger.error(`notify listener stop failed: ${(err as Error).message}`);\n }\n this.notifyListener = null;\n }\n\n await this.drainInFlight();\n\n // Any rows still `running` past timeout → release back to pending.\n try {\n await this.db\n .update(jobRuns)\n .set({ status: 'pending', claimedAt: null, startedAt: null })\n .where(\n and(eq(jobRuns.status, 'running'), eq(jobRuns.pool, this.options.pool)),\n );\n } catch (err) {\n this.logger.error(`shutdown reset failed: ${(err as Error).message}`);\n }\n }\n\n private async drainInFlight(): Promise<void> {\n if (this.inFlight.size === 0) return;\n const timeout = new Promise<void>((resolve) =>\n setTimeout(resolve, this.shutdownTimeoutMs),\n );\n await Promise.race([\n Promise.allSettled([...this.inFlight]).then(() => undefined),\n timeout,\n ]);\n }\n\n // ============================================================================\n // Poll loop\n // ============================================================================\n\n async pollAndProcess(): Promise<void> {\n if (this.shuttingDown) return;\n if (this.inFlight.size >= this.options.concurrency) return;\n\n let claimed: JobRunRow | null;\n try {\n claimed = await this.claimNext(this.options.pool);\n } catch (err) {\n this.logger.error(`claimNext failed: ${(err as Error).message}`);\n return;\n }\n if (!claimed) return;\n\n const run = claimed;\n const promise = this.processRun(run).catch((err) => {\n this.logger.error(\n `processRun(${run.id}) unhandled: ${(err as Error).message}`,\n );\n });\n this.inFlight.add(promise);\n promise.finally(() => {\n this.inFlight.delete(promise);\n });\n }\n\n /**\n * Claim the next runnable row from the pool. Transaction ensures the\n * select-candidate + update-to-running pair is atomic; FOR UPDATE SKIP\n * LOCKED lets multiple workers share the table without serialising.\n */\n async claimNext(pool: string): Promise<JobRunRow | null> {\n return this.db.transaction(async (tx) => {\n const candidates = await tx\n .select({ id: jobRuns.id })\n .from(jobRuns)\n .where(\n and(\n eq(jobRuns.status, 'pending'),\n eq(jobRuns.pool, pool),\n lte(jobRuns.runAt, new Date()),\n ),\n )\n .orderBy(desc(jobRuns.priority), asc(jobRuns.runAt))\n .limit(1)\n .for('update', { skipLocked: true });\n const candidate = candidates[0];\n if (!candidate) return null;\n\n const [claimed] = await tx\n .update(jobRuns)\n .set({\n status: 'running',\n claimedAt: new Date(),\n startedAt: new Date(),\n updatedAt: new Date(),\n })\n .where(eq(jobRuns.id, candidate.id))\n .returning();\n return (claimed ?? null) as JobRunRow | null;\n });\n }\n\n // ============================================================================\n // Stale claim sweeper\n // ============================================================================\n\n /**\n * Release rows whose `claimed_at` is older than the threshold. Safe to\n * run concurrently across workers — the two-phase tx (select-for-update\n * then update) guarantees each stranded row is only reset once.\n */\n async sweepStaleClaims(): Promise<void> {\n if (this.shuttingDown) return;\n try {\n await this.db.transaction(async (tx) => {\n const threshold = new Date(Date.now() - this.staleThresholdMs);\n const stale = await tx\n .select({ id: jobRuns.id })\n .from(jobRuns)\n .where(\n and(eq(jobRuns.status, 'running'), lt(jobRuns.claimedAt, threshold)),\n )\n .for('update', { skipLocked: true });\n if (stale.length === 0) return;\n const ids = stale.map((r) => r.id);\n await tx\n .update(jobRuns)\n .set({ status: 'pending', claimedAt: null, startedAt: null })\n .where(inArray(jobRuns.id, ids));\n for (const id of ids) {\n this.logger.warn(`Recovered stale claim on run ${id}`);\n }\n });\n } catch (err) {\n this.logger.error(`sweepStaleClaims failed: ${(err as Error).message}`);\n }\n }\n\n // ============================================================================\n // processRun\n // ============================================================================\n\n private async processRun(claimed: JobRunRow): Promise<void> {\n const registryEntry = JOB_HANDLER_REGISTRY.get(claimed.jobType);\n\n // (a) Missing handler — defensive; JOB-5 boot validator should have caught.\n if (!registryEntry) {\n this.logger.error(\n `No handler registered for jobType='${claimed.jobType}' (run ${claimed.id})`,\n );\n await this.markFailed(\n claimed,\n new Error(`No handler registered for jobType='${claimed.jobType}'`),\n /*finalAttempts*/ (claimed.attempts ?? 0) + 1,\n );\n return;\n }\n\n // (b) Concurrency-queue release gate — defer if another run with the\n // same key is already `running`.\n if (claimed.concurrencyKey) {\n const inflight = await this.db\n .select({ id: jobRuns.id })\n .from(jobRuns)\n .where(\n and(\n eq(jobRuns.concurrencyKey, claimed.concurrencyKey),\n eq(jobRuns.status, 'running'),\n ),\n );\n const other = inflight.find((r) => r.id !== claimed.id);\n if (other) {\n await this.db\n .update(jobRuns)\n .set({\n status: 'pending',\n claimedAt: null,\n startedAt: null,\n updatedAt: new Date(),\n })\n .where(eq(jobRuns.id, claimed.id));\n return;\n }\n }\n\n const meta = registryEntry.meta as JobHandlerMeta<unknown>;\n const HandlerClass = registryEntry.handlerClass;\n\n // (c) Build JobContext. Resolve the handler instance from Nest's DI\n // graph so its `@Inject` constructor params (which may come from\n // any module in the app graph) are satisfied. `moduleRef.create()`\n // would otherwise instantiate a fresh class within JobWorkerModule's\n // scope only — which blows up with \"not a provider of the current\n // module\" for any handler that consumes a service from a peer\n // module (e.g. CrmSyncJob injecting CrmSyncFactory from CrmModule).\n // Consequence: handlers MUST be registered as providers in their\n // owning module (@Injectable + `providers: [HandlerClass]`). The\n // @JobHandler decorator handles registry registration only, not DI.\n // See the jobs skill's handler-authoring.md for the registration\n // rule.\n const handler = this.moduleRef.get(\n HandlerClass as unknown as new (...args: unknown[]) => unknown,\n { strict: false },\n ) as JobHandlerBase<unknown>;\n const ctx: JobContext<unknown> = {\n input: claimed.input,\n run: claimed as JobRun,\n step: this.makeStepFn(claimed),\n spawnChild: this.makeSpawnFn(claimed),\n logger: new Logger(`JobRun:${claimed.id}`),\n };\n\n const attemptsBefore = claimed.attempts ?? 0;\n try {\n // (d) Run the handler.\n const output = (await handler.run(ctx)) as Record<string, unknown> | undefined;\n // (e) Success.\n await this.db\n .update(jobRuns)\n .set({\n status: 'completed',\n output: (output ?? {}) as Record<string, unknown>,\n finishedAt: new Date(),\n updatedAt: new Date(),\n attempts: attemptsBefore + 1,\n })\n .where(eq(jobRuns.id, claimed.id));\n } catch (err) {\n // (f) Error classification + retry/fail.\n const policy = meta.retry;\n const decision = classifyError(err, policy, attemptsBefore);\n const nextAttempts = attemptsBefore + 1;\n if (decision === 'retry' && policy) {\n const delay = computeBackoff(policy, nextAttempts);\n await this.db\n .update(jobRuns)\n .set({\n status: 'pending',\n attempts: nextAttempts,\n runAt: new Date(Date.now() + delay),\n startedAt: null,\n claimedAt: null,\n error: serialiseError(err, nextAttempts, true),\n updatedAt: new Date(),\n })\n .where(eq(jobRuns.id, claimed.id));\n } else {\n await this.markFailed(claimed, err, nextAttempts);\n }\n }\n }\n\n private async markFailed(\n claimed: JobRunRow,\n err: unknown,\n finalAttempts: number,\n ): Promise<void> {\n await this.db\n .update(jobRuns)\n .set({\n status: 'failed',\n attempts: finalAttempts,\n finishedAt: new Date(),\n error: serialiseError(err, finalAttempts, false),\n updatedAt: new Date(),\n })\n .where(eq(jobRuns.id, claimed.id));\n\n // Parent-close-policy cascade: if this run has children under the same\n // root_run_id and this run's own parentClosePolicy is 'terminate', cascade.\n if (claimed.parentClosePolicy === 'terminate') {\n try {\n // JOB-8 — thread the run's own tenantId so the orchestrator's\n // multi-tenant gate passes. Without this, every terminate-policy\n // cascade throws MissingTenantIdError under multiTenant=true and\n // the outer catch silently swallows it — children never cancel.\n await this.orchestrator.cancel(claimed.id, {\n cascade: true,\n reason: 'parent-failed',\n tenantId: claimed.tenantId,\n });\n } catch (cascadeErr) {\n // cancel is idempotent; failure here is unusual but not fatal.\n this.logger.warn(\n `cascade on failed run ${claimed.id}: ${(cascadeErr as Error).message}`,\n );\n }\n }\n }\n\n // ============================================================================\n // ctx.step / ctx.spawnChild builders\n // ============================================================================\n\n private makeStepFn(run: JobRunRow) {\n return async <TOutput>(\n stepId: string,\n fn: () => Promise<TOutput>,\n _opts?: StepOptions,\n ): Promise<TOutput> => {\n void _opts;\n const existing = await this.stepService.findStep(run.id, stepId);\n if (existing?.status === 'completed') {\n return existing.output as TOutput;\n }\n\n const seq = await this.nextStepSeq(run.id);\n const startedAt = new Date();\n const nextAttempts = (existing?.attempts ?? 0) + 1;\n await this.stepService.recordStep({\n jobRunId: run.id,\n stepId,\n kind: 'task',\n seq,\n status: 'running',\n startedAt,\n attempts: nextAttempts,\n });\n try {\n const output = await fn();\n await this.stepService.recordStep({\n jobRunId: run.id,\n stepId,\n kind: 'task',\n seq,\n status: 'completed',\n output: output as Record<string, unknown> | undefined,\n finishedAt: new Date(),\n attempts: nextAttempts,\n });\n return output;\n } catch (err) {\n await this.stepService.recordStep({\n jobRunId: run.id,\n stepId,\n kind: 'task',\n seq,\n status: 'failed',\n error: serialiseError(err, nextAttempts, false),\n finishedAt: new Date(),\n attempts: nextAttempts,\n });\n throw err;\n }\n };\n }\n\n private makeSpawnFn(run: JobRunRow) {\n return async (\n type: string,\n input: unknown,\n opts?: SpawnChildOptions,\n ): Promise<JobRun> => {\n return this.orchestrator.start(type, input, {\n parentRunId: run.id,\n parentClosePolicy: opts?.closePolicy,\n runAt: opts?.runAt,\n priority: opts?.priority,\n tags: opts?.tags,\n triggerSource: 'parent',\n triggerRef: run.id,\n });\n };\n }\n\n /**\n * Allocate the next `seq` for a given run. SELECT-max approach — runs\n * typically have <100 steps so the scan is cheap, and correctness across\n * retries is more important than the microseconds saved by an in-memory\n * counter (which would drift if the worker crashes mid-run and another\n * worker resumes via stale-claim sweep).\n */\n private async nextStepSeq(runId: string): Promise<number> {\n const result = await this.db.execute(\n sql`SELECT COALESCE(MAX(seq), 0) + 1 AS next FROM job_step WHERE job_run_id = ${runId}`,\n );\n // Driver shape varies and is NOT uniformly array-iterable, so we must\n // never array-destructure the raw result (that throws `{} is not iterable`\n // on the node-postgres `Result` object, which exposes `.rows` instead of\n // being an array — first hit by package-mode bridge deliveries on\n // `drizzle-orm/node-postgres`). Normalise to a row array first, then read.\n // - node-postgres `db.execute(sql)` → `{ rows: [{ next }], ... }`\n // - some drivers / future shapes → a plain `[{ next }]` array\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const raw = result as any;\n const rows: Array<{ next?: unknown }> = Array.isArray(raw)\n ? raw\n : Array.isArray(raw?.rows)\n ? raw.rows\n : [];\n const next = rows[0]?.next;\n return typeof next === 'undefined' ? 1 : Number(next);\n }\n\n // ============================================================================\n // (suppress unused-import noise)\n // ============================================================================\n}\n\n// Terminal statuses re-exported for JOB-4 parity imports.\nexport { TERMINAL_STATUSES };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,SAAS,QAA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
-
import {
|
|
7
|
-
EVENTS_MODULE_OPTIONS
|
|
8
|
-
} from "./chunk-H5NH7KPE.js";
|
|
9
9
|
import {
|
|
10
10
|
__decorateClass,
|
|
11
11
|
__decorateParam
|
|
@@ -197,4 +197,4 @@ MemoryEventBus = __decorateClass([
|
|
|
197
197
|
export {
|
|
198
198
|
MemoryEventBus
|
|
199
199
|
};
|
|
200
|
-
//# sourceMappingURL=chunk-
|
|
200
|
+
//# sourceMappingURL=chunk-LQ6PYFU6.js.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// runtime/subsystems/jobs/pg-notify.ts
|
|
2
|
+
import { Logger } from "@nestjs/common";
|
|
3
|
+
import { sql } from "drizzle-orm";
|
|
4
|
+
var JOBS_WAKE_CHANNEL = "codegen_jobs_wake";
|
|
5
|
+
var EVENTS_WAKE_CHANNEL = "codegen_events_wake";
|
|
6
|
+
async function pgNotify(tx, channel, payload) {
|
|
7
|
+
const client = tx;
|
|
8
|
+
await client.execute(sql`select pg_notify(${channel}, ${payload})`);
|
|
9
|
+
}
|
|
10
|
+
var DEFAULT_BACKOFF_MIN_MS = 100;
|
|
11
|
+
var DEFAULT_BACKOFF_MAX_MS = 5e3;
|
|
12
|
+
var PgNotifyListener = class {
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
this.opts = opts;
|
|
15
|
+
this.logger = new Logger(`PgNotifyListener(${opts.label})`);
|
|
16
|
+
this.backoffMinMs = opts.backoffMinMs ?? DEFAULT_BACKOFF_MIN_MS;
|
|
17
|
+
this.backoffMaxMs = opts.backoffMaxMs ?? DEFAULT_BACKOFF_MAX_MS;
|
|
18
|
+
this.backoffMs = this.backoffMinMs;
|
|
19
|
+
}
|
|
20
|
+
opts;
|
|
21
|
+
logger;
|
|
22
|
+
client = null;
|
|
23
|
+
stopped = false;
|
|
24
|
+
reconnectTimer = null;
|
|
25
|
+
backoffMs;
|
|
26
|
+
backoffMinMs;
|
|
27
|
+
backoffMaxMs;
|
|
28
|
+
/** WARN-once gate so a flapping listener doesn't spam the log. */
|
|
29
|
+
warnedDown = false;
|
|
30
|
+
/** Begin listening. Idempotent-ish: a second call while connected is a no-op. */
|
|
31
|
+
async start() {
|
|
32
|
+
this.stopped = false;
|
|
33
|
+
await this.connect();
|
|
34
|
+
}
|
|
35
|
+
/** Stop listening + release the connection. Safe to call repeatedly. */
|
|
36
|
+
async stop() {
|
|
37
|
+
this.stopped = true;
|
|
38
|
+
if (this.reconnectTimer) {
|
|
39
|
+
clearTimeout(this.reconnectTimer);
|
|
40
|
+
this.reconnectTimer = null;
|
|
41
|
+
}
|
|
42
|
+
await this.releaseClient();
|
|
43
|
+
}
|
|
44
|
+
async connect() {
|
|
45
|
+
if (this.stopped) return;
|
|
46
|
+
try {
|
|
47
|
+
const client = await this.opts.pool.connect();
|
|
48
|
+
client.on("notification", (msg) => {
|
|
49
|
+
if (msg.channel !== this.opts.channel) return;
|
|
50
|
+
try {
|
|
51
|
+
this.opts.onNotify(msg.payload ?? "");
|
|
52
|
+
} catch (err) {
|
|
53
|
+
this.logger.error(`onNotify threw: ${err.message}`);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
client.on("error", (err) => {
|
|
57
|
+
this.logger.debug?.(`listener connection error: ${err.message}`);
|
|
58
|
+
this.handleDrop();
|
|
59
|
+
});
|
|
60
|
+
await client.query(`LISTEN ${this.opts.channel}`);
|
|
61
|
+
this.client = client;
|
|
62
|
+
if (this.warnedDown) {
|
|
63
|
+
this.logger.log(
|
|
64
|
+
`listener reconnected; LISTEN ${this.opts.channel} re-established`
|
|
65
|
+
);
|
|
66
|
+
this.warnedDown = false;
|
|
67
|
+
}
|
|
68
|
+
this.backoffMs = this.backoffMinMs;
|
|
69
|
+
} catch (err) {
|
|
70
|
+
this.handleConnectFailure(err);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Connection dropped after being established → reconnect. */
|
|
74
|
+
handleDrop() {
|
|
75
|
+
if (this.stopped) return;
|
|
76
|
+
void this.releaseClient().finally(() => this.scheduleReconnect());
|
|
77
|
+
}
|
|
78
|
+
/** Initial / reconnect `connect()` threw. */
|
|
79
|
+
handleConnectFailure(err) {
|
|
80
|
+
this.scheduleReconnect(err);
|
|
81
|
+
}
|
|
82
|
+
scheduleReconnect(err) {
|
|
83
|
+
if (this.stopped) return;
|
|
84
|
+
if (!this.warnedDown) {
|
|
85
|
+
this.warnedDown = true;
|
|
86
|
+
this.logger.warn(
|
|
87
|
+
`listener down \u2014 falling back to interval polling until reconnect. Cause: ${err instanceof Error ? err.message : "connection lost"}. (This degrades latency, not durability \u2014 polling still drives all work.)`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
91
|
+
const delay = this.backoffMs;
|
|
92
|
+
this.backoffMs = Math.min(this.backoffMs * 2, this.backoffMaxMs);
|
|
93
|
+
this.reconnectTimer = setTimeout(() => {
|
|
94
|
+
this.reconnectTimer = null;
|
|
95
|
+
void this.connect();
|
|
96
|
+
}, delay);
|
|
97
|
+
}
|
|
98
|
+
async releaseClient() {
|
|
99
|
+
const client = this.client;
|
|
100
|
+
this.client = null;
|
|
101
|
+
if (!client) return;
|
|
102
|
+
try {
|
|
103
|
+
client.removeAllListeners?.("notification");
|
|
104
|
+
client.removeAllListeners?.("error");
|
|
105
|
+
if (client.release) client.release(true);
|
|
106
|
+
else if (client.end) await client.end();
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export {
|
|
113
|
+
JOBS_WAKE_CHANNEL,
|
|
114
|
+
EVENTS_WAKE_CHANNEL,
|
|
115
|
+
pgNotify,
|
|
116
|
+
PgNotifyListener
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=chunk-MYQIQ27N.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../runtime/subsystems/jobs/pg-notify.ts"],"sourcesContent":["/**\n * PgNotifyListener + pgNotify — Postgres LISTEN/NOTIFY wakeups\n * (LISTEN-NOTIFY-1, dogfood gap #7).\n *\n * The drizzle jobs worker and events outbox drainer poll on an interval today\n * (default 1 s/hop). With `listen_notify` enabled, a row write that makes work\n * claimable emits an in-transaction `pg_notify(...)`; a dedicated listener\n * connection wakes the polling loop the moment the writing transaction commits.\n *\n * Two halves:\n * - `pgNotify(tx, channel, payload)` — fire an in-tx `pg_notify`. MUST be\n * called with the SAME transaction handle as the row write it announces, so\n * Postgres delivers it only on commit (the transactional-outbox guarantee).\n * - `PgNotifyListener` — owns a single long-lived `pg.PoolClient`, issues\n * `LISTEN <channel>`, forwards each notification's payload to an owner\n * callback, debounces bursts, and reconnects with capped backoff on drop.\n *\n * **Polling never stops.** This is a wake-early optimisation layered ON TOP of\n * interval polling. A lost notification (listener down, pooler eats the LISTEN,\n * etc.) degrades to today's poll latency, never to lost work — the claim/drain\n * query remains the source of truth.\n *\n * **PgBouncer caveat:** session-scoped `LISTEN` does not survive a\n * transaction-mode pooler. `listen_notify` requires a direct (or session-mode)\n * connection; behind a transaction pooler notifies are simply never received and\n * the system degrades to polling. See the jobs config block / skill.\n */\n// TODO(logging-subsystem): swap to ILogger once ADR-028 lands\nimport { Logger } from '@nestjs/common';\nimport { sql } from 'drizzle-orm';\nimport type { DrizzleClient } from '../../types/drizzle';\nimport type { DrizzleTransaction } from '../events/event-bus.protocol';\n\n/** Channel the jobs worker LISTENs on; payload = pool name. */\nexport const JOBS_WAKE_CHANNEL = 'codegen_jobs_wake';\n/** Channel the events drainer LISTENs on; payload = event pool (or ''). */\nexport const EVENTS_WAKE_CHANNEL = 'codegen_events_wake';\n\n/**\n * Emit an in-transaction `pg_notify`. Call with the SAME `tx`/client handle as\n * the row write being announced so delivery is gated on commit. `payload` is a\n * short plain string (a pool name); it is NOT JSON — the wake is a hint and the\n * subsequent claim/drain query is authoritative. Channel names are framework\n * constants (never user input), so the `set_config`-free literal-channel form is\n * safe; the payload is bound as a parameter.\n */\nexport async function pgNotify(\n tx: DrizzleClient | DrizzleTransaction,\n channel: string,\n payload: string,\n): Promise<void> {\n const client = tx as DrizzleClient;\n // `pg_notify(channel, payload)` is the function form (vs the `NOTIFY chan,\n // 'payload'` statement form) precisely because it accepts bound parameters —\n // the payload is parameterised, never string-concatenated.\n await client.execute(sql`select pg_notify(${channel}, ${payload})`);\n}\n\n/** Minimal structural view of the `pg` Client/PoolClient surface we touch. */\ninterface PgListenClient {\n query(text: string): Promise<unknown>;\n on(event: 'notification', cb: (msg: { channel: string; payload?: string }) => void): void;\n on(event: 'error', cb: (err: Error) => void): void;\n removeAllListeners?: (event?: string) => void;\n release?: (err?: boolean) => void;\n end?: () => Promise<void>;\n}\n\n/** Minimal structural view of the `pg` Pool's `connect()`. */\ninterface PgPoolish {\n connect(): Promise<PgListenClient>;\n}\n\nconst DEFAULT_BACKOFF_MIN_MS = 100;\nconst DEFAULT_BACKOFF_MAX_MS = 5_000;\n\nexport interface PgNotifyListenerOptions {\n /** Channel to LISTEN on. */\n channel: string;\n /**\n * The underlying `pg.Pool` — obtained from `drizzleClient.$client`. A\n * dedicated `PoolClient` is checked out and held for the listener's lifetime\n * (separate from the query pool so a slow query never delays a wake).\n */\n pool: PgPoolish;\n /**\n * Called for every notification on `channel`, with the raw payload string\n * (`''` when Postgres delivers an empty payload). The owner decides whether\n * the payload is relevant (e.g. \"is this one of my pools?\") and debounces its\n * own claim cycle.\n */\n onNotify: (payload: string) => void;\n /** Label used in log lines (e.g. 'jobs:interactive', 'events'). */\n label: string;\n backoffMinMs?: number;\n backoffMaxMs?: number;\n}\n\n/**\n * Holds a dedicated listener connection and forwards notifications to `onNotify`.\n * Reconnects with capped exponential backoff on drop; logs the first failure +\n * the recovery exactly once each so a flapping connection doesn't flood logs.\n */\nexport class PgNotifyListener {\n private readonly logger: Logger;\n private client: PgListenClient | null = null;\n private stopped = false;\n private reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private backoffMs: number;\n private readonly backoffMinMs: number;\n private readonly backoffMaxMs: number;\n /** WARN-once gate so a flapping listener doesn't spam the log. */\n private warnedDown = false;\n\n constructor(private readonly opts: PgNotifyListenerOptions) {\n this.logger = new Logger(`PgNotifyListener(${opts.label})`);\n this.backoffMinMs = opts.backoffMinMs ?? DEFAULT_BACKOFF_MIN_MS;\n this.backoffMaxMs = opts.backoffMaxMs ?? DEFAULT_BACKOFF_MAX_MS;\n this.backoffMs = this.backoffMinMs;\n }\n\n /** Begin listening. Idempotent-ish: a second call while connected is a no-op. */\n async start(): Promise<void> {\n this.stopped = false;\n await this.connect();\n }\n\n /** Stop listening + release the connection. Safe to call repeatedly. */\n async stop(): Promise<void> {\n this.stopped = true;\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n await this.releaseClient();\n }\n\n private async connect(): Promise<void> {\n if (this.stopped) return;\n try {\n const client = await this.opts.pool.connect();\n client.on('notification', (msg) => {\n if (msg.channel !== this.opts.channel) return;\n try {\n this.opts.onNotify(msg.payload ?? '');\n } catch (err) {\n this.logger.error(`onNotify threw: ${(err as Error).message}`);\n }\n });\n client.on('error', (err) => {\n // A connection-level error is the signal to reconnect. Don't double-log\n // here — scheduleReconnect owns the WARN-once.\n this.logger.debug?.(`listener connection error: ${err.message}`);\n this.handleDrop();\n });\n await client.query(`LISTEN ${this.opts.channel}`);\n this.client = client;\n // Recovery: only announce if we had previously warned about being down.\n if (this.warnedDown) {\n this.logger.log(\n `listener reconnected; LISTEN ${this.opts.channel} re-established`,\n );\n this.warnedDown = false;\n }\n this.backoffMs = this.backoffMinMs;\n } catch (err) {\n this.handleConnectFailure(err);\n }\n }\n\n /** Connection dropped after being established → reconnect. */\n private handleDrop(): void {\n if (this.stopped) return;\n void this.releaseClient().finally(() => this.scheduleReconnect());\n }\n\n /** Initial / reconnect `connect()` threw. */\n private handleConnectFailure(err: unknown): void {\n this.scheduleReconnect(err);\n }\n\n private scheduleReconnect(err?: unknown): void {\n if (this.stopped) return;\n if (!this.warnedDown) {\n this.warnedDown = true;\n this.logger.warn(\n `listener down — falling back to interval polling until reconnect. ` +\n `Cause: ${err instanceof Error ? err.message : 'connection lost'}. ` +\n `(This degrades latency, not durability — polling still drives all work.)`,\n );\n }\n if (this.reconnectTimer) clearTimeout(this.reconnectTimer);\n const delay = this.backoffMs;\n this.backoffMs = Math.min(this.backoffMs * 2, this.backoffMaxMs);\n this.reconnectTimer = setTimeout(() => {\n this.reconnectTimer = null;\n void this.connect();\n }, delay);\n }\n\n private async releaseClient(): Promise<void> {\n const client = this.client;\n this.client = null;\n if (!client) return;\n try {\n client.removeAllListeners?.('notification');\n client.removeAllListeners?.('error');\n // A listener client is a checked-out pool connection; release it back\n // with `release(true)` (destroy) so a half-broken socket isn't reused.\n if (client.release) client.release(true);\n else if (client.end) await client.end();\n } catch {\n // best-effort teardown\n }\n }\n}\n"],"mappings":";AA4BA,SAAS,cAAc;AACvB,SAAS,WAAW;AAKb,IAAM,oBAAoB;AAE1B,IAAM,sBAAsB;AAUnC,eAAsB,SACpB,IACA,SACA,SACe;AACf,QAAM,SAAS;AAIf,QAAM,OAAO,QAAQ,uBAAuB,OAAO,KAAK,OAAO,GAAG;AACpE;AAiBA,IAAM,yBAAyB;AAC/B,IAAM,yBAAyB;AA6BxB,IAAM,mBAAN,MAAuB;AAAA,EAW5B,YAA6B,MAA+B;AAA/B;AAC3B,SAAK,SAAS,IAAI,OAAO,oBAAoB,KAAK,KAAK,GAAG;AAC1D,SAAK,eAAe,KAAK,gBAAgB;AACzC,SAAK,eAAe,KAAK,gBAAgB;AACzC,SAAK,YAAY,KAAK;AAAA,EACxB;AAAA,EAL6B;AAAA,EAVZ;AAAA,EACT,SAAgC;AAAA,EAChC,UAAU;AAAA,EACV,iBAAuD;AAAA,EACvD;AAAA,EACS;AAAA,EACA;AAAA;AAAA,EAET,aAAa;AAAA;AAAA,EAUrB,MAAM,QAAuB;AAC3B,SAAK,UAAU;AACf,UAAM,KAAK,QAAQ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Y;AACpB,WAAK,aAAa;AAClB,WAAK,OAAO;AAAA,QACV,iFACY,eAAe,QAAQ,IAAI,UAAU,iBAAiB;AAAA,MAEpE;AAAA,IACF;AACA,QAAI,KAAK,eAAgB,cAAa,KAAK,cAAc;AACzD,UAAM,QAAQ,KAAK;AACnB,SAAK,YAAY,KAAK,IAAI,KAAK,YAAY,GAAG,KAAK,YAAY;AAC/D,SAAK,iBAAiB,WAAW,MAAM;AACrC,WAAK,iBAAiB;AACtB,WAAK,KAAK,QAAQ;AAAA,IACpB,GAAG,KAAK;AAAA,EACV;AAAA,EAEA,MAAc,gBAA+B;AAC3C,UAAM,SAAS,KAAK;AACpB,SAAK,SAAS;AACd,QAAI,CAAC,OAAQ;AACb,QAAI;AACF,aAAO,qBAAqB,cAAc;AAC1C,aAAO,qBAAqB,OAAO;AAGnC,UAAI,OAAO,QAAS,QAAO,QAAQ,IAAI;AAAA,eAC9B,OAAO,IAAK,OAAM,OAAO,IAAI;AAAA,IACxC,QAAQ;AAAA,IAER;AAAA,EACF;AACF;","names":[]}
|
|
@@ -7,15 +7,15 @@ import {
|
|
|
7
7
|
} from "./chunk-CO6LUM72.js";
|
|
8
8
|
import {
|
|
9
9
|
JOB_ORCHESTRATOR
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
import {
|
|
12
|
-
EVENT_BUS
|
|
13
|
-
} from "./chunk-H5NH7KPE.js";
|
|
10
|
+
} from "./chunk-ZPL74UQN.js";
|
|
14
11
|
import {
|
|
15
12
|
BRIDGE_DELIVERY_REPO,
|
|
16
13
|
BRIDGE_MULTI_TENANT,
|
|
17
14
|
BRIDGE_REGISTRY
|
|
18
15
|
} from "./chunk-4LH67P4U.js";
|
|
16
|
+
import {
|
|
17
|
+
EVENT_BUS
|
|
18
|
+
} from "./chunk-H5NH7KPE.js";
|
|
19
19
|
import {
|
|
20
20
|
__decorateClass,
|
|
21
21
|
__decorateParam
|
|
@@ -118,4 +118,4 @@ export {
|
|
|
118
118
|
BRIDGE_DELIVERY_JOB_TYPE,
|
|
119
119
|
BridgeDeliveryHandler
|
|
120
120
|
};
|
|
121
|
-
//# sourceMappingURL=chunk-
|
|
121
|
+
//# sourceMappingURL=chunk-NXNVTXKG.js.map
|
|
@@ -7,13 +7,13 @@ import {
|
|
|
7
7
|
import {
|
|
8
8
|
MemoryJobStore
|
|
9
9
|
} from "./chunk-SNQ3TOWP.js";
|
|
10
|
-
import {
|
|
11
|
-
MissingTenantIdError
|
|
12
|
-
} from "./chunk-T4BIIU5E.js";
|
|
13
10
|
import {
|
|
14
11
|
JOBS_MULTI_TENANT,
|
|
15
12
|
JOB_ORCHESTRATOR
|
|
16
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-ZPL74UQN.js";
|
|
14
|
+
import {
|
|
15
|
+
MissingTenantIdError
|
|
16
|
+
} from "./chunk-T4BIIU5E.js";
|
|
17
17
|
import {
|
|
18
18
|
__decorateClass,
|
|
19
19
|
__decorateParam
|
|
@@ -209,4 +209,4 @@ function compareBy(a, b, order) {
|
|
|
209
209
|
export {
|
|
210
210
|
MemoryJobRunService
|
|
211
211
|
};
|
|
212
|
-
//# sourceMappingURL=chunk-
|
|
212
|
+
//# sourceMappingURL=chunk-QSJ3J4HE.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LocalStorageBackend
|
|
3
|
-
} from "./chunk-JWNHNUYL.js";
|
|
4
1
|
import {
|
|
5
2
|
MemoryStorageBackend
|
|
6
3
|
} from "./chunk-3SZFUTXE.js";
|
|
7
4
|
import {
|
|
8
5
|
STORAGE
|
|
9
6
|
} from "./chunk-NYBCQZC7.js";
|
|
7
|
+
import {
|
|
8
|
+
LocalStorageBackend
|
|
9
|
+
} from "./chunk-JWNHNUYL.js";
|
|
10
10
|
import {
|
|
11
11
|
__decorateClass
|
|
12
12
|
} from "./chunk-2E224ZSN.js";
|
|
@@ -37,4 +37,4 @@ StorageModule = __decorateClass([
|
|
|
37
37
|
export {
|
|
38
38
|
StorageModule
|
|
39
39
|
};
|
|
40
|
-
//# sourceMappingURL=chunk-
|
|
40
|
+
//# sourceMappingURL=chunk-RUSUZZAF.js.map
|
|
@@ -4,6 +4,9 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
MemoryJobStore
|
|
6
6
|
} from "./chunk-SNQ3TOWP.js";
|
|
7
|
+
import {
|
|
8
|
+
JOBS_MULTI_TENANT
|
|
9
|
+
} from "./chunk-ZPL74UQN.js";
|
|
7
10
|
import {
|
|
8
11
|
JobCollisionError,
|
|
9
12
|
JobNotReplayableError,
|
|
@@ -11,9 +14,6 @@ import {
|
|
|
11
14
|
JobTypeNotFoundError,
|
|
12
15
|
MissingTenantIdError
|
|
13
16
|
} from "./chunk-T4BIIU5E.js";
|
|
14
|
-
import {
|
|
15
|
-
JOBS_MULTI_TENANT
|
|
16
|
-
} from "./chunk-BIO6F7YI.js";
|
|
17
17
|
import {
|
|
18
18
|
__decorateClass,
|
|
19
19
|
__decorateParam
|
|
@@ -632,4 +632,4 @@ function serialiseError(err, attempt, retryable) {
|
|
|
632
632
|
export {
|
|
633
633
|
MemoryJobOrchestrator
|
|
634
634
|
};
|
|
635
|
-
//# sourceMappingURL=chunk-
|
|
635
|
+
//# sourceMappingURL=chunk-T4YJRD22.js.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CACHE_DEFAULT_TTL
|
|
3
|
-
} from "./chunk-L6FTY45T.js";
|
|
4
1
|
import {
|
|
5
2
|
cacheEntries
|
|
6
3
|
} from "./chunk-FASRXRX5.js";
|
|
4
|
+
import {
|
|
5
|
+
CACHE_DEFAULT_TTL
|
|
6
|
+
} from "./chunk-L6FTY45T.js";
|
|
7
7
|
import {
|
|
8
8
|
DRIZZLE
|
|
9
9
|
} from "./chunk-U64T4YZE.js";
|
|
@@ -109,4 +109,4 @@ DrizzleCacheService = __decorateClass([
|
|
|
109
109
|
export {
|
|
110
110
|
DrizzleCacheService
|
|
111
111
|
};
|
|
112
|
-
//# sourceMappingURL=chunk-
|
|
112
|
+
//# sourceMappingURL=chunk-T6C4LFLC.js.map
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assertTenantId
|
|
3
|
-
} from "./chunk-MZ6GV4YF.js";
|
|
4
1
|
import {
|
|
5
2
|
INTEGRATION_CHANGE_SOURCE,
|
|
6
3
|
INTEGRATION_CURSOR_STORE,
|
|
@@ -9,6 +6,9 @@ import {
|
|
|
9
6
|
INTEGRATION_RUN_RECORDER,
|
|
10
7
|
INTEGRATION_SINK
|
|
11
8
|
} from "./chunk-S7C6TIIF.js";
|
|
9
|
+
import {
|
|
10
|
+
assertTenantId
|
|
11
|
+
} from "./chunk-MZ6GV4YF.js";
|
|
12
12
|
import {
|
|
13
13
|
__decorateClass,
|
|
14
14
|
__decorateParam
|
|
@@ -219,4 +219,4 @@ ExecuteIntegrationUseCase = __decorateClass([
|
|
|
219
219
|
export {
|
|
220
220
|
ExecuteIntegrationUseCase
|
|
221
221
|
};
|
|
222
|
-
//# sourceMappingURL=chunk-
|
|
222
|
+
//# sourceMappingURL=chunk-TDEHU73T.js.map
|
|
@@ -3,6 +3,11 @@ var WebhookChangeSource = class {
|
|
|
3
3
|
label;
|
|
4
4
|
queue;
|
|
5
5
|
externalIdSourceField;
|
|
6
|
+
/**
|
|
7
|
+
* Record field carrying the event id, when `webhook.eventIdField` is
|
|
8
|
+
* configured. Used only as the fallback when the queue iterator does NOT
|
|
9
|
+
* yield an `eventId` — see {@link WebhookFetchCallback} for the precedence.
|
|
10
|
+
*/
|
|
6
11
|
eventIdSourceField;
|
|
7
12
|
composed;
|
|
8
13
|
constructor(opts) {
|
|
@@ -39,19 +44,18 @@ var WebhookChangeSource = class {
|
|
|
39
44
|
subscription,
|
|
40
45
|
cursor
|
|
41
46
|
};
|
|
42
|
-
for await (const {
|
|
47
|
+
for await (const {
|
|
48
|
+
record,
|
|
49
|
+
eventId: yieldedEventId,
|
|
50
|
+
cursor: nextCursor
|
|
51
|
+
} of this.queue(ctx)) {
|
|
43
52
|
const externalIdRaw = record[this.externalIdSourceField];
|
|
44
53
|
if (typeof externalIdRaw !== "string" || externalIdRaw.length === 0) {
|
|
45
54
|
throw new Error(
|
|
46
55
|
`WebhookChangeSource: record missing string '${this.externalIdSourceField}' \u2014 emitted records MUST carry the canonical external id keyed by the mapping source`
|
|
47
56
|
);
|
|
48
57
|
}
|
|
49
|
-
const
|
|
50
|
-
if (typeof eventIdRaw !== "string" || eventIdRaw.length === 0) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`WebhookChangeSource: record missing string '${this.eventIdSourceField}' \u2014 webhook records MUST carry the event id (DetectionConfig.webhook.eventIdField) so Change<T>.dedupKey can be populated`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
58
|
+
const dedupKey = this.deriveDedupKey(yieldedEventId, record);
|
|
55
59
|
const change = {
|
|
56
60
|
externalId: externalIdRaw,
|
|
57
61
|
// Webhook mode cannot distinguish create vs. update vs. delete on
|
|
@@ -62,14 +66,37 @@ var WebhookChangeSource = class {
|
|
|
62
66
|
record,
|
|
63
67
|
cursor: nextCursor ?? null,
|
|
64
68
|
source: "webhook",
|
|
65
|
-
dedupKey
|
|
69
|
+
dedupKey
|
|
66
70
|
};
|
|
67
71
|
yield change;
|
|
68
72
|
}
|
|
69
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Resolve `Change<T>.dedupKey` with the precedence: yielded `eventId` >
|
|
76
|
+
* `webhook.eventIdField` record extraction > `undefined`. A non-empty
|
|
77
|
+
* yielded `eventId` always wins; otherwise the configured field is read off
|
|
78
|
+
* the record (and must be a non-empty string when the field is configured);
|
|
79
|
+
* with neither, `dedupKey` is `undefined` (the orchestrator then has no
|
|
80
|
+
* delivery-level dedup signal for this change).
|
|
81
|
+
*/
|
|
82
|
+
deriveDedupKey(yieldedEventId, record) {
|
|
83
|
+
if (yieldedEventId !== void 0 && yieldedEventId.length > 0) {
|
|
84
|
+
return yieldedEventId;
|
|
85
|
+
}
|
|
86
|
+
if (this.eventIdSourceField === void 0) {
|
|
87
|
+
return void 0;
|
|
88
|
+
}
|
|
89
|
+
const eventIdRaw = record[this.eventIdSourceField];
|
|
90
|
+
if (typeof eventIdRaw !== "string" || eventIdRaw.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`WebhookChangeSource: record missing string '${this.eventIdSourceField}' \u2014 a webhook record MUST carry the event id (DetectionConfig.webhook.eventIdField) so Change<T>.dedupKey can be populated, unless the queue iterator yields an 'eventId' alongside the record`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return eventIdRaw;
|
|
96
|
+
}
|
|
70
97
|
};
|
|
71
98
|
|
|
72
99
|
export {
|
|
73
100
|
WebhookChangeSource
|
|
74
101
|
};
|
|
75
|
-
//# sourceMappingURL=chunk-
|
|
102
|
+
//# sourceMappingURL=chunk-TIZXQU26.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../runtime/subsystems/integration/webhook-change-source.ts"],"sourcesContent":["/**\n * Integration subsystem — `WebhookChangeSource<T>` primitive (#226-4, ADR-033).\n *\n * Generic webhook-mode `IChangeSource<T>` implementation parameterized by a\n * parsed `DetectionConfig` (webhook mode) and a consumer-supplied\n * `WebhookFetchCallback<T>` that iterates a consumer-owned inbound staging\n * queue. The primitive owns:\n *\n * - canonical `Change<T>.source = 'webhook'` stamping;\n * - `dedupKey` derivation, preferring the `eventId` yielded alongside the\n * record by the queue iterator, and falling back to the configured\n * `webhook.eventIdField` on the emitted record when no `eventId` is yielded\n * (precedence: yielded `eventId` > `eventIdField` record extraction >\n * undefined `dedupKey`);\n * - `externalId` derivation: the mapping entry whose `target === 'external_id'`\n * names — via its `source` — the field on the emitted record that carries\n * the canonical external id (mirrors `PollChangeSource`);\n * - middleware-chain composition via the locked `ChangeMiddleware<T>` shape\n * (#226-1) — same composition seam as the poll primitive.\n *\n * The primitive is **passive**: it iterates whatever the consumer-owned\n * queue yields. It does NOT synchronously drive the orchestrator, does NOT\n * own a transport, and does NOT manage acks. The inbound staging table\n * schema is consumer-owned and deferred per ADR-0002 §Phase 4 — the\n * `WebhookFetchCallback<T>` is the queue contract the consumer injects.\n *\n * Shape locks (decision memo Q5, mirrored from poll primitive):\n * - `WebhookFetchContext = { subscription, cursor }` — explicitly NO\n * `userId` / `tenantId`. Run-scope identity is closed over by the\n * consumer at queue construction or resolved inside the callback via\n * consumer services. There are no `filters` on the webhook context —\n * filtering is done at registration / on the staging row, not at the\n * port seam (the queue is already filtered by the time the primitive\n * iterates).\n *\n * Long-lived streaming CDC primitives (SFDC Pub-Sub gRPC, Debezium/Kafka,\n * Postgres logical replication) are deferred to `#226-8` — they need a\n * fundamentally different lifecycle (`subscribe(onChange, onError)`,\n * server-paced backpressure, ack-on-yield) and shouldn't be retrofitted\n * into either this primitive or the poll primitive.\n */\n\nimport type { DetectionConfig } from \"./detection-config.schema\";\nimport type {\n\tChange,\n\tIChangeSource,\n\tIntegrationSubscriptionView,\n} from \"./integration-change-source.protocol\";\nimport type {\n\tChangeIterator,\n\tChangeMiddleware,\n} from \"./integration-middleware.protocol\";\n\n// ============================================================================\n// Cursor + queue callback shapes\n// ============================================================================\n\n/**\n * Opaque webhook cursor shape. Webhook mode typically has a cursor of\n * `{ ts: ISO-string }` (last drained staging-row timestamp) but the\n * primitive treats it as opaque. Consumer-owned queue iterators interpret\n * it however the staging schema needs.\n */\nexport type WebhookCursor = unknown;\n\n/**\n * Context the primitive forwards to the queue iterator. Locked to exactly\n * two fields per the same Q5 reasoning that locks `PollFetchContext` — no\n * `userId` / `tenantId`.\n */\nexport interface WebhookFetchContext {\n\treadonly subscription: IntegrationSubscriptionView;\n\treadonly cursor: WebhookCursor | null;\n}\n\n/**\n * Consumer-supplied queue iterator. Returns an async iterable of\n * `{ record, eventId?, cursor? }` tuples — the consumer drains the inbound\n * staging queue and emits already-mapped canonical records `T`. The primitive\n * stamps `source: 'webhook'` and derives `dedupKey` with this precedence:\n *\n * 1. the yielded `eventId` (vendor delivery metadata — the queue is the\n * right channel for it: a vendor's event id should never need a field\n * on the vendor-neutral canonical record);\n * 2. else the record field named by `webhook.eventIdField`, when configured;\n * 3. else `undefined`.\n *\n * Yielding `eventId` is the safe channel when one canonical record identity\n * (the `external_id`) can recur across distinct vendor events in a single\n * drain batch — e.g. a message create and its later edit share an\n * `external_id` but are different events. Reading dedup identity off the\n * record (`eventIdField`) collapses those into one `dedupKey`; the yielded\n * `eventId` keeps them distinct. The consumer is the one who decided when a\n * staging row is \"ready\" to drain.\n *\n * Webhook mode has no per-record cursor advance — the staging-row drain\n * order is consumer policy (FIFO by ingestion timestamp, by event id, etc.)\n * and is opaque to the primitive. The orchestrator's last-yielded cursor\n * is whatever the consumer chooses to surface, if anything.\n */\nexport type WebhookFetchCallback<T> = (\n\tctx: WebhookFetchContext,\n) => AsyncIterable<{ record: T; eventId?: string; cursor?: WebhookCursor }>;\n\n// ============================================================================\n// Constructor options\n// ============================================================================\n\nexport interface WebhookChangeSourceOptions<T> {\n\t/** Consumer-supplied inbound queue iterator. */\n\treadonly queue: WebhookFetchCallback<T>;\n\t/**\n\t * Parsed detection config. MUST be `mode: 'webhook'`; the constructor\n\t * throws if a poll config is supplied. Codegen-emitted factories call\n\t * `DetectionConfigSchema.parse(...)` upstream so this is a safety net,\n\t * not the primary validation point.\n\t */\n\treadonly config: DetectionConfig;\n\t/**\n\t * Optional middleware chain. Same shape and composition rules as\n\t * `PollChangeSource` — first element is the outermost layer.\n\t */\n\treadonly middlewares?: ReadonlyArray<ChangeMiddleware<T>>;\n\t/**\n\t * Optional human label for run logs (e.g. `'stripe-webhook-charge'`).\n\t * Defaults to a derived label based on the mapping at construction.\n\t */\n\treadonly label?: string;\n}\n\n// ============================================================================\n// WebhookChangeSource<T>\n// ============================================================================\n\nexport class WebhookChangeSource<T> implements IChangeSource<T> {\n\tpublic readonly label: string;\n\n\tprivate readonly queue: WebhookFetchCallback<T>;\n\tprivate readonly externalIdSourceField: string;\n\t/**\n\t * Record field carrying the event id, when `webhook.eventIdField` is\n\t * configured. Used only as the fallback when the queue iterator does NOT\n\t * yield an `eventId` — see {@link WebhookFetchCallback} for the precedence.\n\t */\n\tprivate readonly eventIdSourceField: string | undefined;\n\tprivate readonly composed: ChangeIterator<T>;\n\n\tconstructor(opts: WebhookChangeSourceOptions<T>) {\n\t\tif (opts.config.mode !== \"webhook\") {\n\t\t\tthrow new Error(\n\t\t\t\t`WebhookChangeSource requires DetectionConfig.mode === 'webhook'; got '${(opts.config as { mode: string }).mode}'`,\n\t\t\t);\n\t\t}\n\t\tconst config = opts.config;\n\n\t\t// Field mapping: locate the entry whose canonical `target` is `external_id`\n\t\t// — mirrors the poll primitive's contract. Adapters emit records\n\t\t// already-mapped; the primitive needs to know which key on T carries the\n\t\t// external id so it can stamp `Change.externalId`. That key is the\n\t\t// mapping's `source` (the field on the emitted record), NOT its `target`\n\t\t// (the canonical column) — they differ whenever the canonical record is\n\t\t// vendor-neutral camelCase (e.g. `source: 'externalId'` → `target: 'external_id'`).\n\t\tconst externalIdMapping = config.mapping.find(\n\t\t\t(m) => m.target === \"external_id\",\n\t\t);\n\t\tif (!externalIdMapping) {\n\t\t\tthrow new Error(\n\t\t\t\t\"WebhookChangeSource: DetectionConfig.mapping must include an entry with target 'external_id' so emitted Change<T>.externalId can be populated\",\n\t\t\t);\n\t\t}\n\t\tthis.externalIdSourceField = externalIdMapping.source;\n\t\tthis.eventIdSourceField = config.webhook.eventIdField;\n\t\t// `eventIdField` is optional (a callback that always yields `eventId` need\n\t\t// not declare one); `undefined` here just disables the fallback extraction.\n\n\t\tthis.queue = opts.queue;\n\n\t\tthis.label =\n\t\t\topts.label ?? `webhook-change-source:${externalIdMapping.source}`;\n\n\t\t// Compose middleware chain — same shape as PollChangeSource.\n\t\tconst inner: ChangeIterator<T> = (sub, cur) => this.fetch(sub, cur);\n\t\tconst middlewares = opts.middlewares ?? [];\n\t\tthis.composed = middlewares.reduceRight<ChangeIterator<T>>(\n\t\t\t(next, mw) => mw(next),\n\t\t\tinner,\n\t\t);\n\t}\n\n\tlistChanges(\n\t\tsubscription: IntegrationSubscriptionView,\n\t\tcursor: unknown | null,\n\t): AsyncIterable<Change<T>> {\n\t\treturn this.composed(subscription, cursor);\n\t}\n\n\tprivate async *fetch(\n\t\tsubscription: IntegrationSubscriptionView,\n\t\tcursor: unknown | null,\n\t): AsyncIterable<Change<T>> {\n\t\tconst ctx: WebhookFetchContext = {\n\t\t\tsubscription,\n\t\t\tcursor: cursor as WebhookCursor | null,\n\t\t};\n\n\t\tfor await (const {\n\t\t\trecord,\n\t\t\teventId: yieldedEventId,\n\t\t\tcursor: nextCursor,\n\t\t} of this.queue(ctx)) {\n\t\t\tconst externalIdRaw = (record as Record<string, unknown>)[\n\t\t\t\tthis.externalIdSourceField\n\t\t\t];\n\t\t\tif (typeof externalIdRaw !== \"string\" || externalIdRaw.length === 0) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`WebhookChangeSource: record missing string '${this.externalIdSourceField}' — emitted records MUST carry the canonical external id keyed by the mapping source`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// dedupKey precedence: yielded `eventId` > `eventIdField` record\n\t\t\t// extraction > undefined. The yielded id is vendor delivery metadata\n\t\t\t// (the right channel for it), and keeps distinct vendor events for the\n\t\t\t// same `external_id` (e.g. a message and its edit) from collapsing to\n\t\t\t// one dedupKey — which a record-field extraction would do.\n\t\t\tconst dedupKey = this.deriveDedupKey(yieldedEventId, record);\n\n\t\t\tconst change: Change<T> = {\n\t\t\t\texternalId: externalIdRaw,\n\t\t\t\t// Webhook mode cannot distinguish create vs. update vs. delete on\n\t\t\t\t// its own — the orchestrator's diff stage handles classification.\n\t\t\t\t// Tombstone / soft-delete detection is consumer-driven (same as\n\t\t\t\t// poll mode — see ADR-033).\n\t\t\t\toperation: \"updated\",\n\t\t\t\trecord,\n\t\t\t\tcursor: nextCursor ?? null,\n\t\t\t\tsource: \"webhook\",\n\t\t\t\tdedupKey,\n\t\t\t};\n\t\t\tyield change;\n\t\t}\n\t}\n\n\t/**\n\t * Resolve `Change<T>.dedupKey` with the precedence: yielded `eventId` >\n\t * `webhook.eventIdField` record extraction > `undefined`. A non-empty\n\t * yielded `eventId` always wins; otherwise the configured field is read off\n\t * the record (and must be a non-empty string when the field is configured);\n\t * with neither, `dedupKey` is `undefined` (the orchestrator then has no\n\t * delivery-level dedup signal for this change).\n\t */\n\tprivate deriveDedupKey(\n\t\tyieldedEventId: string | undefined,\n\t\trecord: T,\n\t): string | undefined {\n\t\tif (yieldedEventId !== undefined && yieldedEventId.length > 0) {\n\t\t\treturn yieldedEventId;\n\t\t}\n\t\tif (this.eventIdSourceField === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst eventIdRaw = (record as Record<string, unknown>)[\n\t\t\tthis.eventIdSourceField\n\t\t];\n\t\tif (typeof eventIdRaw !== \"string\" || eventIdRaw.length === 0) {\n\t\t\tthrow new Error(\n\t\t\t\t`WebhookChangeSource: record missing string '${this.eventIdSourceField}' — a webhook record MUST carry the event id (DetectionConfig.webhook.eventIdField) so Change<T>.dedupKey can be populated, unless the queue iterator yields an 'eventId' alongside the record`,\n\t\t\t);\n\t\t}\n\t\treturn eventIdRaw;\n\t}\n}\n"],"mappings":";AAsIO,IAAM,sBAAN,MAAyD;AAAA,EAC/C;AAAA,EAEC;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA;AAAA,EACA;AAAA,EAEjB,YAAY,MAAqC;AAChD,QAAI,KAAK,OAAO,SAAS,WAAW;AACnC,YAAM,IAAI;AAAA,QACT,yEAA0E,KAAK,OAA4B,IAAI;AAAA,MAChH;AAAA,IACD;AACA,UAAM,SAAS,KAAK;AASpB,UAAM,oBAAoB,OAAO,QAAQ;AAAA,MACxC,CAAC,MAAM,EAAE,WAAW;AAAA,IACrB;AACA,QAAI,CAAC,mBAAmB;AACvB,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAC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B,KAAK,kBACN;AACA,QAAI,OAAO,eAAe,YAAY,WAAW,WAAW,GAAG;AAC9D,YAAM,IAAI;AAAA,QACT,+CAA+C,KAAK,kBAAkB;AAAA,MACvE;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACD;","names":[]}
|
|
@@ -3,10 +3,10 @@ import {
|
|
|
3
3
|
} from "./chunk-GM3RMJIJ.js";
|
|
4
4
|
import {
|
|
5
5
|
DrizzleEventBus
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-H6FO2ZDJ.js";
|
|
7
7
|
import {
|
|
8
8
|
MemoryEventBus
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-LQ6PYFU6.js";
|
|
10
10
|
import {
|
|
11
11
|
EVENTS_MODULE_OPTIONS,
|
|
12
12
|
EVENTS_MULTI_TENANT,
|
|
@@ -152,4 +152,4 @@ EventsModule = __decorateClass([
|
|
|
152
152
|
export {
|
|
153
153
|
EventsModule
|
|
154
154
|
};
|
|
155
|
-
//# sourceMappingURL=chunk-
|
|
155
|
+
//# sourceMappingURL=chunk-TKVTEUBD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../runtime/subsystems/events/events.module.ts"],"sourcesContent":["/**\n * EventsModule — DynamicModule factory for the event bus subsystem.\n *\n * Register once in AppModule:\n * ```typescript\n * @Module({\n * imports: [\n * EventsModule.forRoot({ backend: 'drizzle' }),\n * ],\n * })\n * export class AppModule {}\n * ```\n *\n * Tests swap to the memory backend without touching application code:\n * ```typescript\n * Test.createTestingModule({\n * imports: [EventsModule.forRoot({ backend: 'memory' })],\n * });\n * ```\n *\n * Per-pool drain isolation (EVT-4):\n * ```typescript\n * EventsModule.forRoot({ backend: 'drizzle', pools: ['events_change'] });\n * ```\n * Each process restricts its drain loop to the pools listed here. `pools`\n * is undefined by default → drain all pending rows (backwards-compatible).\n *\n * Typed facade + multi-tenancy (EVT-6):\n * - `TYPED_EVENT_BUS` resolves to the generated `TypedEventBus` wrapping\n * whichever backend is selected.\n * - `multiTenant: true` makes `TypedEventBus.publish` throw\n * `MissingTenantIdError` when the caller forgets `metadata.tenantId`.\n *\n * `global: true` means entity modules do not need to import EventsModule\n * individually — the EVENT_BUS and TYPED_EVENT_BUS tokens are available\n * project-wide.\n *\n * Async configuration (`forRootAsync`):\n * The async factory returns `EventsModuleOptions`; the EVENT_BUS provider\n * then receives its backend dependencies — DRIZZLE for the drizzle\n * backend, REDIS_URL for the redis backend, the resolved options for the\n * memory backend — through a proper `useFactory` so Nest DI wires them\n * correctly. Earlier revisions hand-constructed backends with\n * `new Class()` which silently left `db` / `redisUrl` undefined\n * (issue #108).\n */\nimport { Module, type DynamicModule, type Provider, type Type } from '@nestjs/common';\nimport {\n EVENT_BUS,\n EVENT_READ_PORT,\n EVENTS_MODULE_OPTIONS,\n EVENTS_MULTI_TENANT,\n REDIS_URL,\n TYPED_EVENT_BUS,\n} from './events.tokens';\nimport { DRIZZLE } from '../../constants/tokens';\nimport type { DrizzleClient } from '../../types/drizzle';\nimport { DrizzleEventBus } from './event-bus.drizzle-backend';\nimport { MemoryEventBus } from './event-bus.memory-backend';\n// #6 — `RedisEventBus` is lazy-loaded only when `backend: 'redis'` is selected.\n// The file is filtered out of the vendor set for non-redis installs (see\n// `backendFileFilter` in src/cli/commands/subsystem.ts); the dynamic-string\n// import below makes TS treat the specifier as `any` so the consumer's tsc\n// never tries to resolve the absent file.\nimport { TypedEventBus } from './generated/bus';\n\n/**\n * Lazy-load the Redis backend. Routed through a non-literal specifier so\n * the consumer's `tsc` doesn't resolve `./event-bus.redis-backend` at type\n * check time — important because that file is filtered out of drizzle/\n * memory installs (#6).\n */\nasync function loadRedisEventBus(): Promise<new (url: string) => object> {\n // Non-literal specifier — TS gives this an `any` module type, sidestepping\n // resolution of a file that may not be vendored.\n const specifier = './event-bus.redis-backend';\n const mod = (await import(specifier)) as { RedisEventBus: new (url: string) => object };\n return mod.RedisEventBus;\n}\n\nexport interface EventsModuleOptions {\n backend: 'drizzle' | 'memory' | 'redis';\n /**\n * Redis connection URL used when `backend` is `'redis'`.\n * Falls back to the REDIS_URL environment variable, then\n * `redis://localhost:6379` if neither is set.\n */\n redisUrl?: string;\n /**\n * Restrict the drain loop to these pools. Each pool name matches the\n * `domain_events.pool` column (populated from `event.metadata.pool` at\n * publish time). Leave undefined to drain all pending rows.\n *\n * Typical lane split: one process per `events_inbound` /\n * `events_change` / `events_outbound` so a slow outbound handler\n * cannot stall change-event propagation (see ADR-022).\n */\n pools?: string[];\n /**\n * LISTEN-NOTIFY-1 — when `true` (drizzle backend only), the drainer holds a\n * dedicated listener connection and LISTENs on `codegen_events_wake`. Each\n * `publish`/`publishMany` emits an in-tx `pg_notify(codegen_events_wake,\n * <pool>)` so the drainer wakes the moment the publishing transaction commits,\n * instead of waiting for the next poll tick. Polling continues unchanged as\n * the fallback heartbeat; a lost notify degrades to poll latency, never to\n * lost work. Defaults to `false`.\n *\n * Ignored by the memory + redis backends (memory dispatches inline; redis has\n * its own fan-out). Requires a direct (non-transaction-pooler) connection —\n * see the events/jobs config block re: PgBouncer.\n */\n listenNotify?: boolean;\n /**\n * Multi-tenancy opt-in (EVT-6).\n *\n * When `true`, every `TypedEventBus.publish()` call must supply\n * `opts.metadata.tenantId` — otherwise it throws `MissingTenantIdError`.\n * The tenantId is preserved on `event.metadata` and, for the Drizzle\n * backend, written to `domain_events.tenant_id` (EVT-4).\n *\n * Drain-side tenant filtering is deferred — the tenant-context model\n * (per-process vs. per-request vs. async-local-storage) is still\n * unsettled; see ADR-024 §Multi-tenancy. Only the publish-side\n * requirement ships here.\n *\n * Defaults to `false`.\n */\n multiTenant?: boolean;\n /**\n * The generated `TypedEventBus` class to bind to `TYPED_EVENT_BUS`.\n *\n * **Package mode (ADR-037).** When the runtime is imported from\n * `@pattern-stack/codegen` (not vendored), the bundled `./generated/bus`\n * `TypedEventBus` is typed to an EMPTY event union and reads the bundled\n * empty `eventRegistry` — a consumer's `events/*.yaml` are scanned into\n * `src/generated/events/bus.ts` (typed to THEIR union, reading THEIR\n * registry), which the package can't import. The generated subsystem barrel\n * therefore threads that class in here:\n * `EventsModule.forRoot({ ..., typedBus: TypedEventBus })`. Nest constructs\n * it with this module's `EVENT_BUS` + `EVENTS_MULTI_TENANT` providers (the\n * generated class injects the same string-valued tokens, which match across\n * the package boundary).\n *\n * Omitted (vendored mode / tests) ⇒ falls back to the bundled\n * `./generated/bus`, which in a vendored tree IS the consumer's generated\n * file. Without this, a package-mode consumer's typed `publish<'…'>()` calls\n * resolve against the empty union and their events never get the right\n * `pool` / `direction` stamped.\n *\n * Only consulted by `forRoot` (the path the barrel emits); `forRootAsync`\n * keeps the bundled bus.\n */\n typedBus?: Type<unknown>;\n}\n\nexport interface EventsModuleAsyncOptions {\n useFactory: (...args: unknown[]) => Promise<EventsModuleOptions> | EventsModuleOptions;\n inject?: unknown[];\n imports?: unknown[];\n}\n\n/**\n * Shared provider set: `TypedEventBus` itself, the `TYPED_EVENT_BUS` token\n * binding, and the resolved `EVENTS_MULTI_TENANT` flag. Returned from one\n * place so every `forRoot` branch and `forRootAsync` agree.\n */\nfunction buildTypedBusProviders(\n multiTenant: boolean,\n typedBus?: Type<unknown>,\n): Provider[] {\n // Package mode threads the consumer's generated `TypedEventBus` (typed to\n // their event union, reading their registry) via `typedBus`; vendored mode\n // omits it and we fall back to the bundled `./generated/bus` (which IS the\n // consumer's generated file in a vendored tree). See `EventsModuleOptions.typedBus`.\n const BusClass = typedBus ?? TypedEventBus;\n return [\n BusClass,\n { provide: TYPED_EVENT_BUS, useExisting: BusClass },\n { provide: EVENTS_MULTI_TENANT, useValue: multiTenant },\n ];\n}\n\n/**\n * Construct the backend instance for the async path, routing constructor\n * arguments through Nest-resolved dependencies.\n *\n * DRIZZLE is declared optional at inject time so that memory-backend\n * consumers aren't required to also import `DatabaseModule`. If the\n * drizzle backend is selected but no DRIZZLE provider is registered, we\n * throw a clear error instead of silently constructing a broken bus.\n */\nasync function buildEventBusAsync(\n options: EventsModuleOptions,\n db: DrizzleClient | null,\n redisUrl: string,\n): Promise<unknown> {\n if (options.backend === 'drizzle') {\n if (!db) {\n throw new Error(\n \"EventsModule.forRootAsync: backend: 'drizzle' selected but DRIZZLE provider is not available. \" +\n 'Ensure DatabaseModule (or another provider exposing DRIZZLE) is imported before EventsModule.forRootAsync.',\n );\n }\n return new DrizzleEventBus(db, options);\n }\n if (options.backend === 'redis') {\n // #6: lazy import — the redis backend ships only with `--backend redis`\n // installs; drizzle/memory consumers never touch the file.\n const RedisEventBus = await loadRedisEventBus();\n return new RedisEventBus(redisUrl);\n }\n return new MemoryEventBus(options);\n}\n\n@Module({})\nexport class EventsModule {\n static forRootAsync(asyncOptions: EventsModuleAsyncOptions): DynamicModule {\n return {\n module: EventsModule,\n global: true,\n imports: (asyncOptions.imports ?? []) as Parameters<typeof Module>[0]['imports'],\n providers: [\n {\n provide: EVENTS_MODULE_OPTIONS,\n useFactory: asyncOptions.useFactory,\n inject: (asyncOptions.inject ?? []) as (string | symbol | Function)[],\n },\n {\n provide: EVENTS_MULTI_TENANT,\n useFactory: (options: EventsModuleOptions) => options.multiTenant ?? false,\n inject: [EVENTS_MODULE_OPTIONS],\n },\n {\n provide: REDIS_URL,\n useFactory: (options: EventsModuleOptions) =>\n options.redisUrl ?? process.env['REDIS_URL'] ?? 'redis://localhost:6379',\n inject: [EVENTS_MODULE_OPTIONS],\n },\n {\n provide: EVENT_BUS,\n useFactory: (\n options: EventsModuleOptions,\n db: DrizzleClient | null,\n redisUrl: string,\n ) => buildEventBusAsync(options, db, redisUrl),\n inject: [\n EVENTS_MODULE_OPTIONS,\n { token: DRIZZLE, optional: true },\n REDIS_URL,\n ],\n },\n {\n // Read port (OBS-LIST-1). Drizzle + memory backends implement\n // IEventReadPort on the EVENT_BUS instance; the redis backend\n // retains no history, so EVENT_READ_PORT resolves to `null` and\n // optional consumers (the observability combiner) degrade to\n // empty results.\n provide: EVENT_READ_PORT,\n useFactory: (options: EventsModuleOptions, bus: unknown) =>\n options.backend === 'redis' ? null : bus,\n inject: [EVENTS_MODULE_OPTIONS, EVENT_BUS],\n },\n TypedEventBus,\n { provide: TYPED_EVENT_BUS, useExisting: TypedEventBus },\n ],\n exports: [EVENT_BUS, EVENT_READ_PORT, TYPED_EVENT_BUS, EVENTS_MULTI_TENANT],\n };\n }\n\n static forRoot(\n options: EventsModuleOptions = { backend: 'drizzle' },\n ): DynamicModule {\n const multiTenant = options.multiTenant ?? false;\n\n if (options.backend === 'redis') {\n const resolvedUrl =\n options.redisUrl ?? process.env['REDIS_URL'] ?? 'redis://localhost:6379';\n\n return {\n module: EventsModule,\n global: true,\n providers: [\n { provide: EVENTS_MODULE_OPTIONS, useValue: options },\n { provide: REDIS_URL, useValue: resolvedUrl },\n {\n // #6: useFactory + dynamic import so the consumer's tsc never\n // needs to resolve `event-bus.redis-backend.ts` for drizzle/\n // memory installs (the file is filtered out by\n // `backendFileFilter`). Nest awaits async factories + manages\n // lifecycle on the returned instance, so we drop the old bare\n // `RedisEventBus` provider entry.\n provide: EVENT_BUS,\n useFactory: async (url: string): Promise<object> => {\n const RedisEventBus = await loadRedisEventBus();\n return new RedisEventBus(url);\n },\n inject: [REDIS_URL],\n },\n ...buildTypedBusProviders(multiTenant, options.typedBus),\n ],\n exports: [EVENT_BUS, TYPED_EVENT_BUS, EVENTS_MULTI_TENANT],\n };\n }\n\n const provider =\n options.backend === 'drizzle'\n ? { provide: EVENT_BUS, useClass: DrizzleEventBus }\n : { provide: EVENT_BUS, useClass: MemoryEventBus };\n\n return {\n module: EventsModule,\n global: true,\n providers: [\n { provide: EVENTS_MODULE_OPTIONS, useValue: options },\n provider,\n // Read port (OBS-LIST-1): drizzle + memory backends implement\n // IEventReadPort on the same instance as EVENT_BUS. The redis\n // backend retains no history and does not provide this token.\n { provide: EVENT_READ_PORT, useExisting: EVENT_BUS },\n ...buildTypedBusProviders(multiTenant, options.typedBus),\n ],\n exports: [EVENT_BUS, EVENT_READ_PORT, TYPED_E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,WAAW,UAAU,gBAAgB,IAChD,EAAE,SAAS,WAAW,UAAU,eAAe;AAErD,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,WAAW;AAAA,QACT,EAAE,SAAS,uBAAuB,UAAU,QAAQ;AAAA,QACpD;AAAA;AAAA;AAAA;AAAA,QAIA,EAAE,SAAS,iBAAiB,aAAa,UAAU;AAAA,QACnD,GAAG,uBAAuB,aAAa,QAAQ,QAAQ;AAAA,MACzD;AAAA,MACA,SAAS,CAAC,WAAW,iBAAiB,iBAAiB,mBAAmB;AAAA,IAC5E;AAAA,EACF;AACF;AA7Ga,eAAN;AAAA,EADN,OAAO,CAAC,CAAC;AAAA,GACG;","names":[]}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
JOB_RUN_SERVICE
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import {
|
|
5
|
-
EVENT_READ_PORT
|
|
6
|
-
} from "./chunk-H5NH7KPE.js";
|
|
3
|
+
} from "./chunk-ZPL74UQN.js";
|
|
7
4
|
import {
|
|
8
5
|
BRIDGE_DELIVERY_REPO
|
|
9
6
|
} from "./chunk-4LH67P4U.js";
|
|
7
|
+
import {
|
|
8
|
+
EVENT_READ_PORT
|
|
9
|
+
} from "./chunk-H5NH7KPE.js";
|
|
10
10
|
import {
|
|
11
11
|
INTEGRATION_CURSOR_STORE,
|
|
12
12
|
INTEGRATION_RUN_RECORDER
|
|
@@ -181,4 +181,4 @@ ObservabilityService = __decorateClass([
|
|
|
181
181
|
export {
|
|
182
182
|
ObservabilityService
|
|
183
183
|
};
|
|
184
|
-
//# sourceMappingURL=chunk-
|
|
184
|
+
//# sourceMappingURL=chunk-W2UIDI3R.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-W4HOHZVF.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
INTEGRATION_MULTI_TENANT
|
|
3
|
+
} from "./chunk-S7C6TIIF.js";
|
|
1
4
|
import {
|
|
2
5
|
assertTenantId
|
|
3
6
|
} from "./chunk-MZ6GV4YF.js";
|
|
4
7
|
import {
|
|
5
8
|
integrationSubscriptions
|
|
6
9
|
} from "./chunk-HNWZFNKP.js";
|
|
7
|
-
import {
|
|
8
|
-
INTEGRATION_MULTI_TENANT
|
|
9
|
-
} from "./chunk-S7C6TIIF.js";
|
|
10
10
|
import {
|
|
11
11
|
DRIZZLE
|
|
12
12
|
} from "./chunk-U64T4YZE.js";
|
|
@@ -97,4 +97,4 @@ PostgresCursorStore = __decorateClass([
|
|
|
97
97
|
export {
|
|
98
98
|
PostgresCursorStore
|
|
99
99
|
};
|
|
100
|
-
//# sourceMappingURL=chunk-
|
|
100
|
+
//# sourceMappingURL=chunk-XWBK3XJK.js.map
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FieldDiffSchema
|
|
3
3
|
} from "./chunk-SQDOBLBP.js";
|
|
4
|
+
import {
|
|
5
|
+
INTEGRATION_MULTI_TENANT
|
|
6
|
+
} from "./chunk-S7C6TIIF.js";
|
|
4
7
|
import {
|
|
5
8
|
assertTenantId
|
|
6
9
|
} from "./chunk-MZ6GV4YF.js";
|
|
@@ -9,9 +12,6 @@ import {
|
|
|
9
12
|
integrationRuns,
|
|
10
13
|
integrationSubscriptions
|
|
11
14
|
} from "./chunk-HNWZFNKP.js";
|
|
12
|
-
import {
|
|
13
|
-
INTEGRATION_MULTI_TENANT
|
|
14
|
-
} from "./chunk-S7C6TIIF.js";
|
|
15
15
|
import {
|
|
16
16
|
DRIZZLE
|
|
17
17
|
} from "./chunk-U64T4YZE.js";
|
|
@@ -127,4 +127,4 @@ DrizzleIntegrationRunRecorder = __decorateClass([
|
|
|
127
127
|
export {
|
|
128
128
|
DrizzleIntegrationRunRecorder
|
|
129
129
|
};
|
|
130
|
-
//# sourceMappingURL=chunk-
|
|
130
|
+
//# sourceMappingURL=chunk-YK5JEVLX.js.map
|