-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsendDataToSumoUsingSplitHandler.js
More file actions
67 lines (56 loc) · 2.47 KB
/
Copy pathsendDataToSumoUsingSplitHandler.js
File metadata and controls
67 lines (56 loc) · 2.47 KB
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
const { decodeDataChunks } = require('./decodeDataChunks');
var sumoHttp = require("./sumoclient");
function sendToSumoBlocking(chunk, sendOptions, context, isText) {
return new Promise(function (resolve, reject) {
function failureHandler(msgArray, ctx) {
reject();
}
function successHandler(ctx) {
resolve();
}
let sumoClient = new sumoHttp.SumoClient(sendOptions, context, failureHandler, successHandler);
if (!isText) {
// Default encoding is UTF-8
let data = chunk.toString('utf8');
sumoClient.addData(data);
} else {
sumoClient.addData(chunk);
}
sumoClient.flushAll();
});
}
/*
Creates a promise chain for each of the chunk received from decodeDataChunks
It increments
*/
async function sendDataToSumoUsingSplitHandler(context, dataBytesBuffer, sendOptions, serviceBusTask) {
var results = decodeDataChunks(context, dataBytesBuffer, serviceBusTask);
var ignoredprefixLen = results[0];
var dataChunks = results[1];
var numChunksSent = 0;
var dataLenSent = 0;
return new Promise(function (resolve, reject) {
let promiseChain = Promise.resolve();
const makeNextPromise = (chunk) => () => {
return sendToSumoBlocking(chunk, sendOptions, context, true).then(function () {
numChunksSent += 1;
dataLenSent += Buffer.byteLength(chunk);
});
};
for (var i = 0; i < dataChunks.length; i++) {
promiseChain = promiseChain.then(makeNextPromise(dataChunks[i]));
}
return promiseChain.catch(function (err) {
context.log.error(`Error in sendDataToSumoUsingSplitHandler blob: ${serviceBusTask.rowKey} prefix: ${ignoredprefixLen} Sent ${dataLenSent} bytes of data. numChunksSent ${numChunksSent}`);
resolve(dataLenSent + ignoredprefixLen);
}).then(function () {
if (dataChunks.length === 0) {
context.log(`No chunks to send ${serviceBusTask.rowKey}`);
} else if (numChunksSent === dataChunks.length) {
context.log(`All chunks successfully sent to sumo, Blob: ${serviceBusTask.rowKey}, Prefix: ${ignoredprefixLen}, Sent ${dataLenSent} bytes of data. numChunksSent ${numChunksSent}`);
}
resolve(dataLenSent + ignoredprefixLen);
});
});
}
exports.sendDataToSumoUsingSplitHandler = sendDataToSumoUsingSplitHandler;