UNPKG

@seasketch/geoprocessing

Version:

Geoprocessing and reporting framework for SeaSketch 2.0

48 lines 2.6 kB
import { BatchWriteCommand, } from "@aws-sdk/lib-dynamodb"; import { wait } from "../../../src/helpers/wait.js"; export async function batchDelete(docClient, tableName, deleteCommandInput, retryCount = 0, maxRetries = 10) { try { // console.log("deleting", JSON.stringify(deleteCommandInput, null, 2)); const id = deleteCommandInput.RequestItems[tableName][0].DeleteRequest.Key.id; const service = deleteCommandInput.RequestItems[tableName][0].DeleteRequest.Key .service; console.log(`${retryCount > 0 ? "Retry #" + retryCount + " " : ""}Deleting batch starting with ${id} - ${service}`); const deleteCommand = new BatchWriteCommand(deleteCommandInput); const res = await docClient.send(deleteCommand); if (res.UnprocessedItems && Object.keys(res.UnprocessedItems).length > 0) { if (retryCount > maxRetries) { throw new Error(`${Object.keys(res.UnprocessedItems).length} items not deleted after ${maxRetries} retries`); } // const id = res.UnprocessedItems[tableName!][0].DeleteRequest!.Key!.id; // const service = // res.UnprocessedItems[tableName!][0].DeleteRequest!.Key!.service; // console.log( // ` ${Object.keys(res.UnprocessedItems[tableName]).length} unprocessed, retry batch in ${2 ** retryCount * 10}ms, starting with ${id} - ${service}` // ); await wait(2 ** retryCount * 10); // wait time increases exponentially await batchDelete(docClient, tableName, { RequestItems: res.UnprocessedItems }, // call again with unprocessed items retryCount + 1, maxRetries); } } catch (error) { // console.log(JSON.stringify(e, null, 2)); if (error.$metadata && error.$metadata.httpStatusCode === 400 && error.$metadata.totalRetryDelay) { // const id = // deleteCommandInput!.RequestItems![tableName!][0].DeleteRequest!.Key!.id; // const service = // deleteCommandInput!.RequestItems![tableName!][0].DeleteRequest!.Key! // .service; // console.log( // ` ThroughputError, retry in ${e.$metadata.totalRetryDelay}ms starting with ${id} - ${service}` // ); await wait(error.$metadata.totalRetryDelay); await batchDelete(docClient, tableName, deleteCommandInput, 0, maxRetries); } else { throw new Error(error); } } } //# sourceMappingURL=batchDelete.js.map