-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (38 loc) · 1.1 KB
/
index.js
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
import fs from 'fs';
import readline from 'readline';
import './config/index.js';
import { flushCursor, getPreviousCursor } from './filesystem/cursor.js';
import { execute, shutdown } from './runner/index.js';
let cursorPosition;
async function main() {
const previousCursor = await getPreviousCursor();
const readInterface = readline.createInterface({
input: fs.createReadStream(process.env.INPUT_FILE),
});
let previousCursorFound = previousCursor === '';
let ids = [];
for await (const line of readInterface) {
if (!previousCursorFound) {
// seek till cursor
if (line === previousCursor) previousCursorFound = true;
else continue;
}
cursorPosition = line;
ids.push(line);
if (ids.length >= Number(process.env.ID_BATCH_SIZE)) {
await execute(ids);
await flushCursor(cursorPosition);
console.log(`sent ${ids.length} records`);
ids = [];
}
}
// process end of file batch
if (ids.length > 0) {
await execute(ids);
console.log(`sent ${ids.length} records`);
}
}
main().finally(() => {
flushCursor('');
shutdown();
});