Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Jan 9, 2021
1 parent 94c7a20 commit c61a371
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions WDL/runtime/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def put_download(
) -> str:
"""
Move the downloaded file to the cache location & return the new path; or if the uri isn't
cacheable, return the given path.
cacheable, memoize the association and return the given path.
"""
if directory:
uri = uri.rstrip("/")
Expand Down Expand Up @@ -243,23 +243,35 @@ def put_download(
if directory and os.path.isdir(p):
rmtree_atomic(p)
os.renames(filename, p)
self.flock(p)
# the renames() op should be atomic, because the download operation should have
# been run under the cache directory (download.py:run_cached)
logger.info(_("stored in download cache", uri=uri, cache_path=p))
ans = p
if not p:
with self._lock:
(self._workflow_directory_downloads if directory else self._workflow_downloads)[
uri
] = ans
self.flock(ans)
self.memo_download(uri, filename, directory=directory)
return ans

def download_cacheable(self, uri: str, directory: bool = False) -> Optional[str]:
if not self._cfg["download_cache"].get_bool("put"):
return None
return self.download_path(uri, directory=directory)

def memo_download(
self,
uri: str,
filename: str,
directory: bool = False,
) -> None:
"""
Memoize (for the lifetime of self) that filename is a local copy of uri; flock it as well.
"""
with self._lock:
memo = self._workflow_directory_downloads if directory else self._workflow_downloads
if uri not in memo:
memo[uri] = filename
self.flock(filename)

def flock(self, filename: str, exclusive: bool = False) -> None:
self._flocker.flock(filename, update_atime=True, exclusive=exclusive)

Expand Down

0 comments on commit c61a371

Please sign in to comment.