Skip to content

Commit 82dc7de

Browse files
committed
Adding waiting for timer mechanism
1 parent c805dcb commit 82dc7de

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/actions/write.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ let rowCount = 0;
1919
let ax;
2020
let putUrl;
2121

22+
let readyFlag = false;
23+
2224
exports.init = async function init(cfg) {
2325
const delimiter = cfg.writer.separator || ',';
2426
const header = cfg.includeHeaders !== 'No';
@@ -45,17 +47,25 @@ exports.init = async function init(cfg) {
4547
console.log('CSV file to be uploaded file to uri=%s', putUrl);
4648
ax = axios.create();
4749
util.addRetryCountInterceptorToAxios(ax);
50+
readyFlag = true;
4851
};
4952

5053
exports.process = async function ProcessAction(msg, cfg) {
5154
// eslint-disable-next-line consistent-this
5255
const self = this;
5356

57+
while (!readyFlag) {
58+
// eslint-disable-next-line no-loop-func,no-await-in-loop
59+
await new Promise(resolve => timeout(resolve, 100));
60+
}
61+
5462
if (timeout) {
5563
clearTimeout(timeout);
5664
}
5765

5866
timeout = setTimeout(async () => {
67+
readyFlag = false;
68+
5969
console.log('Closing the stream due to inactivity');
6070

6171
const finalRowCount = rowCount;
@@ -68,6 +78,7 @@ exports.process = async function ProcessAction(msg, cfg) {
6878
maxContentLength: REQUEST_MAX_CONTENT_LENGTH,
6979
});
7080
stringifier.end();
81+
7182
const messageToEmit = messages.newMessageWithBody({
7283
rowCount: finalRowCount,
7384
});
@@ -80,7 +91,6 @@ exports.process = async function ProcessAction(msg, cfg) {
8091
rowCount = 0;
8192
console.log('Emitting message %j', messageToEmit);
8293
await self.emit('data', messageToEmit);
83-
await exports.init(cfg);
8494
}, TIMEOUT_BETWEEN_EVENTS);
8595

8696
let row = msg.body.writer;

0 commit comments

Comments
 (0)