@azure/event-hubs
Version:
Azure Event Hubs SDK for JS.
150 lines (149 loc) • 5.6 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pumpManager_exports = {};
__export(pumpManager_exports, {
PumpManagerImpl: () => PumpManagerImpl
});
module.exports = __toCommonJS(pumpManager_exports);
var import_logger = require("./logger.js");
var import_public = require("./models/public.js");
var import_partitionPump = require("./partitionPump.js");
class PumpManagerImpl {
_eventProcessorName;
_options;
_partitionIdToPumps = {};
constructor(eventProcessorName, eventProcessorOptions) {
this._eventProcessorName = eventProcessorName;
this._options = eventProcessorOptions;
}
/**
* Returns a list of partitionIds that are actively receiving messages.
*/
receivingFromPartitions() {
return Object.keys(this._partitionIdToPumps).filter((id) => {
const pump = this._partitionIdToPumps[id];
return Boolean(pump && pump.isReceiving);
});
}
/**
* Indicates whether the pump manager is actively receiving events from a given partition.
* @internal
*/
isReceivingFromPartition(partitionId) {
const pump = this._partitionIdToPumps[partitionId];
return Boolean(pump && pump.isReceiving);
}
/**
* Creates and starts a PartitionPump.
* @param startPosition - The position in the partition to start reading from.
* @param connectionContext - The ConnectionContext to forward to the PartitionPump.
* @param partitionProcessor - The PartitionProcessor to forward to the PartitionPump.
*/
async createPump(startPosition, connectionContext, partitionProcessor, abortSignal) {
const partitionId = partitionProcessor.partitionId;
if (abortSignal.aborted) {
import_logger.logger.verbose(
`${this._eventProcessorName}] The subscription was closed before creating the pump for partition ${partitionId}.`
);
return;
}
const existingPump = this._partitionIdToPumps[partitionId];
if (existingPump) {
if (existingPump.isReceiving) {
import_logger.logger.verbose(
`[${this._eventProcessorName}] [${partitionId}] The existing pump is running.`
);
return;
}
import_logger.logger.verbose(
`[${this._eventProcessorName}] [${partitionId}] The existing pump is not running.`
);
await this.removePump(partitionId, import_public.CloseReason.OwnershipLost);
}
import_logger.logger.verbose(`[${this._eventProcessorName}] [${partitionId}] Creating a new pump.`);
const pump = new import_partitionPump.PartitionPump(
connectionContext,
partitionProcessor,
startPosition,
this._options
);
try {
this._partitionIdToPumps[partitionId] = pump;
await pump.start();
} catch (err) {
import_logger.logger.verbose(
`[${this._eventProcessorName}] [${partitionId}] An error occured while adding/updating a pump: ${err}`
);
(0, import_logger.logErrorStackTrace)(err);
}
}
/**
* Stop a PartitionPump and removes it from the internal map.
* @param partitionId - The partitionId to remove the associated PartitionPump from.
* @param reason - The reason for removing the pump.
*/
async removePump(partitionId, reason) {
try {
const pump = this._partitionIdToPumps[partitionId];
if (pump) {
delete this._partitionIdToPumps[partitionId];
import_logger.logger.verbose(`[${this._eventProcessorName}] [${partitionId}] Stopping the pump.`);
await pump.stop(reason);
} else {
import_logger.logger.verbose(
`[${this._eventProcessorName}] [${partitionId}] No pump was found to remove.`
);
}
} catch (err) {
import_logger.logger.verbose(
`[${this._eventProcessorName}] [${partitionId}] An error occured while removing a pump: ${err}`
);
(0, import_logger.logErrorStackTrace)(err);
}
}
/**
* Stops all PartitionPumps and removes them from the internal map.
* @param reason - The reason for removing the pump.
*/
async removeAllPumps(reason) {
const partitionIds = Object.keys(this._partitionIdToPumps);
import_logger.logger.verbose(`[${this._eventProcessorName}] Removing all pumps due to reason ${reason}.`);
const tasks = [];
for (const partitionId of partitionIds) {
const pump = this._partitionIdToPumps[partitionId];
if (pump) {
tasks.push(pump.stop(reason));
}
}
try {
await Promise.all(tasks);
} catch (err) {
import_logger.logger.verbose(
`[${this._eventProcessorName}] An error occured while removing all pumps: ${err}`
);
(0, import_logger.logErrorStackTrace)(err);
} finally {
this._partitionIdToPumps = {};
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PumpManagerImpl
});
//# sourceMappingURL=pumpManager.js.map