-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathstream-processor.ts
111 lines (92 loc) · 2.5 KB
/
stream-processor.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
import type { Document } from '@mongosh/service-provider-core';
import type Mongo from './mongo';
import { asPrintable } from './enums';
import {
ShellApiWithMongoClass,
returnsPromise,
shellApiClassDefault,
} from './decorators';
import type { Streams } from './streams';
@shellApiClassDefault
export default class StreamProcessor extends ShellApiWithMongoClass {
constructor(public _streams: Streams, public name: string) {
super();
}
get _mongo(): Mongo {
return this._streams._mongo;
}
[asPrintable]() {
return `Atlas Stream Processor: ${this.name}`;
}
@returnsPromise
async start(options: Document = {}) {
return await this._streams._runStreamCommand({
startStreamProcessor: this.name,
...options,
});
}
@returnsPromise
async stop() {
return await this._streams._runStreamCommand({
stopStreamProcessor: this.name,
});
}
@returnsPromise
async drop() {
return this._drop();
}
async _drop() {
return await this._streams._runStreamCommand({
dropStreamProcessor: this.name,
});
}
@returnsPromise
async stats(options: Document = {}) {
return this._streams._runStreamCommand({
getStreamProcessorStats: this.name,
...options,
});
}
@returnsPromise
async sample(options: Document = {}) {
const r = await this._streams._runStreamCommand({
startSampleStreamProcessor: this.name,
...options,
});
if (r.ok !== 1) {
return r;
}
return this._sampleFrom(r.cursorId as number);
}
async _sampleFrom(cursorId: number) {
let currentCursorId = cursorId;
// keep pulling until end of stream
while (String(currentCursorId) !== '0') {
const res = await this._streams._runStreamCommand({
getMoreSampleStreamProcessor: this.name,
cursorId: currentCursorId,
});
if (res.ok !== 1) {
return res;
}
currentCursorId = res.cursorId;
// print fetched documents
for (const doc of res.messages) {
await this._instanceState.shellApi.printjson(doc);
}
// wait before pulling again if no result in this batch
if (!res.messages.length) {
const interruptable = this._instanceState.interrupted.asPromise();
try {
await Promise.race([
this._instanceState.shellApi.sleep(1000), // wait 1 second
interruptable.promise, // unless interruppted
]);
} finally {
interruptable.destroy();
}
}
}
return;
}
}