diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 12285d494b..faef1a6cdb 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -18,11 +18,32 @@ class InvenTreeConfig(AppConfig): def ready(self): if canAppAccessDatabase(): + + self.remove_obsolete_tasks() + self.start_background_tasks() if not isInTestMode(): self.update_exchange_rates() + def remove_obsolete_tasks(self): + """ + Delete any obsolete scheduled tasks in the database + """ + + obsolete = [ + 'InvenTree.tasks.delete_expired_sessions', + 'stock.tasks.delete_old_stock_items', + ] + + try: + from django_q.models import Schedule + except (AppRegistryNotReady): + return + + # Remove any existing obsolete tasks + Schedule.objects.filter(func__in=obsolete).delete() + def start_background_tasks(self): try: @@ -57,12 +78,6 @@ class InvenTreeConfig(AppConfig): schedule_type=Schedule.DAILY, ) - # Remove expired sessions - InvenTree.tasks.schedule_task( - 'InvenTree.tasks.delete_expired_sessions', - schedule_type=Schedule.DAILY, - ) - # Delete old error messages InvenTree.tasks.schedule_task( 'InvenTree.tasks.delete_old_error_logs', diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index adb9c0a370..18c3bcc564 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -231,25 +231,6 @@ def check_for_updates(): ) -def delete_expired_sessions(): - """ - Remove any expired user sessions from the database - """ - - try: - from django.contrib.sessions.models import Session - - # Delete any sessions that expired more than a day ago - expired = Session.objects.filter(expire_date__lt=timezone.now() - timedelta(days=1)) - - if expired.count() > 0: - logger.info(f"Deleting {expired.count()} expired sessions.") - expired.delete() - - except AppRegistryNotReady: - logger.info("Could not perform 'delete_expired_sessions' - App registry not ready") - - def update_exchange_rates(): """ Update currency exchange rates