From cf67b537ab136f2688a1a7c0399daed61b8a6da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Redrejo?= Date: Tue, 5 Jul 2022 11:25:33 +0200 Subject: [PATCH] Applying feedback from PR review --- .../device/migrations/0003_contentcachekey.py | 13 +++++++- kolibri/utils/main.py | 30 +++++++++---------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/kolibri/core/device/migrations/0003_contentcachekey.py b/kolibri/core/device/migrations/0003_contentcachekey.py index 4ef603838a6..02cb61f1250 100644 --- a/kolibri/core/device/migrations/0003_contentcachekey.py +++ b/kolibri/core/device/migrations/0003_contentcachekey.py @@ -2,21 +2,32 @@ # Generated by Django 1.11.10 on 2018-09-05 17:20 from __future__ import unicode_literals +import sqlite3 import time from django.db import migrations from django.db import models +from django.db import transaction +from django.db.utils import OperationalError +from django.db.utils import ProgrammingError def create_content_cache_key(apps, schema_editor): from kolibri.core.device.models import ContentCacheKey - ContentCacheKey.update_cache_key() + try: + with transaction.atomic(): + ContentCacheKey.update_cache_key() + except (OperationalError, ProgrammingError, sqlite3.OperationalError): + # let's not run this on a database cache + # that might not have been created yet + pass class Migration(migrations.Migration): dependencies = [("device", "0002_devicesettings_default_facility")] + atomic = False operations = [ migrations.CreateModel( diff --git a/kolibri/utils/main.py b/kolibri/utils/main.py index ff9f0ba662c..3078b6bb41e 100644 --- a/kolibri/utils/main.py +++ b/kolibri/utils/main.py @@ -25,6 +25,7 @@ from kolibri.core.upgrade import matches_version from kolibri.core.upgrade import run_upgrades from kolibri.core.utils.cache import process_cache +from kolibri.deployment.default.cache import CACHES from kolibri.deployment.default.sqlite_db_names import ADDITIONAL_SQLITE_DATABASES from kolibri.plugins.utils import autoremove_unavailable_plugins from kolibri.plugins.utils import check_plugin_config_file_location @@ -224,22 +225,19 @@ def _upgrades_before_django_setup(updated, version): def _post_django_initialization(): - if OPTIONS["Cache"]["CACHE_BACKEND"] != "redis": - try: - process_cache.cull() - except SQLite3DatabaseError: - shutil.rmtree(process_cache.directory, ignore_errors=True) - os.mkdir(process_cache.directory) - process_cache._cache = FanoutCache( - process_cache.directory, - settings.CACHES["process_cache"]["SHARDS"], - settings.CACHES["process_cache"]["TIMEOUT"], - **settings.CACHES["process_cache"]["OPTIONS"] - ) - except AttributeError: - # DatabaseCache, no actions needed supposing - # kolibri manage createcachetable has been executed previously - pass + if "process_cache" in CACHES: # usually it means not using redis + if "DatabaseCache" not in CACHES["process_cache"]["BACKEND"]: + try: + process_cache.cull() + except SQLite3DatabaseError: + shutil.rmtree(process_cache.directory, ignore_errors=True) + os.mkdir(process_cache.directory) + process_cache._cache = FanoutCache( + process_cache.directory, + settings.CACHES["process_cache"]["SHARDS"], + settings.CACHES["process_cache"]["TIMEOUT"], + **settings.CACHES["process_cache"]["OPTIONS"] + ) def _upgrades_after_django_setup(updated, version):