Skip to content

Commit

Permalink
fix: Ensure agent events are delivered with a 50k backlog
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltiunov committed Jan 12, 2021
1 parent 224914e commit bf0b9ec
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/cubejs-server-core/src/core/agentCollect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default async (event, endpointUrl, logger) => {
});
const flush = async (toFlush, retries) => {
if (!toFlush) {
toFlush = trackEvents.splice(0, 10);
toFlush = trackEvents.splice(0, 50);
}
if (!toFlush.length) {
return null;
return false;
}
if (retries == null) {
retries = 3;
Expand All @@ -31,19 +31,27 @@ export default async (event, endpointUrl, logger) => {
return flush(toFlush, retries - 1);
}
// console.log(await result.json());
return true;
} catch (e) {
if (retries > 0) {
return flush(toFlush, retries - 1);
}
logger('Agent Error', { error: (e.stack || e).toString() });
}
return null;
return true;
};
const currentPromise = (flushPromise || Promise.resolve()).then(() => flush()).then(() => {
if (currentPromise === flushPromise) {
flushPromise = null;
const flushCycle = async () => {
for (let i = 0; i < 1000; i++) {
if (!await flush()) {
return;
}
}
});
flushPromise = currentPromise;
};
if (!flushPromise) {
flushPromise = flushCycle().then(() => {
flushPromise = null;
});
}

return flushPromise;
};

0 comments on commit bf0b9ec

Please sign in to comment.