@mastra/core
Version:
1 lines • 5.58 kB
Source Map (JSON)
{"version":3,"sources":["../src/worker/worker.ts","../src/worker/transport/pull-transport.ts"],"names":[],"mappings":";;;AA2BO,IAAe,eAAf,MAA4B;AAAA,EAGvB,MAAA;AAAA,EACA,IAAA;AAAA;AAAA,EAGV,iBAAiB,MAAA,EAAsB;AACrC,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AAAA;AAAA,EAGA,MAAM,KAAK,IAAA,EAAiC;AAC1C,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAI,CAAC,IAAA,CAAK,MAAA,IAAU,IAAA,CAAK,MAAA,EAAQ;AAC/B,MAAA,IAAA,CAAK,SAAS,IAAA,CAAK,MAAA;AAAA,IACrB;AAAA,EACF;AAKF;;;AC5CA,IAAM,eAAA,GAAkB,WAAA;AAEjB,IAAM,gBAAN,MAA+C;AAAA,EACpD,OAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,aAA0D,EAAC;AAAA,EAE3D,WAAA,CAAY;AAAA,IACV,MAAA;AAAA,IACA,KAAA;AAAA,IACA,KAAA;AAAA,IACA;AAAA,GACF,EAMG;AACD,IAAA,IAAA,CAAK,OAAA,GAAU,MAAA;AACf,IAAA,IAAA,CAAK,MAAA,GAAS,KAAA;AACd,IAAA,IAAA,CAAK,SAAS,KAAA,IAAS,eAAA;AACvB,IAAA,IAAA,CAAK,OAAA,GAAU,MAAA;AAAA,EACjB;AAAA,EAEA,MAAM,MAAM,MAAA,EAAoC;AAC9C,IAAA,IAAI,IAAA,CAAK,UAAA,CAAW,MAAA,GAAS,CAAA,EAAG;AAC9B,MAAA,IAAA,CAAK,OAAA,EAAS,MAAM,kFAAkF,CAAA;AACtG,MAAA;AAAA,IACF;AACA,IAAA,MAAM,EAAA,GAAoB,CAAC,KAAA,EAAO,GAAA,EAAK,IAAA,KAAS;AAK9C,MAAA,MAAA,CAAO,MAAM,KAAA,EAAO,GAAA,EAAK,IAAI,CAAA,CAAE,MAAM,CAAA,GAAA,KAAO;AAC1C,QAAA,IAAI;AAEF,UAAA,IAAI,OAAO,SAAS,UAAA,EAAY;AAC9B,YAAA,KAAK,IAAA,EAAK;AAAA,UACZ;AAAA,QACF,CAAA,SAAE;AACA,UAAA,IAAA,CAAK,OAAA,EAAS,KAAA,CAAM,uCAAA,EAAyC,EAAE,KAAK,CAAA;AAAA,QACtE;AAAA,MACF,CAAC,CAAA;AAAA,IACH,CAAA;AACA,IAAA,MAAM,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,IAAA,CAAK,MAAA,EAAQ,IAAI,EAAE,KAAA,EAAO,IAAA,CAAK,MAAA,EAAQ,CAAA;AACpE,IAAA,IAAA,CAAK,WAAW,IAAA,CAAK,EAAE,OAAO,IAAA,CAAK,MAAA,EAAQ,IAAI,CAAA;AAAA,EACjD;AAAA,EAEA,MAAM,IAAA,GAAsB;AAC1B,IAAA,KAAA,MAAW,EAAE,KAAA,EAAO,EAAA,EAAG,IAAK,KAAK,UAAA,EAAY;AAC3C,MAAA,MAAM,IAAA,CAAK,OAAA,CAAQ,WAAA,CAAY,KAAA,EAAO,EAAE,CAAA;AAAA,IAC1C;AACA,IAAA,IAAA,CAAK,aAAa,EAAC;AACnB,IAAA,MAAM,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,EAC3B;AACF","file":"chunk-CWKMGK5K.cjs","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"]}