From 80d8645a0c79ce1265ea3aec09594af8ad3e5d39 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 6 Oct 2021 21:20:40 +1100 Subject: [PATCH] Adds a new task to periodically remove old error messages --- InvenTree/InvenTree/apps.py | 6 ++++++ InvenTree/InvenTree/tasks.py | 37 +++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 6456c5994f..31a887d736 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -63,6 +63,12 @@ class InvenTreeConfig(AppConfig): schedule_type=Schedule.DAILY, ) + # Delete old error messages + InvenTree.tasks.schedule_task( + 'InvenTree.tasks.delete_old_error_logs', + schedule_type=Schedule.DAILY, + ) + # Delete "old" stock items InvenTree.tasks.schedule_task( 'stock.tasks.delete_old_stock_items', diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index 5fb6960601..3889f108af 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -156,7 +156,34 @@ def delete_successful_tasks(): started__lte=threshold ) - results.delete() + if results.count() > 0: + logger.info(f"Deleting {results.count()} successful task records") + results.delete() + + +def delete_old_error_logs(): + """ + Delete old error logs from the server + """ + + try: + from error_report.models import Error + + # Delete any error logs more than 30 days old + threshold = timezone.now() - timedelta(days=30) + + errors = Error.objects.filter( + when__lte=threshold, + ) + + if errors.count() > 0: + logger.info(f"Deleting {errors.count()} old error logs") + errors.delete() + + except AppRegistryNotReady: + # Apps not yet loaded + logger.info("Could not perform 'delete_old_error_logs' - App registry not ready") + return def check_for_updates(): @@ -215,7 +242,7 @@ def delete_expired_sessions(): # Delete any sessions that expired more than a day ago expired = Session.objects.filter(expire_date__lt=timezone.now() - timedelta(days=1)) - if True or expired.count() > 0: + if expired.count() > 0: logger.info(f"Deleting {expired.count()} expired sessions.") expired.delete() @@ -247,15 +274,15 @@ def update_exchange_rates(): pass except: # Some other error - print("Database not ready") + logger.warning("update_exchange_rates: Database not ready") return backend = InvenTreeExchange() - print(f"Updating exchange rates from {backend.url}") + logger.info(f"Updating exchange rates from {backend.url}") base = currency_code_default() - print(f"Using base currency '{base}'") + logger.info(f"Using base currency '{base}'") backend.update_rates(base_currency=base)