Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: csv.addRow #72

Merged
merged 8 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions src/appmixer/utils/csv/AddRow/AddRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,44 @@ module.exports = {
parseNumbers,
parseBooleans
});
let lock;
let lockExtendInterval;
try {
lock = await context.lock(fileId, { ttl: 1000 * 60 * 1, maxRetryCount: 0 });
harshaio marked this conversation as resolved.
Show resolved Hide resolved
await processor.loadHeaders();

await processor.loadHeaders();
let rowAsArray;

let rowAsArray;

if (withHeaders) {
const headers = processor.getHeaders();
const parsed = expressionTransformer(rowWithColumns);
rowAsArray = headers.map(item => '');
parsed.forEach(item => {
const idx = processor.getHeaderIndex(item.column);
rowAsArray[idx] = item.value;
});
} else {
rowAsArray = row.split(delimiter);
}
if (withHeaders) {
const headers = processor.getHeaders();
const parsed = expressionTransformer(rowWithColumns);
rowAsArray = headers.map(item => '');
parsed.forEach(item => {
const idx = processor.getHeaderIndex(item.column);
rowAsArray[idx] = item.value;
});
} else {
rowAsArray = row.split(delimiter);
}

for (let i = 0; i < rowAsArray.length; i++) {
const item = rowAsArray[i];
if (item === undefined || item === null) {
rowAsArray[i] = '';
for (let i = 0; i < rowAsArray.length; i++) {
const item = rowAsArray[i];
if (item === undefined || item === null) {
rowAsArray[i] = '';
}
}
lockExtendInterval = setInterval(async () => {
if (lock) {
await lock.extend(parseInt(context.config.lockExtendTime, 10) || 1000 * 60 * 1);
}
}, 30000);
harshaio marked this conversation as resolved.
Show resolved Hide resolved
const savedFile = await processor.addRow(rowAsArray, (idx, currentRow, isEndOfFile) => {
return isEndOfFile;
});
return context.sendJson({ fileId: savedFile.fileId }, 'fileId');
} finally {
lock && await lock.unlock();
clearInterval(lockExtendInterval);
}

const savedFile = await processor.addRow(rowAsArray, (idx, currentRow, isEndOfFile) => {
return isEndOfFile;
});

return context.sendJson({ fileId: savedFile.fileId }, 'fileId');
}
};
8 changes: 1 addition & 7 deletions src/appmixer/utils/csv/CSVProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ module.exports = class CSVProcessor {
*/
async addRow(newRow, closure) {

const lock = await this.context.lock(this.fileId);
vtalas marked this conversation as resolved.
Show resolved Hide resolved
const stream = await this.loadFile();

let idx = 0;
Expand All @@ -385,7 +384,6 @@ module.exports = class CSVProcessor {
stream.destroy(err);
}
}).on('error', (err) => {
lock.unlock();
stream.destroy(err);
}).on('end', async () => {
if (closure(idx, null, true)) {
Expand All @@ -394,11 +392,7 @@ module.exports = class CSVProcessor {
writeStream.end();
});

try {
return await this.context.replaceFileStream(this.fileId, writeStream);
} finally {
lock.unlock();
}
return await this.context.replaceFileStream(this.fileId, writeStream);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/appmixer/utils/csv/bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appmixer.utils.csv",
"version": "1.2.4",
"version": "1.2.5",
"changelog": {
"1.0.0": [
"Initial version"
Expand All @@ -22,6 +22,9 @@
],
"1.2.4": [
"Fix issue with CSV module on Appmixer 5.0.0 and higher."
],
"1.2.5": [
"Added lock mechanism to avoid race condition in 'AddRow' component."
]
}
}
Loading