Skip to content

Commit 5f3650f

Browse files
author
Sean Sullivan
committed
Fix buildup of empty folders that eventually cause the worker to time out.
1 parent 09d857c commit 5f3650f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

elastic_datashader/cache.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from collections import OrderedDict
33
from datetime import datetime, timedelta, timezone
44
from os import scandir
5+
import os
6+
from contextlib import suppress
57
from pathlib import Path
68
from shutil import rmtree
79
from time import time
@@ -162,6 +164,15 @@ def age_off_cache(cache_path: Path, idx_name: str, max_age: timedelta) -> None:
162164
# set missing_ok=True in case another process deleted the same file
163165
file_path.unlink(missing_ok=True)
164166

167+
# clear all empty dirs and dirs that contain empty dirs to prevent build up of param hash directories
168+
remove_empty_dirs(cache_path/idx_name)
169+
170+
def remove_empty_dirs(path: Path):
171+
for root,dirs,_ in os.walk(path, topdown=False):
172+
for d in dirs:
173+
with suppress(OSError):
174+
os.rmdir(Path(root,d))
175+
165176
def get_idx_names(cache_path: Path) -> Iterable[str]:
166177
for path in cache_path.glob("*"):
167178
if path.is_dir():

tests/test_cache.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_clear_hash_cache(tmp_path):
7575

7676

7777
def test_age_off_cache(tmp_path):
78-
xdir = tmp_path / "fooindex/somehash/3/1"
78+
xdir = tmp_path / "fooindex/some_new_hash/3/1"
7979
xdir.mkdir(parents=True)
8080

8181
yfile = xdir / "2.png"
@@ -90,6 +90,10 @@ def test_age_off_cache(tmp_path):
9090

9191
assert not yfile.exists()
9292
assert yfile_after.exists()
93+
sleep(2)
94+
# clear again should remove all files and empty folders
95+
cache.age_off_cache(tmp_path, "fooindex", timedelta(seconds=1))
96+
assert not xdir.exists()
9397

9498

9599
def test_build_layer_info(tmp_path):

0 commit comments

Comments
 (0)