Retry job folder rename on windows to workaround programs locking files. Sigh.

This commit is contained in:
Kovid Goyal 2025-06-21 08:25:21 +05:30
parent fb4654bd91
commit 69faf8c936
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -111,6 +111,17 @@ def clean_final(interval=24 * 60 * 60):
safe_remove(x) safe_remove(x)
def rename_with_retry(a, b, sleep_time=1):
try:
os.rename(a, b)
except PermissionError:
if iswindows:
time.sleep(sleep_time) # In case something has temporarily locked a file
os.rename(a, b)
else:
raise
def job_done(job): def job_done(job):
with cache_lock: with cache_lock:
bhash, pathtoebook, tdir = job.data bhash, pathtoebook, tdir = job.data
@ -124,7 +135,7 @@ def job_done(job):
clean_final() clean_final()
dest = os.path.join(books_cache_dir(), 'f', bhash) dest = os.path.join(books_cache_dir(), 'f', bhash)
safe_remove(dest, False) safe_remove(dest, False)
os.rename(tdir, dest) rename_with_retry(tdir, dest)
except Exception: except Exception:
import traceback import traceback
failed_jobs[bhash] = (False, traceback.format_exc()) failed_jobs[bhash] = (False, traceback.format_exc())