From b48e67d71452ac7f04f004ee0af74735eb4b1f8e Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Sat, 13 Feb 2021 19:51:04 +0100 Subject: [PATCH] revert a faulty change that caused memory usage to explode #537 --- src/documents/classifier.py | 39 +++++++++++--------------- src/documents/tests/test_classifier.py | 5 ++-- src/documents/tests/test_tasks.py | 1 - src/paperless/settings.py | 10 ------- 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/documents/classifier.py b/src/documents/classifier.py index c880e50d6..b577997e3 100755 --- a/src/documents/classifier.py +++ b/src/documents/classifier.py @@ -5,7 +5,6 @@ import pickle import re from django.conf import settings -from django.core.cache import cache from documents.models import Document, MatchingModel @@ -31,29 +30,23 @@ def load_classifier(): ) return None - version = os.stat(settings.MODEL_FILE).st_mtime + classifier = DocumentClassifier() + try: + classifier.load() - classifier = cache.get("paperless-classifier", version=version) - - if not classifier: - classifier = DocumentClassifier() - try: - classifier.load() - cache.set("paperless-classifier", classifier, - version=version, timeout=86400) - except (EOFError, IncompatibleClassifierVersionError) as e: - # there's something wrong with the model file. - logger.exception( - f"Unrecoverable error while loading document " - f"classification model, deleting model file." - ) - os.unlink(settings.MODEL_FILE) - classifier = None - except OSError as e: - logger.error( - f"Error while loading document classification model: {str(e)}" - ) - classifier = None + except (EOFError, IncompatibleClassifierVersionError) as e: + # there's something wrong with the model file. + logger.exception( + f"Unrecoverable error while loading document " + f"classification model, deleting model file." + ) + os.unlink(settings.MODEL_FILE) + classifier = None + except OSError as e: + logger.error( + f"Error while loading document classification model: {str(e)}" + ) + classifier = None return classifier diff --git a/src/documents/tests/test_classifier.py b/src/documents/tests/test_classifier.py index 1efe564d1..fcc08f842 100644 --- a/src/documents/tests/test_classifier.py +++ b/src/documents/tests/test_classifier.py @@ -3,6 +3,7 @@ import tempfile from pathlib import Path from unittest import mock +import pytest from django.conf import settings from django.test import TestCase, override_settings @@ -233,7 +234,6 @@ class TestClassifier(DirectoriesMixin, TestCase): self.assertFalse(os.path.exists(settings.MODEL_FILE)) self.assertIsNone(load_classifier()) - @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}) @mock.patch("documents.classifier.DocumentClassifier.load") def test_load_classifier(self, load): Path(settings.MODEL_FILE).touch() @@ -242,6 +242,7 @@ class TestClassifier(DirectoriesMixin, TestCase): @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}) @override_settings(MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle")) + @pytest.mark.skip(reason="Disabled caching due to high memory usage - need to investigate.") def test_load_classifier_cached(self): classifier = load_classifier() self.assertIsNotNone(classifier) @@ -250,7 +251,6 @@ class TestClassifier(DirectoriesMixin, TestCase): classifier2 = load_classifier() load.assert_not_called() - @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}) @mock.patch("documents.classifier.DocumentClassifier.load") def test_load_classifier_incompatible_version(self, load): Path(settings.MODEL_FILE).touch() @@ -260,7 +260,6 @@ class TestClassifier(DirectoriesMixin, TestCase): self.assertIsNone(load_classifier()) self.assertFalse(os.path.exists(settings.MODEL_FILE)) - @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}) @mock.patch("documents.classifier.DocumentClassifier.load") def test_load_classifier_os_error(self, load): Path(settings.MODEL_FILE).touch() diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index ed280441f..8c5a1b5df 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -52,7 +52,6 @@ class TestTasks(DirectoriesMixin, TestCase): load_classifier.assert_called_once() self.assertFalse(os.path.isfile(settings.MODEL_FILE)) - @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}) def test_train_classifier(self): c = Correspondent.objects.create(matching_algorithm=Tag.MATCH_AUTO, name="test") doc = Document.objects.create(correspondent=c, content="test", title="test") diff --git a/src/paperless/settings.py b/src/paperless/settings.py index 60e7b45bd..150709bb1 100644 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -169,16 +169,6 @@ CHANNEL_LAYERS = { }, } -CACHES = { - "default": { - "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": os.getenv("PAPERLESS_REDIS", "redis://localhost:6379"), - "OPTIONS": { - "CLIENT_CLASS": "django_redis.client.DefaultClient", - } - } -} - ############################################################################### # Security # ###############################################################################