Skip to content

Commit

Permalink
Merge branch 'fix_cache_partition_file_close' into 'series/3.4'
Browse files Browse the repository at this point in the history
Properly close storage file when CachePartion.create_file contextmanager ends

See merge request mayan-edms/mayan-edms!86
  • Loading branch information
siloraptor committed Aug 30, 2020
2 parents c269adf + d115e22 commit ca13919
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 13 additions & 2 deletions mayan/apps/file_caching/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def create_file(self, filename):
with transaction.atomic():
partition_file = self.files.create(filename=filename)
yield partition_file.open(mode='wb')
partition_file.update_size()

except Exception as exception:
logger.error(
'Unexpected exception while trying to save new '
Expand All @@ -169,6 +169,9 @@ def create_file(self, filename):
name=self.get_full_filename(filename=filename)
)
raise
finally:
partition_file.close()
partition_file.update_size()
finally:
lock.release()
except LockError:
Expand Down Expand Up @@ -208,6 +211,8 @@ class CachePartitionFile(models.Model):
default=0, verbose_name=_('File size')
)

_storage_object = None

class Meta:
get_latest_by = 'datetime'
unique_together = ('partition', 'filename')
Expand All @@ -231,15 +236,21 @@ def open(self, mode='rb'):
# Open the file for reading. If the file is written to, the
# .update_size() must be called.
try:
return self.partition.cache.storage.open(
self._storage_object = self.partition.cache.storage.open(
name=self.full_filename, mode=mode
)
return self._storage_object
except Exception as exception:
logger.error(
'Unexpected exception opening the cache file; %s', exception
)
raise

def close(self):
if self._storage_object is not None:
self._storage_object.close()
self._storage_object = None

def update_size(self):
self.file_size = self.partition.cache.storage.size(
name=self.full_filename
Expand Down
10 changes: 10 additions & 0 deletions mayan/apps/file_caching/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mock

from mayan.apps.common.tests.base import BaseTestCase

from .mixins import CacheTestMixin
Expand All @@ -14,3 +16,11 @@ def test_cache_purge(self):
self.test_cache.purge()

self.assertNotEqual(cache_total_size, self.test_cache.get_total_size())

@mock.patch('django.core.files.File.close')
def test_storage_file_close(self, mock_storage_file_close_method):
self._create_test_cache()
self._create_test_cache_partition()
self._create_test_cache_partition_file()

self.assertTrue(mock_storage_file_close_method.called)

0 comments on commit ca13919

Please sign in to comment.