in lib/recipes-data/src/lib/dynamo.ts [201:244]
async function bulkRemovePage(
page: WriteRequest[],
others: WriteRequest[],
retryCount: number,
): Promise<void> {
if (page.length == 0) {
if (others.length == 0) {
//we are done. Shouldn't get here, but meh.
return;
} else {
//ok for some weird reason we got an empty array to process but more to do.
return bulkRemovePage(others, [], 0);
}
}
const RequestItems: Record<string, WriteRequest[]> = {};
RequestItems[TableName as string] = page;
const req = new BatchWriteItemCommand({
RequestItems,
});
const result = await client.send(req);
const unprocessedItems = result.UnprocessedItems?.[TableName as string];
if (unprocessedItems && unprocessedItems.length > 0) {
if (retryCount > MaximumRetries) {
console.error(
`ERROR Could not remove items after maximum number of attempts, giving up.`,
);
throw new Error('Unable to remove all items');
}
console.log(
`WARNING Could not remove all items on attempt ${retryCount}. Pausing before trying again...`,
);
await awaitableDelay();
return bulkRemovePage(unprocessedItems, others, retryCount + 1);
} else if (others.length > 0) {
const nextPage = others.slice(0, 25);
const nextOthers = others.length > 25 ? others.slice(25) : [];
return bulkRemovePage(nextPage, nextOthers, 0);
} else {
return;
}
}