diff --git a/src/sewerrat/deregister.py b/src/sewerrat/deregister.py index 267b2af..bc881d5 100644 --- a/src/sewerrat/deregister.py +++ b/src/sewerrat/deregister.py @@ -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 @@ -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) @@ -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) diff --git a/src/sewerrat/register.py b/src/sewerrat/register.py index 4f8daae..36fe312 100644 --- a/src/sewerrat/register.py +++ b/src/sewerrat/register.py @@ -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. @@ -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'") @@ -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)