@mastra/core
Version:
82 lines (81 loc) • 2.27 kB
JavaScript
//#region src/worker/worker.ts
/**
* Abstract base class for Mastra workers.
*
* Each worker is a self-contained, independently deployable unit of
* background processing. Concrete implementations include:
* - OrchestrationWorker: processes workflow events
* - SchedulerWorker: fires cron-based workflow schedules
* - BackgroundTaskWorker: manages background tool execution
*
* Workers are registered on a Mastra instance and run inline by default.
* They can also be launched standalone via the CLI for separate deployment.
*/
var MastraWorker = class {
mastra;
deps;
/** Called by Mastra during registration to provide the instance reference. */
__registerMastra(mastra) {
this.mastra = mastra;
}
/** Initialize with infrastructure deps. Called before start(). */
async init(deps) {
this.deps = deps;
if (!this.mastra && deps.mastra) this.mastra = deps.mastra;
}
};
//#endregion
//#region src/worker/transport/pull-transport.ts
const TOPIC_WORKFLOWS = "workflows";
var PullTransport = class {
#pubsub;
#group;
#topic;
#logger;
#callbacks = [];
constructor({ pubsub, group, topic, logger }) {
this.#pubsub = pubsub;
this.#group = group;
this.#topic = topic ?? TOPIC_WORKFLOWS;
this.#logger = logger;
}
async start(router) {
if (this.#callbacks.length > 0) {
this.#logger?.debug("[PullTransport] start() called while already subscribed; ignoring duplicate call");
return;
}
const cb = (event, ack, nack) => {
router.route(event, ack, nack).catch((err) => {
try {
if (typeof nack === "function") nack();
} finally {
this.#logger?.error("[PullTransport] router.route rejected", { err });
}
});
};
await this.#pubsub.subscribe(this.#topic, cb, { group: this.#group });
this.#callbacks.push({
topic: this.#topic,
cb
});
}
async stop() {
for (const { topic, cb } of this.#callbacks) await this.#pubsub.unsubscribe(topic, cb);
this.#callbacks = [];
await this.#pubsub.flush();
}
};
//#endregion
Object.defineProperty(exports, "MastraWorker", {
enumerable: true,
get: function() {
return MastraWorker;
}
});
Object.defineProperty(exports, "PullTransport", {
enumerable: true,
get: function() {
return PullTransport;
}
});
//# sourceMappingURL=pull-transport-BlEVfOcc.cjs.map