@@ -734,7 +734,7 @@ def check_download_links(request, target, site_observations):
734
734
del original_search ['csrfmiddlewaretoken' ]
735
735
736
736
# 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.
738
738
# If the zip is there, then we can just return the file_url.
739
739
# If the record is there but the zip file isn't, then regenerate the zip file.
740
740
# from the search, to contain the latest information.
@@ -746,24 +746,32 @@ def check_download_links(request, target, site_observations):
746
746
protein_params = protein_params ,
747
747
other_params = other_params ,
748
748
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
+ )
759
758
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
+ )
762
771
# Repeat call, file is currently being created by another process.
763
- return existing_link [ 0 ]. file_url , False
772
+ return file_url , False
764
773
765
- # Recreate file.
766
- #
774
+ logger .info ('Download file is lost (file_url=%s). Recreating...' , file_url )
767
775
# This step can result in references to missing SD Files (see FE issue 907).
768
776
# If so the missing file will have a file reference of 'MISSING/<filename>'
769
777
# in the corresponding ['molecules']['sdf_files'] entry.
@@ -773,26 +781,28 @@ def check_download_links(request, target, site_observations):
773
781
_create_structures_zip (
774
782
target ,
775
783
zip_contents ,
776
- existing_link [ 0 ]. file_url ,
777
- existing_link [ 0 ] .original_search ,
784
+ file_url ,
785
+ existing_link .original_search ,
778
786
host ,
779
787
)
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
782
790
if static_link :
783
791
# 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 ()
788
795
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
790
798
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>.
793
801
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 )
796
806
797
807
zip_contents = _create_structures_dict (
798
808
target , site_observations , protein_params , other_params
@@ -818,12 +828,14 @@ def check_download_links(request, target, site_observations):
818
828
download_link .original_search = original_search
819
829
download_link .save ()
820
830
831
+ logger .info ('- Download record saved %s (file_url=%s)' , download_link , file_url )
821
832
return file_url , True
822
833
823
834
824
835
def recreate_static_file (existing_link , host ):
825
836
"""Recreate static file for existing link"""
826
837
target = existing_link .target
838
+ logger .info ('+ recreate_static_file(%s)' , target .title )
827
839
828
840
_create_structures_zip (
829
841
target ,
@@ -836,19 +848,38 @@ def recreate_static_file(existing_link, host):
836
848
existing_link .zip_file = True
837
849
existing_link .save ()
838
850
851
+ logger .info ('- Existing download link saved %s' , existing_link )
839
852
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.
845
853
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.
846
864
"""
847
-
848
- old_links = DownloadLinks .objects .filter (
865
+ out_of_date_links = DownloadLinks .objects .filter (
849
866
keep_zip_until__lt = datetime .datetime .now (datetime .timezone .utc )
850
867
).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 ()
0 commit comments