retention: Improve the placement of logging in run_archiving_in_chunks.

This commit is contained in:
Mateusz Mandera 2019-06-28 19:08:37 +02:00 committed by Tim Abbott
parent 17c5398703
commit 89ba6d7941
1 changed files with 10 additions and 8 deletions

View File

@ -90,19 +90,21 @@ def run_archiving_in_chunks(query: str, type: int, realm: Optional[Realm]=None,
while True: while True:
with transaction.atomic(): with transaction.atomic():
archive_transaction = ArchiveTransaction.objects.create(type=type, realm=realm) archive_transaction = ArchiveTransaction.objects.create(type=type, realm=realm)
logger.info("Archiving in {}".format(archive_transaction))
new_chunk = move_rows(Message, query, chunk_size=chunk_size, returning_id=True, new_chunk = move_rows(Message, query, chunk_size=chunk_size, returning_id=True,
archive_transaction_id=archive_transaction.id, **kwargs) archive_transaction_id=archive_transaction.id, **kwargs)
if new_chunk: if new_chunk:
logger.info(
"Processing {} messages in {}".format(len(new_chunk), archive_transaction)
)
move_related_objects_to_archive(new_chunk) move_related_objects_to_archive(new_chunk)
delete_messages(new_chunk) delete_messages(new_chunk)
message_count += len(new_chunk) message_count += len(new_chunk)
else: else:
archive_transaction.delete() # Nothing was archived archive_transaction.delete() # Nothing was archived
# This line needs to be outside of the atomic block, to capture the actual moment
# archiving of the chunk is finished (since Django does some significant additional work
# when leaving the block).
logger.info("Finished. Archived {} messages in this transaction.".format(len(new_chunk)))
# We run the loop, until the query returns fewer results than chunk_size, # We run the loop, until the query returns fewer results than chunk_size,
# which means we are done: # which means we are done:
if len(new_chunk) < chunk_size: if len(new_chunk) < chunk_size: