Skip to content

Commit 0336120

Browse files
Alan Christiealanbchristie
Alan Christie
authored andcommitted
fix: zip-file only cleared if file removed. Adds extra log
1 parent 56aa249 commit 0336120

File tree

2 files changed

+73
-41
lines changed

2 files changed

+73
-41
lines changed

viewer/download_structures.py

+71-40
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ def check_download_links(request, target, site_observations):
734734
del original_search['csrfmiddlewaretoken']
735735

736736
# For dynamic files, if the target, proteins, protein_params and other_params
737-
# are in an existing record then this is a repeat search .
737+
# are in an existing record then this is a repeat search.
738738
# If the zip is there, then we can just return the file_url.
739739
# If the record is there but the zip file isn't, then regenerate the zip file.
740740
# from the search, to contain the latest information.
@@ -746,24 +746,32 @@ def check_download_links(request, target, site_observations):
746746
protein_params=protein_params,
747747
other_params=other_params,
748748
static_link=False,
749-
)
750-
751-
if existing_link.exists():
752-
if (
753-
existing_link[0].zip_file
754-
and os.path.isfile(existing_link[0].file_url)
755-
and not static_link
756-
):
757-
logger.info('Existing link (%s)', existing_link[0].file_url)
758-
return existing_link[0].file_url, True
749+
).first()
750+
751+
if existing_link:
752+
linked_file_exists = os.path.isfile(existing_link.file_url)
753+
logger.info(
754+
'Found existing download link (%s) linked_file_exists=%s',
755+
existing_link,
756+
linked_file_exists,
757+
)
759758

760-
if os.path.isfile(existing_link[0].file_url) and not static_link:
761-
logger.info('Existing link in progress (%s)', existing_link[0].file_url)
759+
file_url = existing_link.file_url
760+
if existing_link.zip_file and linked_file_exists and not static_link:
761+
logger.info('- Returning existing download (file_url=%s)', file_url)
762+
return file_url, True
763+
764+
if linked_file_exists and not static_link:
765+
# The file exists but it is not marked as being 'valid' (i.e. zip_file is False).
766+
# If it was our process then it would have been marked as valid and
767+
# we would have returned it above.
768+
logger.warning(
769+
'- Download exists. From another progress? (file_url=%s)', file_url
770+
)
762771
# Repeat call, file is currently being created by another process.
763-
return existing_link[0].file_url, False
772+
return file_url, False
764773

765-
# Recreate file.
766-
#
774+
logger.info('Download file is lost (file_url=%s). Recreating...', file_url)
767775
# This step can result in references to missing SD Files (see FE issue 907).
768776
# If so the missing file will have a file reference of 'MISSING/<filename>'
769777
# in the corresponding ['molecules']['sdf_files'] entry.
@@ -773,26 +781,28 @@ def check_download_links(request, target, site_observations):
773781
_create_structures_zip(
774782
target,
775783
zip_contents,
776-
existing_link[0].file_url,
777-
existing_link[0].original_search,
784+
file_url,
785+
existing_link.original_search,
778786
host,
779787
)
780-
existing_link[0].keep_zip_until = get_keep_until()
781-
existing_link[0].zip_file = True
788+
existing_link.keep_zip_until = get_keep_until()
789+
existing_link.zip_file = True
782790
if static_link:
783791
# Changing from a dynamic to a static link
784-
existing_link[0].static_link = static_link
785-
existing_link[0].zip_contents = zip_contents
786-
existing_link[0].save()
787-
logger.info('Link saved (%s)', existing_link[0].file_url)
792+
existing_link.static_link = static_link
793+
existing_link.zip_contents = zip_contents
794+
existing_link.save()
788795

789-
return existing_link[0].file_url, True
796+
logger.info('- Recreated download and saved (file_url=%s)', file_url)
797+
return file_url, True
790798

791-
# Create a new link for this user
792-
# /code/media/downloads/<download_uuid>
799+
# No existing Download record - create a new link for this user,
800+
# which we'll locate in <MEDIA_ROOT>/downloads/<download_uuid>/<filename>.
793801
filename = target.title + '.zip'
794-
media_root = settings.MEDIA_ROOT
795-
file_url = os.path.join(media_root, 'downloads', str(uuid.uuid4()), filename)
802+
file_url = os.path.join(
803+
settings.MEDIA_ROOT, 'downloads', str(uuid.uuid4()), filename
804+
)
805+
logger.info('Creating new download (file_url=%s)...', file_url)
796806

797807
zip_contents = _create_structures_dict(
798808
target, site_observations, protein_params, other_params
@@ -818,12 +828,14 @@ def check_download_links(request, target, site_observations):
818828
download_link.original_search = original_search
819829
download_link.save()
820830

831+
logger.info('- Download record saved %s (file_url=%s)', download_link, file_url)
821832
return file_url, True
822833

823834

824835
def recreate_static_file(existing_link, host):
825836
"""Recreate static file for existing link"""
826837
target = existing_link.target
838+
logger.info('+ recreate_static_file(%s)', target.title)
827839

828840
_create_structures_zip(
829841
target,
@@ -836,19 +848,38 @@ def recreate_static_file(existing_link, host):
836848
existing_link.zip_file = True
837849
existing_link.save()
838850

851+
logger.info('- Existing download link saved %s', existing_link)
839852

840-
def maintain_download_links():
841-
"""Maintain zip files
842-
843-
Physical zip files are removed after 1 hour (or when the next POST call
844-
is made) for security reasons and to conserve memory space.
845853

854+
def maintain_download_links():
855+
"""Physical zip files are removed after 1 hour (or when the next POST call
856+
is made) for security reasons and to conserve memory space.Only if the file can
857+
be deleted do we reset the 'zip-file' flag. So, if there are any problems
858+
with the file-system the model should continue to reflect the current state
859+
of the world.
860+
861+
'zip_file' records whether the file should be present.
862+
For every record that has this set (and is too old), we remove the file directory
863+
and then clear the flag.
846864
"""
847-
848-
old_links = DownloadLinks.objects.filter(
865+
out_of_date_links = DownloadLinks.objects.filter(
849866
keep_zip_until__lt=datetime.datetime.now(datetime.timezone.utc)
850867
).filter(zip_file=True)
851-
for old_link in old_links:
852-
old_link.zip_file = False
853-
old_link.save()
854-
shutil.rmtree(os.path.dirname(old_link.file_url), ignore_errors=True)
868+
for out_of_date_link in out_of_date_links:
869+
if os.path.isfile(out_of_date_link.file_url):
870+
dir_name = os.path.dirname(out_of_date_link.file_url)
871+
logger.info('Removing file_url directory (%s)...', dir_name)
872+
shutil.rmtree(dir_name, ignore_errors=True)
873+
874+
# Does the file exist now?
875+
if os.path.isfile(out_of_date_link.file_url):
876+
logger.warning(
877+
'Failed removal of file_url directory (%s), not clearing zip_file flag',
878+
dir_name,
879+
)
880+
else:
881+
logger.info(
882+
'Removed file_url directory (%s), clearing zip_file flag', dir_name
883+
)
884+
out_of_date_link.zip_file = False
885+
out_of_date_link.save()

viewer/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,9 @@ def __str__(self):
11041104
return str(self.file_url)
11051105

11061106
def __repr__(self) -> str:
1107-
return "<DownloadLinks %r %r %r %r>" % (
1107+
return "<DownloadLinks %r %r %r %r %r>" % (
11081108
self.id,
1109+
self.zip_file,
11091110
self.file_url,
11101111
self.user,
11111112
self.target,

0 commit comments

Comments
 (0)