From c8c35e2f042c9ad8ce7ef545469792b6f40f55fc Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Wed, 8 Dec 2021 09:55:41 +1100
Subject: [PATCH] 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,
+        )
+    ]