WIP partial sync requests

This commit is contained in:
zadam
2021-01-10 21:56:40 +01:00
parent cd653b9f0c
commit 7f8b19aee4
6 changed files with 111 additions and 47 deletions

View File

@@ -138,14 +138,54 @@ function getChanged(req) {
return ret;
}
const partialRequests = {};
function update(req) {
const {sourceId, entities} = req.body;
let {body} = req;
const pageCount = parseInt(req.get('pageCount'));
const pageIndex = parseInt(req.get('pageIndex'));
if (pageCount !== 1) {
const requestId = req.get('requestId');
if (pageIndex === 0) {
partialRequests[requestId] = {
createdAt: Date.now(),
payload: ''
};
}
if (!partialRequests[requestId]) {
throw new Error(`Partial request ${requestId}, index ${pageIndex} of ${pageCount} of pages does not have expected record.`);
}
partialRequests[requestId].payload += req.body;
if (pageIndex !== pageCount - 1) {
return;
}
else {
body = JSON.parse(partialRequests[requestId].payload);
delete partialRequests[requestId];
}
}
const {sourceId, entities} = body;
for (const {entityChange, entity} of entities) {
syncUpdateService.updateEntity(entityChange, entity, sourceId);
}
}
setInterval(() => {
for (const key in partialRequests) {
if (partialRequests[key].createdAt - Date.now() > 5 * 60 * 1000) {
delete partialRequests[key];
}
}
}, 60 * 1000);
function syncFinished() {
// after first sync finishes, the application is ready to be used
// this is meaningless but at the same time harmless (idempotent) for further syncs