UNPKG

@azure/event-hubs

Version:
187 lines (186 loc) 7.03 kB
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 partitionPump_exports = {}; __export(partitionPump_exports, { PartitionPump: () => PartitionPump, toProcessingSpanOptions: () => toProcessingSpanOptions }); module.exports = __toCommonJS(partitionPump_exports); var import_logger = require("./logger.js"); var import_public = require("./models/public.js"); var import_partitionReceiver = require("./partitionReceiver.js"); var import_tracing = require("./diagnostics/tracing.js"); var import_instrumentEventData = require("./diagnostics/instrumentEventData.js"); class PartitionPump { constructor(_context, partitionProcessor, _startPosition, options) { this._context = _context; this._startPosition = _startPosition; this._partitionProcessor = partitionProcessor; this._processorOptions = options; this._abortController = new AbortController(); } _context; _startPosition; _partitionProcessor; _processorOptions; _receiver; _isReceiving = false; _isStopped = false; _abortController; get isReceiving() { return this._isReceiving; } async start() { this._isReceiving = true; try { await this._partitionProcessor.initialize(); } catch (err) { this._partitionProcessor.processError(err); } this._receiveEvents(this._partitionProcessor.partitionId); import_logger.logger.info( `Successfully started the receiver for partition "${this._partitionProcessor.partitionId}".` ); } /** * Creates a new `PartitionReceiver` and replaces any existing receiver. * @param partitionId - The partition the receiver should read messages from. * @param lastSeenSequenceNumber - The sequence number to begin receiving messages from (exclusive). * If `-1`, then the PartitionPump's startPosition will be used instead. */ _setOrReplaceReceiver(partitionId, lastSeenSequenceNumber) { const currentEventPosition = lastSeenSequenceNumber >= 0 ? { sequenceNumber: lastSeenSequenceNumber, isInclusive: false } : this._startPosition; this._receiver = (0, import_partitionReceiver.createReceiver)( this._context, this._partitionProcessor.consumerGroup, this._partitionProcessor.eventProcessorId, partitionId, currentEventPosition, { ownerLevel: this._processorOptions.ownerLevel, trackLastEnqueuedEventProperties: this._processorOptions.trackLastEnqueuedEventProperties, retryOptions: this._processorOptions.retryOptions, skipParsingBodyAsJson: this._processorOptions.skipParsingBodyAsJson, prefetchCount: this._processorOptions.prefetchCount } ); return this._receiver; } async _receiveEvents(partitionId) { let lastSeenSequenceNumber = -1; let receiver = this._setOrReplaceReceiver(partitionId, lastSeenSequenceNumber); while (this._isReceiving) { try { if (receiver.isClosed) { receiver = this._setOrReplaceReceiver(partitionId, lastSeenSequenceNumber); } const receivedEvents = await receiver.receiveBatch( this._processorOptions.maxBatchSize, this._processorOptions.maxWaitTimeInSeconds, this._abortController.signal ); if (this._processorOptions.trackLastEnqueuedEventProperties && receiver.lastEnqueuedEventProperties) { this._partitionProcessor.lastEnqueuedEventProperties = receiver.lastEnqueuedEventProperties; } if (!this._isReceiving) { return; } if (receivedEvents.length) { lastSeenSequenceNumber = receivedEvents[receivedEvents.length - 1].sequenceNumber; } await import_tracing.tracingClient.withSpan( "PartitionPump.process", {}, () => this._partitionProcessor.processEvents(receivedEvents), toProcessingSpanOptions(receivedEvents, this._context.config) ); } catch (err) { if (!this._isReceiving) { return; } import_logger.logger.warning( `An error was thrown while receiving or processing events on partition "${this._partitionProcessor.partitionId}"` ); (0, import_logger.logErrorStackTrace)(err); try { await this._partitionProcessor.processError(err); } catch (errorFromUser) { import_logger.logger.verbose("An error was thrown by user's processError method: ", errorFromUser); } if (typeof err !== "object" || !err.retryable) { try { if (err.code === "ReceiverDisconnectedError") { return await this.stop(import_public.CloseReason.OwnershipLost); } return await this.stop(import_public.CloseReason.Shutdown); } catch (errorFromStop) { import_logger.logger.verbose( `An error occurred while closing the receiver with reason ${import_public.CloseReason.Shutdown}: `, errorFromStop ); } } } } } async stop(reason) { if (this._isStopped) { return; } this._isStopped = true; this._isReceiving = false; try { this._abortController.abort(); await this._receiver?.close(); await this._partitionProcessor.close(reason); } catch (err) { import_logger.logger.warning(`An error occurred while closing the receiver: ${err?.name}: ${err?.message}`); (0, import_logger.logErrorStackTrace)(err); this._partitionProcessor.processError(err); throw err; } } } function toProcessingSpanOptions(receivedEvents, eventHubProperties) { const spanLinks = []; for (const receivedEvent of receivedEvents) { const tracingContext = (0, import_instrumentEventData.extractSpanContextFromEventData)(receivedEvent); if (tracingContext) { spanLinks.push({ tracingContext, attributes: { enqueuedTime: receivedEvent.enqueuedTimeUtc.getTime() } }); } } return { spanLinks, spanKind: "consumer", ...(0, import_tracing.toSpanOptions)(eventHubProperties, "process") }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PartitionPump, toProcessingSpanOptions }); //# sourceMappingURL=partitionPump.js.map