UNPKG

@hotmeshio/hotmesh

Version:

Permanent-Memory Workflows & AI Agents

81 lines (80 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LifecycleManager = exports.InstanceRegistry = void 0; const utils_1 = require("../../../modules/utils"); const config_1 = require("../config"); class InstanceRegistry { static add(router) { InstanceRegistry.instances.add(router); } static remove(router) { InstanceRegistry.instances.delete(router); } static async stopAll() { const stopPromises = []; for (const instance of [...InstanceRegistry.instances]) { stopPromises.push(instance.stopConsuming()); } await Promise.all(stopPromises); await (0, utils_1.sleepFor)(config_1.HMSH_BLOCK_TIME_MS * 2); } static getInstances() { return new Set(InstanceRegistry.instances); } } InstanceRegistry.instances = new Set(); exports.InstanceRegistry = InstanceRegistry; class LifecycleManager { constructor(readonly, topic, logger, stream) { this.shouldConsume = false; this.isUsingNotifications = false; this.readonly = readonly; this.topic = topic; this.logger = logger; this.stream = stream; } getShouldConsume() { return this.shouldConsume; } setShouldConsume(value) { this.shouldConsume = value; } getIsUsingNotifications() { return this.isUsingNotifications; } setIsUsingNotifications(value) { this.isUsingNotifications = value; } isReadonly() { return this.readonly; } isStopped(group, consumer, stream) { if (!this.shouldConsume) { this.logger.info(`router-stream-stopped`, { group, consumer, stream, }); } return !this.shouldConsume; } async startConsuming(router) { this.shouldConsume = true; InstanceRegistry.add(router); } async stopConsuming(router) { this.shouldConsume = false; this.logger.info(`router-stream-stopping`, this.topic ? { topic: this.topic } : undefined); InstanceRegistry.remove(router); // If using notifications, properly clean up if (this.isUsingNotifications && this.stream.stopNotificationConsumer) { try { await this.stream.cleanup?.(); } catch (error) { this.logger.error('router-stream-cleanup-error', { error }); } } } } exports.LifecycleManager = LifecycleManager;