Skip to content

Instantly share code, notes, and snippets.

@sushant-at-nitor
Created August 25, 2022 10:24
Show Gist options
  • Save sushant-at-nitor/4f9f7c36155813b2fbef0baef9a4d463 to your computer and use it in GitHub Desktop.
Save sushant-at-nitor/4f9f7c36155813b2fbef0baef9a4d463 to your computer and use it in GitHub Desktop.
failed-queue.js
/**
* 1. Create a state management / centralized store
* 2. Create a generic service which will be used by all the reducers / slices of the state management
* 3. On service call failures, add the payload and action to the reprocess queue
* 4. Create an EvenStream which would listen to the failure events receiving from the base service
* 5. Send the event as telemetry to the server - either retry sending to the same endpoint / send to common log collector service
* 6. After notifying remove the even from the stream
*/
const apiEvent = {
endpoint,
payload,
retry,
};
const initQueue = (telemetryEndpoint = "http://tel", pendingEvents = []) => {
let events = pendingEvents || []; // hydrate from local storage
const enableAutoProcess = true;
/**
* 1. Auto process the event - cron
* 2. Atomic processing
*/
setInterval(async () => {
if (!enableAutoProcess) return false;
const processedEvents = [];
for (const iEvent in events) {
const event = events[iEvent];
const result = await processEvent(event);
processedEvents.push(iEvent);
}
// deduce events excluding processed events
return true;
}, 5 * 60 * 1000); // process events each 5 mins
const processEvent = async (event) => {
const endpoint = event.retry ? endpoint : telemetryEndpoint;
const response = await fetch(endpoint, {
body: event.payload,
});
return response.ok;
};
return (event) => {
enableAutoProcess = false;
processEvent(event)
.then((result) => {
// handle ok
console.debug("pushed to backend");
})
.catch((error) => {
events.push(event);
})
.finally(() => {
enableAutoProcess = true;
});
};
};
const queue = initQueue();
queue({
// endpoint: "http:///sdf/co",
payload: {},
retry: false,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment