Skip to content

Commit 3082a88

Browse files
committed
Fix for #1818 db files locked on Windows so not rm
- Close db connections after startup metadata cache update - This seems to be the root cause of the latest bug from Friday when Kolibri would not delete files AFTER restarting (call to update_channel_metadata_cache locks all files in databases/ dir) - Disable ContentDBRoutingMiddleware for TasksViewSet worker views so they won't lock content DBs - The 'ATTACH DATABASE' statements seem to be fine when ContentDBRoutingMiddleware partially disabled - Close DB connections after ALL task runs - To fix can't-delete-after-import-from-Web bug - close_all DB connections before doing delete
1 parent 24089b6 commit 3082a88

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

kolibri/content/middleware.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ class ContentDBRoutingMiddleware(object):
99
"""
1010

1111
def process_view(self, request, view_func, view_args, view_kwargs):
12-
request.PREVIOUSLY_ACTIVE_CONTENT_DATABASE = get_active_content_database(return_none_if_not_set=True)
13-
if "channel_id" in view_kwargs:
14-
set_active_content_database(view_kwargs["channel_id"])
12+
if view_func.__name__ == 'TasksViewSet':
13+
pass # Fix #1818.1: skip get_active_content_database for Task workers to avoid locking DB
14+
else:
15+
request.PREVIOUSLY_ACTIVE_CONTENT_DATABASE = get_active_content_database(return_none_if_not_set=True)
16+
if "channel_id" in view_kwargs:
17+
set_active_content_database(view_kwargs["channel_id"])
1518

1619
def process_response(self, request, response):
1720
set_active_content_database(getattr(request, "PREVIOUSLY_ACTIVE_CONTENT_DATABASE", None))

kolibri/content/utils/annotation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ def update_channel_metadata_cache():
3232
if ch_metadata_obj.last_updated is None:
3333
ch_metadata_obj.last_updated = local_now()
3434
ch_metadata_obj.save()
35+
36+
# Fix #1818.1 content database files get locked (this is called on startup)
37+
from django.db import connections
38+
connections.close_all()

kolibri/tasks/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import requests
1515
from django.core.management import call_command
1616
from django.http import Http404
17+
from django.db import connections
1718
from django.utils.translation import ugettext as _
1819
from kolibri.content.models import ChannelMetadataCache
1920
from kolibri.content.utils.channels import get_mounted_drives_with_channel_info
@@ -153,7 +154,7 @@ def _networkimport(channel_id, update_progress=None):
153154
"network",
154155
channel_id,
155156
update_progress=update_progress)
156-
157+
connections.close_all() # close all DB connections (FIX for #1818)
157158

158159
def _localimport(drive_id, update_progress=None):
159160
drives = get_mounted_drives_with_channel_info()
@@ -172,6 +173,7 @@ def _localimport(drive_id, update_progress=None):
172173
channel["id"],
173174
drive.datafolder,
174175
update_progress=update_progress)
176+
connections.close_all() # close all DB connections (FIX for #1818)
175177

176178

177179
def _localexport(drive_id, update_progress=None):
@@ -188,6 +190,7 @@ def _localexport(drive_id, update_progress=None):
188190
channel.id,
189191
drive.datafolder,
190192
update_progress=update_progress)
193+
connections.close_all() # close all DB connections (FIX for #1818)
191194

192195

193196
def _job_to_response(job):

0 commit comments

Comments
 (0)