mirror of https://github.com/jumpserver/jumpserver
perf: optimize file removal
parent
47d0d8b7a0
commit
22d4bd5292
|
@ -96,17 +96,20 @@ def batch_delete(queryset, batch_size=3000):
|
||||||
def remove_files_by_days(root_path, days, file_types=None):
|
def remove_files_by_days(root_path, days, file_types=None):
|
||||||
if file_types is None:
|
if file_types is None:
|
||||||
file_types = ['.json', '.tar', '.gz', '.mp4']
|
file_types = ['.json', '.tar', '.gz', '.mp4']
|
||||||
need_rm_files = []
|
|
||||||
expire_date = timezone.now() - timezone.timedelta(days=days)
|
expire_date = timezone.now() - timezone.timedelta(days=days)
|
||||||
timestamp = expire_date.timestamp()
|
timestamp = expire_date.timestamp()
|
||||||
for root, dirs, files in os.walk(root_path):
|
for root, dirs, files in os.walk(root_path):
|
||||||
|
rm_files = []
|
||||||
for file in files:
|
for file in files:
|
||||||
if any(file.endswith(file_type) for file_type in file_types):
|
if any(file.endswith(file_type) for file_type in file_types):
|
||||||
file_path = os.path.join(root, file)
|
file_path = os.path.join(root, file)
|
||||||
if os.path.getmtime(file_path) <= timestamp:
|
if os.path.getmtime(file_path) <= timestamp:
|
||||||
need_rm_files.append(file_path)
|
rm_files.append(file_path)
|
||||||
for file in need_rm_files:
|
for file in rm_files:
|
||||||
os.remove(file)
|
try:
|
||||||
|
os.remove(file)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Remove file {file} error: {e}")
|
||||||
|
|
||||||
|
|
||||||
def clean_expired_session_period():
|
def clean_expired_session_period():
|
||||||
|
|
Loading…
Reference in New Issue