/
ChangeFeedIteratorEpkRange.ts
132 lines (117 loc) · 4.24 KB
/
ChangeFeedIteratorEpkRange.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary Demonstrates using a ChangeFeed for an epk range
*/
import * as dotenv from "dotenv";
dotenv.config();
import { finish, handleError, logSampleHeader } from "../Shared/handleError";
import {
CosmosClient,
PartitionKeyDefinitionVersion,
Container,
StatusCodes,
FeedRange,
ChangeFeedIteratorOptions,
ChangeFeedStartFrom,
} from "@azure/cosmos";
const key = process.env.COSMOS_KEY || "<cosmos key>";
const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
const databaseId = process.env.COSMOS_DATABASE || "<cosmos database>";
const containerId = process.env.COSMOS_CONTAINER || "<cosmos container>";
logSampleHeader("Change Feed");
async function ingestData(container: Container, initialize: number, end: number) {
console.log("beginning data ingestion");
for (let i = initialize; i < end; i++) {
await container.items.create({ name: "sample1", key: i });
await container.items.create({ name: "sample2", key: i });
await container.items.create({ name: "sample3", key: i });
await container.items.create({ name: "sample4", key: i });
}
console.log("ingested items");
}
async function waitFor(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
// Establish a new instance of the CosmosClient to be used throughout this demo
const client = new CosmosClient({ endpoint, key });
async function iterateChangeFeedTillNow(
container: Container,
feedRange: FeedRange
): Promise<string> {
console.log("fetching changefeed until now");
const changeFeedIteratorOptions: ChangeFeedIteratorOptions = {
maxItemCount: 1,
changeFeedStartFrom: ChangeFeedStartFrom.Beginning(feedRange),
};
let continuationToken: string = "";
for await (const result of container.items
.getChangeFeedIterator(changeFeedIteratorOptions)
.getAsyncIterator()) {
// infinite loop to check for new results.
try {
if (result.statusCode === StatusCodes.NotModified) {
// If no new results are found, break the loop and return the continuation token
continuationToken = result.continuationToken;
console.log("No new results");
break;
} else {
console.log("Result found", result.result);
}
} catch (error) {
console.error("Error occurred", error);
}
}
return continuationToken;
}
async function run(): Promise<void> {
const { database } = await client.databases.createIfNotExists({ id: databaseId });
const containerDef = {
id: containerId,
partitionKey: {
paths: ["/name"],
version: PartitionKeyDefinitionVersion.V1,
},
throughput: 11000,
};
try {
const { container } = await database.containers.createIfNotExists(containerDef);
console.log("Container created");
await ingestData(container, 1, 11);
// query all the feed ranges inside the container
const feedRanges = await container.getFeedRanges();
// fetch the continuation token, so that we can start from the same point in time
const continuationToken = await iterateChangeFeedTillNow(container, feedRanges[0]);
// ingest some new data after fetching the continuation token
await ingestData(container, 11, 21);
let timeout = 0;
const changeFeedIteratorOptions: ChangeFeedIteratorOptions = {
maxItemCount: 1,
changeFeedStartFrom: ChangeFeedStartFrom.Continuation(continuationToken),
};
console.log("Starting fetching changes from continuation token");
for await (const result of container.items
.getChangeFeedIterator(changeFeedIteratorOptions)
.getAsyncIterator()) {
// infinite loop to check for new results.
try {
if (result.statusCode === StatusCodes.NotModified) {
// if no new changes are found, wait for 5 seconds and try again
console.log("No new results, waiting for 5 seconds");
timeout = 5000;
} else {
console.log("Result found", result.result);
timeout = 0;
}
} catch (error) {
console.error("Error occurred", error);
}
await waitFor(timeout);
}
} catch (err) {
console.error(err);
} finally {
await finish();
}
}
run().catch(handleError);