Skip to content

Commit

Permalink
Added a retry loop for (de)registration on slow network shares.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Aug 6, 2024
1 parent a0776bf commit afd2115
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
27 changes: 19 additions & 8 deletions src/sewerrat/deregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from . import _utils as ut


def deregister(path: str, url: str, wait: float = 1):
def deregister(path: str, url: str, retry: int = 3, wait: float = 1):
"""
Deregister a directory from the SewerRat search index. It is assumed that
this directory is world-readable and that the caller has write access to
Expand All @@ -18,9 +18,14 @@ def deregister(path: str, url: str, wait: float = 1):
url:
URL to the SewerRat REST API.
retry:
Number of times to try to finish the registration. Larger values
may be necessary if ``path`` is in a network share that takes some
time to synchronise.
wait:
Number of seconds to wait for a file write to synchronise before
requesting verification.
requesting verification during each retry.
"""
path = os.path.abspath(path)
res = requests.post(url + "/deregister/start", json = { "path": path }, allow_redirects=True)
Expand All @@ -37,13 +42,19 @@ def deregister(path: str, url: str, wait: float = 1):
with open(target, "w") as handle:
pass

# Sleeping for a while so that files can sync on network shares.
time.sleep(wait)

try:
res = requests.post(url + "/deregister/finish", json = { "path": path }, allow_redirects=True)
if res.status_code >= 300:
raise ut.format_error(res)
for t in range(retry):
# Sleeping for a while so that files can sync on network shares.
time.sleep(wait)

res = requests.post(url + "/deregister/finish", json = { "path": path }, allow_redirects=True)
if res.status_code < 300:
break

# Only looping if the status code is an Unauth failure and we're not on the last loop iteration.
if res.status_code != 401 or t + 1 == retry:
raise ut.format_error(res)

finally:
os.unlink(target)

Expand Down
29 changes: 20 additions & 9 deletions src/sewerrat/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import _utils as ut


def register(path: str, names: List[str], url: str, wait: int = 1):
def register(path: str, names: List[str], url: str, retry: int = 3, wait: int = 1):
"""
Register a directory into the SewerRat search index. It is assumed that
that the directory is world-readable and that the caller has write access.
Expand All @@ -26,9 +26,14 @@ def register(path: str, names: List[str], url: str, wait: int = 1):
url:
URL to the SewerRat REST API.
retry:
Number of times to try to finish the registration. Larger values
may be necessary if ``path`` is in a network share that takes some
time to synchronise.
wait:
Number of seconds to wait for a file write to synchronise before
requesting verification.
requesting verification during each retry.
"""
if len(names) == 0:
raise ValueError("expected at least one entry in 'names'")
Expand All @@ -47,14 +52,20 @@ def register(path: str, names: List[str], url: str, wait: int = 1):
with open(target, "w") as handle:
handle.write("")

# Sleeping for a while so that files can sync on network shares.
time.sleep(wait)

try:
res = requests.post(url + "/register/finish", json = { "path": path, "base": names }, allow_redirects=True)
if res.status_code >= 300:
raise ut.format_error(res)
body = res.json()
for t in range(retry):
# Sleeping for a while so that files can sync on network shares.
time.sleep(wait)

res = requests.post(url + "/register/finish", json = { "path": path, "base": names }, allow_redirects=True)
if res.status_code < 300:
body = res.json()
break

# Only looping if the status code is an Unauth failure and we're not on the last loop iteration.
if res.status_code != 401 or t + 1 == retry:
raise ut.format_error(res)

finally:
os.unlink(target)

Expand Down

0 comments on commit afd2115

Please sign in to comment.