Skip to content

Commit

Permalink
Do actual deleting in aws-dynamodb-truncate
Browse files Browse the repository at this point in the history
Include an exponential back-off if items come back unprocessed.
  • Loading branch information
corpulentcoffee committed Mar 6, 2021
1 parent 738aa2e commit 0c21c67
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions home/bin/aws_dynamodb_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ def main() -> int:
if get_confirmation(table_name, item_count, first_batch) is not True:
return 1

print("TODO")
return 1
delete_items(client, table_name, first_batch)
for successive_batch in batches:
delete_items(client, table_name, successive_batch)

return 0


def get_parser():
Expand Down Expand Up @@ -129,6 +132,34 @@ def get_confirmation(
return input(f"{dedent(prompt).strip()} ").lower().startswith("y")


def delete_items(client, table_name: str, items: List[DynamoItem]):
print("deleting:", get_formatted_items(items))

request_items = {
table_name: [{"DeleteRequest": {"Key": item}} for item in items]
}
backoff_time = 1

while request_items:
response = client.batch_write_item(RequestItems=request_items)

if response.get("UnprocessedItems"):
from time import sleep

request_items = cast(dict, response["UnprocessedItems"])
print(
f"Retrying {len(request_items)}",
"item" if len(request_items) == 1 else "items",
f"after a {backoff_time}-second delay",
)

sleep(backoff_time)
backoff_time *= 2

else:
request_items = None


def get_formatted_items(items: List[DynamoItem]) -> str:
return ", ".join(get_formatted_item(item) for item in items)

Expand Down

0 comments on commit 0c21c67

Please sign in to comment.