From c8c35e2f042c9ad8ce7ef545469792b6f40f55fc Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 8 Dec 2021 09:55:41 +1100 Subject: [PATCH 1/2] Remove old task to delete expired sessions - Does not apply any more with new session management --- InvenTree/InvenTree/apps.py | 6 ---- InvenTree/InvenTree/tasks.py | 19 ---------- .../migrations/0013_auto_20211207_2250.py | 36 +++++++++++++++++++ 3 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 InvenTree/common/migrations/0013_auto_20211207_2250.py diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 12285d494b..b4563c7c65 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -57,12 +57,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 diff --git a/InvenTree/common/migrations/0013_auto_20211207_2250.py b/InvenTree/common/migrations/0013_auto_20211207_2250.py new file mode 100644 index 0000000000..2cb061f028 --- /dev/null +++ b/InvenTree/common/migrations/0013_auto_20211207_2250.py @@ -0,0 +1,36 @@ +# Generated by Django 3.2.5 on 2021-12-07 22:50 + +from django.db import migrations + + +def delete_task(apps, schema_editor): + """ + Remove scheduled task to delete old user sessions. + + Ref: https://github.com/inventree/InvenTree/issues/2429 + """ + + Task = apps.get_model('django_q', 'schedule') + + Task.objects.filter(func='InvenTree.tasks.delete_expired_sessions').delete() + + +def ksat_eteled(apps, schema_editor): + """ + Dummy function provided for reverse migrations + """ + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0012_notificationentry'), + ] + + operations = [ + migrations.RunPython( + delete_task, + reverse_code=ksat_eteled, + ) + ] From b19a7cc4fb89b61e164daadbfaf00205247b671e Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 8 Dec 2021 22:26:59 +1100 Subject: [PATCH 2/2] Run at app startup, not as a migration --- InvenTree/InvenTree/apps.py | 21 +++++++++++ .../migrations/0013_auto_20211207_2250.py | 36 ------------------- 2 files changed, 21 insertions(+), 36 deletions(-) delete mode 100644 InvenTree/common/migrations/0013_auto_20211207_2250.py diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index b4563c7c65..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: diff --git a/InvenTree/common/migrations/0013_auto_20211207_2250.py b/InvenTree/common/migrations/0013_auto_20211207_2250.py deleted file mode 100644 index 2cb061f028..0000000000 --- a/InvenTree/common/migrations/0013_auto_20211207_2250.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 3.2.5 on 2021-12-07 22:50 - -from django.db import migrations - - -def delete_task(apps, schema_editor): - """ - Remove scheduled task to delete old user sessions. - - Ref: https://github.com/inventree/InvenTree/issues/2429 - """ - - Task = apps.get_model('django_q', 'schedule') - - Task.objects.filter(func='InvenTree.tasks.delete_expired_sessions').delete() - - -def ksat_eteled(apps, schema_editor): - """ - Dummy function provided for reverse migrations - """ - pass - - -class Migration(migrations.Migration): - - dependencies = [ - ('common', '0012_notificationentry'), - ] - - operations = [ - migrations.RunPython( - delete_task, - reverse_code=ksat_eteled, - ) - ]