Skip to content

Commit a9c50f1

Browse files
committed
Support non-blocking (de)registration in the latest SewerRat API.
1 parent aab244f commit a9c50f1

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

src/sewerrat/deregister.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from . import _utils as ut
66

77

8-
def deregister(path: str, url: str, retry: int = 3, wait: float = 1):
8+
def deregister(path: str, url: str, retry: int = 3, wait: float = 1, block: bool = True):
99
"""
10-
Deregister a directory from the SewerRat search index. It is assumed that
11-
this directory is world-readable and that the caller has write access to
12-
it; or, the directory does not exist.
10+
Deregister a directory from the SewerRat search index.
1311
1412
Args:
1513
path:
16-
Path to the directory to be registered.
14+
Path to the directory to be deregistered.
15+
The directory should either be readable by the SewerRat API and the caller should have write access;
16+
or the directory should not exist.
1717
1818
url:
1919
URL to the SewerRat REST API.
@@ -23,9 +23,18 @@ def deregister(path: str, url: str, retry: int = 3, wait: float = 1):
2323
2424
wait:
2525
Deprecated, ignored.
26+
27+
block:
28+
Whether to block on successful deregistration.
29+
30+
Returns:
31+
On success, the directory is deregistered.
32+
33+
If ``block = False``, the function returns before confirmation of successful deregistration from the SewerRat API.
34+
This can be useful for asynchronous processing of directories with many files.
2635
"""
2736
path = ut.clean_path(path)
28-
res = requests.post(url + "/deregister/start", json = { "path": path }, allow_redirects=True)
37+
res = requests.post(url + "/deregister/start", json = { "path": path, "block": block }, allow_redirects=True)
2938
if res.status_code >= 300:
3039
raise ut.format_error(res)
3140

@@ -40,7 +49,7 @@ def deregister(path: str, url: str, retry: int = 3, wait: float = 1):
4049
pass
4150

4251
try:
43-
res = requests.post(url + "/deregister/finish", json = { "path": path }, allow_redirects=True)
52+
res = requests.post(url + "/deregister/finish", json = { "path": path, "block": block }, allow_redirects=True)
4453
if res.status_code >= 300:
4554
raise ut.format_error(res)
4655
finally:

src/sewerrat/register.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@
77
from . import _utils as ut
88

99

10-
def register(path: str, names: Union[str, List[str]], url: str, retry: int = 3, wait: int = 1):
10+
def register(path: str, names: Union[str, List[str]], url: str, retry: int = 3, wait: int = 1, block: bool = True):
1111
"""
12-
Register a directory into the SewerRat search index. It is assumed that
13-
that the directory is world-readable and that the caller has write access.
14-
If a metadata file cannot be indexed (e.g., due to incorrect formatting,
15-
insufficient permissions), a warning will be printed but the function will
16-
not throw an error.
12+
Register a directory into the SewerRat search index.
1713
1814
Args:
1915
path:
2016
Path to the directory to be registered.
17+
The directory should be readable by the SewerRat API and the caller should have write access.
2118
2219
names:
23-
List of strings containing the base names of metadata files inside
24-
``path`` to be indexed. Alternatively, a single string containing
25-
the base name for a single metadata file.
20+
List of strings containing the base names of metadata files inside ``path`` to be indexed.
21+
Alternatively, a single string containing the base name for a single metadata file.
2622
2723
url:
2824
URL to the SewerRat REST API.
@@ -32,6 +28,16 @@ def register(path: str, names: Union[str, List[str]], url: str, retry: int = 3,
3228
3329
wait:
3430
Deprecated, ignored.
31+
32+
block:
33+
Whether to block on successful registration.
34+
35+
Returns:
36+
On success, the directory is registered.
37+
If a metadata file cannot be indexed (e.g., due to incorrect formatting, insufficient permissions), a warning will be printed but the function will not throw an error.
38+
39+
If ``block = False``, the function returns before confirmation of successful registration from the SewerRat API.
40+
This can be useful for asynchronous processing of directories with many files.
3541
"""
3642
if isinstance(names, str):
3743
names = [names]
@@ -50,13 +56,14 @@ def register(path: str, names: Union[str, List[str]], url: str, retry: int = 3,
5056
handle.write("")
5157

5258
try:
53-
res = requests.post(url + "/register/finish", json = { "path": path, "base": names }, allow_redirects=True)
59+
res = requests.post(url + "/register/finish", json = { "path": path, "base": names, "block": block }, allow_redirects=True)
5460
if res.status_code >= 300:
5561
raise ut.format_error(res)
5662
body = res.json()
5763
finally:
5864
os.unlink(target)
5965

60-
for comment in body["comments"]:
61-
warnings.warn(comment)
66+
if block:
67+
for comment in body["comments"]:
68+
warnings.warn(comment)
6269
return

src/sewerrat/start_sewerrat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
test_api_port = None
1010

1111

12-
def start_sewerrat(db: Optional[str] = None, port: Optional[int] = None, wait: float = 1, version: str = "1.1.0", overwrite: bool = False) -> Tuple[bool, int]:
12+
def start_sewerrat(db: Optional[str] = None, port: Optional[int] = None, wait: float = 1, version: str = "1.1.1", overwrite: bool = False) -> Tuple[bool, int]:
1313
"""
1414
Start a test SewerRat service.
1515

tests/test_register.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,36 @@ def test_basic():
3333

3434
finally:
3535
sewerrat.deregister(mydir, url=url)
36+
37+
38+
def test_unblocked():
39+
mydir = tempfile.mkdtemp()
40+
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
41+
handle.write('{ "first": "Aaron", "last": "Lun" }')
42+
43+
os.mkdir(os.path.join(mydir, "diet"))
44+
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
45+
handle.write('{ "meal": "lunch", "ingredients": "water" }')
46+
47+
_, url = sewerrat.start_sewerrat()
48+
res = sewerrat.query(url, "aaron")
49+
assert len(res) == 0
50+
51+
try:
52+
sewerrat.register(mydir, ["metadata.json"], url=url, block=False)
53+
for i in range(10):
54+
time.sleep(0.1)
55+
res = sewerrat.query(url, "aaron")
56+
if len(res) == 1:
57+
break
58+
assert len(res) == 1
59+
60+
sewerrat.deregister(mydir, url=url, block=False)
61+
for i in range(10):
62+
time.sleep(0.1)
63+
res = sewerrat.query(url, "aaron")
64+
if len(res) == 0:
65+
break
66+
assert len(res) == 0
67+
finally:
68+
sewerrat.deregister(mydir, url=url)

0 commit comments

Comments
 (0)