/
partitionProcessor.ts
261 lines (240 loc) · 9.28 KB
/
partitionProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { CloseReason, PartitionManager } from "./eventProcessor";
import { ReceivedEventData } from "./eventData";
import { LastEnqueuedEventInfo } from "./eventHubReceiver";
/**
* A checkpoint is meant to represent the last successfully processed event by the user from a particular
* partition of a consumer group in an Event Hub instance.
*
* When the `updateCheckpoint()` method on the `PartitionProcessor` class is called by the user, a
* `Checkpoint` is created internally. It is then stored in the storage solution implemented by the
* `PartitionManager` chosen by the user when creating an `EventProcessor`.
*
* Users are never expected to interact with `Checkpoint` directly. This interface exists to support the
* internal workings of `EventProcessor` and `PartitionManager`.
**/
export interface Checkpoint {
/**
* @property The fully qualified Event Hubs namespace. This is likely to be similar to
* <yournamespace>.servicebus.windows.net
*/
fullyQualifiedNamespace: string;
/**
* @property The event hub name
*/
eventHubName: string;
/**
* @property The consumer group name
*/
consumerGroupName: string;
/**
* @property The unique identifier of the event processor
*/
ownerId: string;
/**
* @property The identifier of the Event Hub partition
*/
partitionId: string;
/**
* @property The sequence number of the event
*/
sequenceNumber: number;
/**
* @property The offset of the event.
*/
offset: number;
/**
* @property The unique identifier for the operation
*/
eTag: string;
}
/**
* The `PartitionProcessor` is responsible for processing events received from Event Hubs when using `EventProcessor`
*
* The EventProcessor creates a new instance of the PartitionProcessor for each partition of the event hub it starts processing. When you extend the `PartitionProcessor` in order to customize it as you see fit,
* - Override the `processEvents()` method to add the code to process the received events. This is also a good place to update the checkpoints using the `updateCheckpoint()` method
* - Optionally override the `processError()` method to handle any error that might have occurred when processing the events.
* - Optionally override the `initialize()` method to implement any set up related tasks you would want to carry out before starting to receive events from the partition
* - Optionally override the `close()` method to implement any tear down or clean up tasks you would want to carry out.
*/
export class PartitionProcessor {
private _partitionManager: PartitionManager | undefined;
private _consumerGroupName: string | undefined;
private _fullyQualifiedNamespace: string | undefined;
private _eventHubName: string | undefined;
private _eventProcessorId: string | undefined;
private _partitionId: string | undefined;
private _eTag: string = "";
private _lastEnqueuedEventInfo: LastEnqueuedEventInfo | undefined;
/**
* @property Information on the last enqueued event in the partition that is being processed.
* This property is updated by the `EventProcessor` if the `trackLastEnqueuedEventInfo` option is set to true
* when creating an instance of EventProcessor
* @readonly
*/
public get lastEnqueuedEventInfo(): LastEnqueuedEventInfo {
return this._lastEnqueuedEventInfo!;
}
/**
* @property Information on the last enqueued event in the partition that is being processed.
* This property is updated by the `EventProcessor` if the `trackLastEnqueuedEventInfo` option is set to true
* when creating an instance of EventProcessor
*/
public set lastEnqueuedEventInfo(lastEnqueuedEventInfo: LastEnqueuedEventInfo) {
this._lastEnqueuedEventInfo = lastEnqueuedEventInfo;
}
/**
* @property The fully qualified namespace from where the current partition is being processed. It is set by the `EventProcessor`
* @readonly
*/
public get fullyQualifiedNamespace() {
return this._fullyQualifiedNamespace!;
}
/**
* @property The fully qualified namespace from where the current partition is being processed. It is set by the `EventProcessor`
*/
public set fullyQualifiedNamespace(fullyQualifiedNamespace: string) {
if (!this._fullyQualifiedNamespace) {
this._fullyQualifiedNamespace = fullyQualifiedNamespace;
}
}
/**
* @property The name of the consumer group from where the current partition is being processed. It is set by the `EventProcessor`
* @readonly
*/
public get consumerGroupName() {
return this._consumerGroupName!;
}
/**
* @property The name of the consumer group from where the current partition is being processed. It is set by the `EventProcessor`
*/
public set consumerGroupName(consumerGroupName: string) {
if (!this._consumerGroupName) {
this._consumerGroupName = consumerGroupName;
}
}
/**
* @property The name of the event hub to which the current partition belongs. It is set by the `EventProcessor`
* @readonly
*/
public get eventHubName() {
return this._eventHubName!;
}
/**
* @property The name of the event hub to which the current partition belongs. It is set by the `EventProcessor`
*/
public set eventHubName(eventHubName: string) {
if (!this._eventHubName) {
this._eventHubName = eventHubName;
}
}
/**
* @property The identifier of the Event Hub partition that is being processed. It is set by the `EventProcessor`
* @readonly
*/
public get partitionId() {
return this._partitionId!;
}
/**
* @property The identifier of the Event Hub partition that is being processed. It is set by the `EventProcessor`
*/
public set partitionId(partitionId: string) {
if (!this._partitionId) {
this._partitionId = partitionId;
}
}
/**
* @property The unique identifier of the `EventProcessor` that has spawned the current instance of `PartitionProcessor`. This is set by the `EventProcessor`
*/
public get eventProcessorId() {
return this._eventProcessorId!;
}
/**
* @property The unique identifier of the `EventProcessor` that has spawned the current instance of `PartitionProcessor`. This is set by the `EventProcessor`
*/
public set eventProcessorId(eventProcessorId: string) {
if (!this._eventProcessorId) {
this._eventProcessorId = eventProcessorId;
}
}
/**
* @property The Partition Manager used for checkpointing events. This is set by the `EventProcessor`
*/
public set partitionManager(partitionManager: PartitionManager) {
if (!this._partitionManager) {
this._partitionManager = partitionManager;
}
}
/**
* This method is called when the `EventProcessor` takes ownership of a new partition and before any
* events are received.
*
* @return {Promise<void>}
*/
async initialize(): Promise<void> {}
/**
* This method is called before the partition processor is closed by the EventProcessor.
*
* @param reason The reason for closing this partition processor.
* @return {Promise<void>}
*/
async close(reason: CloseReason): Promise<void> {}
/**
* This method is called when new events are received.
*
* This is also a good place to update checkpoints as appropriate.
*
* @param events The received events to be processed.
* @return {Promise<void>}
*/
async processEvents(events: ReceivedEventData[]): Promise<void> {}
/**
* This method is called when an error occurs while receiving events from Event Hubs.
*
* @param error The error to be processed.
* @return {Promise<void>}
*/
async processError(error: Error): Promise<void> {}
/**
* Updates the checkpoint using the event data.
*
* A checkpoint is meant to represent the last successfully processed event by the user from a particular
* partition of a consumer group in an Event Hub instance.
*
* @param eventData The event that you want to update the checkpoint with.
* @return Promise<void>
*/
public async updateCheckpoint(eventData: ReceivedEventData): Promise<void>;
/**
* Updates the checkpoint using the given offset and sequence number.
*
* A checkpoint is meant to represent the last successfully processed event by the user from a particular
* partition of a consumer group in an Event Hub instance.
*
* @param sequenceNumber The sequence number of the event that you want to update the checkpoint with.
* @param offset The offset of the event that you want to update the checkpoint with.
* @return Promise<void>.
*/
public async updateCheckpoint(sequenceNumber: number, offset: number): Promise<void>;
public async updateCheckpoint(
eventDataOrSequenceNumber: ReceivedEventData | number,
offset?: number
): Promise<void> {
const checkpoint: Checkpoint = {
fullyQualifiedNamespace: this._fullyQualifiedNamespace!,
eventHubName: this._eventHubName!,
consumerGroupName: this._consumerGroupName!,
ownerId: this._eventProcessorId!,
partitionId: this._partitionId!,
sequenceNumber:
typeof eventDataOrSequenceNumber === "number"
? eventDataOrSequenceNumber
: eventDataOrSequenceNumber.sequenceNumber,
offset:
typeof offset === "number"
? offset
: (eventDataOrSequenceNumber as ReceivedEventData).offset,
eTag: this._eTag
};
this._eTag = await this._partitionManager!.updateCheckpoint(checkpoint);
}
}