multitenant: rename whoosh directory before cleaning index (#37291)

This commit is contained in:
Frédéric Péters 2019-11-02 07:54:51 +01:00
parent a14ced4d18
commit ef552c210e
1 changed files with 18 additions and 0 deletions

View File

@ -17,6 +17,8 @@
from __future__ import absolute_import
import os
import shutil
import time
from django.conf import settings
from django.db import connection
@ -35,6 +37,22 @@ class WhooshSearchBackend(haystack.backends.whoosh_backend.WhooshSearchBackend):
def use_file_storage(self, value):
pass
def delete_index(self):
renamed_path = None
if os.path.exists(self.path):
# rename existing path instead of removing, so it works on NFS
# when there are opened files.
renamed_path = self.path + '.deleted-%s' % time.time()
os.rename(self.path, renamed_path)
super(WhooshSearchBackend, self).delete_index()
if renamed_path:
# remove afterwards and ignore errors (residual directories will
# have to be cleaned manually)
try:
shutil.rmtree(renamed_path)
except (IOError, OSError):
pass
@property
def path(self):
tenant = connection.get_tenant()