-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
89 lines (71 loc) · 2.28 KB
/
handler.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
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
"use strict";
const elasticsearch = require("@elastic/elasticsearch");
const yaml = require("js-yaml");
const fs = require("fs");
const { syncDocuments, getCheckpoint } = require("./replicator");
module.exports.replicate = async (event) => {
console.log("Starting...");
let filename = "./config-dev.yaml";
if (process.env.STAGE === "prod") {
filename = "./config-prod.yaml";
}
let tasks = {};
try {
console.log("Reading configuration: " + filename);
const fileContents = fs.readFileSync(filename, "utf8");
const data = yaml.load(fileContents);
tasks = data.tasks;
} catch (e) {
console.log(e);
return;
}
// if only want to process one task, then just take that out of the configuration
if (process.env.TASK) {
console.log("Only processing one task: " + process.env.TASK);
const task = tasks.find((el) => el.name === process.env.TASK);
if (!task) {
console.log("Task not found in config file: " + process.env.TASK);
return;
}
tasks = [task];
} else {
console.log("Will run all tasks in configuration");
}
console.log("Found total " + tasks.length + " replication task(s) to run");
let totalRecords = 0;
let latestCommit = null;
// get the last checkpoint from the sourceClient
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i];
console.log("== Executing replication task: " + task.name);
const sourceClient = new elasticsearch.Client(
task.source.elasticClientOpts
);
const destinationClient = new elasticsearch.Client(
task.destination.elasticClientOpts
);
const checkpoint = await getCheckpoint(task, sourceClient);
latestCommit = checkpoint;
const result = await syncDocuments(
task,
sourceClient,
destinationClient,
checkpoint
);
totalRecords = result.totalRecords;
latestCommit = result.latestCommit;
console.log("Replication done");
console.log("Total records: " + totalRecords);
console.log("Latest checkpoint: " + latestCommit);
console.log("== Completed replication task: " + task.name);
}
console.log("Finished all replication tasks");
return {
message: "Finished all replication tasks",
event,
};
};
// if run directly
if (require.main === module) {
module.exports.replicate({});
}