This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/tools/deleteoldlogentries.py

46 lines
1.2 KiB
Python
Raw Permalink Normal View History

import logging
from datetime import timedelta, datetime
from app import app
2015-10-06 22:42:41 +00:00
from data.database import LogEntry
logger = logging.getLogger(__name__)
LOG_FORMAT = "%(asctime)s [%(process)d] [%(levelname)s] [%(name)s] %(message)s"
BATCH_SIZE = 1000
def delete_old_logentries(delete_before):
delete_up_to_id = (LogEntry
.select(LogEntry.id)
.where(LogEntry.datetime <= delete_before)
.order_by(LogEntry.id.desc())
.limit(1)
.tuples())[0][0]
logger.debug('Deleting up to id: %s', delete_up_to_id)
start_from_id = (LogEntry
.select(LogEntry.id)
.order_by(LogEntry.id)
.limit(1)
.tuples())[0][0]
logger.debug('Starting from id: %s', start_from_id)
deleted = 1
current_batch_end = min(start_from_id + BATCH_SIZE, delete_up_to_id)
while deleted > 0 or current_batch_end < delete_up_to_id:
deleted = (LogEntry
.delete()
.where(LogEntry.id <= current_batch_end)
.execute())
logger.debug('Deleted %s entries', deleted)
current_batch_end = min(current_batch_end + BATCH_SIZE, delete_up_to_id)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
now = datetime.now()
one_month_ago = now - timedelta(days=30)
2015-10-06 22:42:41 +00:00
delete_old_logentries(one_month_ago)