Try twice to delete cache entries on windows

Thanks to the idiotic mandatory file locking, we can never be sure that
some stupid windows service like an antivirus/filesync program has not locked
the cache files.
This commit is contained in:
Kovid Goyal 2018-08-08 07:29:06 +05:30
parent 59b9357c0c
commit 521181280b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -14,7 +14,7 @@ from cPickle import dumps
from hashlib import sha1 from hashlib import sha1
from calibre import walk from calibre import walk
from calibre.constants import cache_dir from calibre.constants import cache_dir, iswindows
from calibre.srv.render_book import RENDER_VERSION from calibre.srv.render_book import RENDER_VERSION
from calibre.utils.ipc.simple_worker import fork_job from calibre.utils.ipc.simple_worker import fork_job
from calibre.utils.lock import ExclusiveFile from calibre.utils.lock import ExclusiveFile
@ -46,16 +46,22 @@ def safe_makedirs(path):
return path return path
def robust_rmtree(x):
retries = 2 if iswindows else 1 # retry on windows to get around the idiotic mandatory file locking
for i in range(retries):
try:
return shutil.rmtree(x)
except EnvironmentError:
time.sleep(0.1)
def clear_temp(temp_path): def clear_temp(temp_path):
now = time.time() now = time.time()
for x in os.listdir(temp_path): for x in os.listdir(temp_path):
x = os.path.join(temp_path, x) x = os.path.join(temp_path, x)
mtime = os.path.getmtime(x) mtime = os.path.getmtime(x)
if now - mtime > DAY: if now - mtime > DAY:
try: robust_rmtree(x)
shutil.rmtree(x)
except EnvironmentError:
pass
def expire_cache(path, instances, max_age): def expire_cache(path, instances, max_age):
@ -64,10 +70,7 @@ def expire_cache(path, instances, max_age):
for instance in remove: for instance in remove:
if instance['status'] == 'finished': if instance['status'] == 'finished':
instances.remove(instance) instances.remove(instance)
try: robust_rmtree(os.path.join(path, instance['path']))
shutil.rmtree(os.path.join(path, instance['path']))
except Exception:
pass
def expire_cache_and_temp(temp_path, finished_path, metadata, max_age): def expire_cache_and_temp(temp_path, finished_path, metadata, max_age):