@mastra/core
Version:
1 lines • 4.9 kB
Source Map (JSON)
{"version":3,"file":"pull-transport-BlEVfOcc.cjs","names":["#pubsub","#group","#topic","#logger","#callbacks"],"sources":["../src/worker/worker.ts","../src/worker/transport/pull-transport.ts"],"sourcesContent":["import type { PubSub } from '../events/pubsub';\nimport type { IMastraLogger } from '../logger';\nimport type { Mastra } from '../mastra';\nimport type { MastraCompositeStore } from '../storage';\n\n/**\n * Infrastructure dependencies provided to workers during initialization.\n */\nexport interface WorkerDeps {\n pubsub: PubSub;\n storage: MastraCompositeStore;\n logger: IMastraLogger;\n mastra?: Mastra;\n}\n\n/**\n * Abstract base class for Mastra workers.\n *\n * Each worker is a self-contained, independently deployable unit of\n * background processing. Concrete implementations include:\n * - OrchestrationWorker: processes workflow events\n * - SchedulerWorker: fires cron-based workflow schedules\n * - BackgroundTaskWorker: manages background tool execution\n *\n * Workers are registered on a Mastra instance and run inline by default.\n * They can also be launched standalone via the CLI for separate deployment.\n */\nexport abstract class MastraWorker {\n abstract readonly name: string;\n\n protected mastra?: Mastra;\n protected deps?: WorkerDeps;\n\n /** Called by Mastra during registration to provide the instance reference. */\n __registerMastra(mastra: Mastra): void {\n this.mastra = mastra;\n }\n\n /** Initialize with infrastructure deps. Called before start(). */\n async init(deps: WorkerDeps): Promise<void> {\n this.deps = deps;\n if (!this.mastra && deps.mastra) {\n this.mastra = deps.mastra;\n }\n }\n\n abstract start(): Promise<void>;\n abstract stop(): Promise<void>;\n abstract get isRunning(): boolean;\n}\n","import type { PubSub } from '../../events/pubsub';\nimport type { EventCallback } from '../../events/types';\nimport type { IMastraLogger } from '../../logger';\nimport type { EventRouter, WorkerTransport } from './transport';\n\nconst TOPIC_WORKFLOWS = 'workflows';\n\nexport class PullTransport implements WorkerTransport {\n #pubsub: PubSub;\n #group: string;\n #topic: string;\n #logger?: IMastraLogger;\n #callbacks: Array<{ topic: string; cb: EventCallback }> = [];\n\n constructor({\n pubsub,\n group,\n topic,\n logger,\n }: {\n pubsub: PubSub;\n group: string;\n /** Pubsub topic to subscribe to. Defaults to the workflows topic. */\n topic?: string;\n logger?: IMastraLogger;\n }) {\n this.#pubsub = pubsub;\n this.#group = group;\n this.#topic = topic ?? TOPIC_WORKFLOWS;\n this.#logger = logger;\n }\n\n async start(router: EventRouter): Promise<void> {\n if (this.#callbacks.length > 0) {\n this.#logger?.debug('[PullTransport] start() called while already subscribed; ignoring duplicate call');\n return;\n }\n const cb: EventCallback = (event, ack, nack) => {\n // route() is async; surface unexpected rejections as a nack instead\n // of an unhandledRejection. The router's own try/catch already turns\n // expected processing errors into nack — this guard only catches\n // synchronous-throw-becomes-rejected-promise leaks.\n router.route(event, ack, nack).catch(err => {\n try {\n // Best-effort: ack/nack are optional in some PubSub backends.\n if (typeof nack === 'function') {\n void nack();\n }\n } finally {\n this.#logger?.error('[PullTransport] router.route rejected', { err });\n }\n });\n };\n await this.#pubsub.subscribe(this.#topic, cb, { group: this.#group });\n this.#callbacks.push({ topic: this.#topic, cb });\n }\n\n async stop(): Promise<void> {\n for (const { topic, cb } of this.#callbacks) {\n await this.#pubsub.unsubscribe(topic, cb);\n }\n this.#callbacks = [];\n await this.#pubsub.flush();\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA2BA,IAAsB,eAAtB,MAAmC;CAGjC;CACA;;CAGA,iBAAiB,QAAsB;EACrC,KAAK,SAAS;CAChB;;CAGA,MAAM,KAAK,MAAiC;EAC1C,KAAK,OAAO;EACZ,IAAI,CAAC,KAAK,UAAU,KAAK,QACvB,KAAK,SAAS,KAAK;CAEvB;AAKF;;;AC5CA,MAAM,kBAAkB;AAExB,IAAa,gBAAb,MAAsD;CACpD;CACA;CACA;CACA;CACA,aAA0D,CAAC;CAE3D,YAAY,EACV,QACA,OACA,OACA,UAOC;EACD,KAAKA,UAAU;EACf,KAAKC,SAAS;EACd,KAAKC,SAAS,SAAS;EACvB,KAAKC,UAAU;CACjB;CAEA,MAAM,MAAM,QAAoC;EAC9C,IAAI,KAAKC,WAAW,SAAS,GAAG;GAC9B,KAAKD,SAAS,MAAM,kFAAkF;GACtG;EACF;EACA,MAAM,MAAqB,OAAO,KAAK,SAAS;GAK9C,OAAO,MAAM,OAAO,KAAK,IAAI,CAAC,CAAC,OAAM,QAAO;IAC1C,IAAI;KAEF,IAAI,OAAO,SAAS,YAClB,KAAU;IAEd,UAAU;KACR,KAAKA,SAAS,MAAM,yCAAyC,EAAE,IAAI,CAAC;IACtE;GACF,CAAC;EACH;EACA,MAAM,KAAKH,QAAQ,UAAU,KAAKE,QAAQ,IAAI,EAAE,OAAO,KAAKD,OAAO,CAAC;EACpE,KAAKG,WAAW,KAAK;GAAE,OAAO,KAAKF;GAAQ;EAAG,CAAC;CACjD;CAEA,MAAM,OAAsB;EAC1B,KAAK,MAAM,EAAE,OAAO,QAAQ,KAAKE,YAC/B,MAAM,KAAKJ,QAAQ,YAAY,OAAO,EAAE;EAE1C,KAAKI,aAAa,CAAC;EACnB,MAAM,KAAKJ,QAAQ,MAAM;CAC3B;AACF"}