You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
5 years ago
|
#!/usr/bin/env python3
|
||
|
import os
|
||
|
import shutil
|
||
|
import threading
|
||
3 years ago
|
from system.swaglog import cloudlog
|
||
2 years ago
|
from system.loggerd.config import ROOT, get_available_bytes, get_available_percent
|
||
|
from system.loggerd.uploader import listdir_by_creation
|
||
5 years ago
|
|
||
5 years ago
|
MIN_BYTES = 5 * 1024 * 1024 * 1024
|
||
|
MIN_PERCENT = 10
|
||
|
|
||
4 years ago
|
DELETE_LAST = ['boot', 'crash']
|
||
|
|
||
5 years ago
|
|
||
|
def deleter_thread(exit_event):
|
||
|
while not exit_event.is_set():
|
||
5 years ago
|
out_of_bytes = get_available_bytes(default=MIN_BYTES + 1) < MIN_BYTES
|
||
|
out_of_percent = get_available_percent(default=MIN_PERCENT + 1) < MIN_PERCENT
|
||
5 years ago
|
|
||
5 years ago
|
if out_of_percent or out_of_bytes:
|
||
5 years ago
|
# remove the earliest directory we can
|
||
4 years ago
|
dirs = sorted(listdir_by_creation(ROOT), key=lambda x: x in DELETE_LAST)
|
||
5 years ago
|
for delete_dir in dirs:
|
||
|
delete_path = os.path.join(ROOT, delete_dir)
|
||
|
|
||
|
if any(name.endswith(".lock") for name in os.listdir(delete_path)):
|
||
|
continue
|
||
|
|
||
|
try:
|
||
3 years ago
|
cloudlog.info(f"deleting {delete_path}")
|
||
3 years ago
|
if os.path.isfile(delete_path):
|
||
|
os.remove(delete_path)
|
||
|
else:
|
||
|
shutil.rmtree(delete_path)
|
||
5 years ago
|
break
|
||
|
except OSError:
|
||
3 years ago
|
cloudlog.exception(f"issue deleting {delete_path}")
|
||
5 years ago
|
exit_event.wait(.1)
|
||
|
else:
|
||
|
exit_event.wait(30)
|
||
|
|
||
|
|
||
5 years ago
|
def main():
|
||
5 years ago
|
deleter_thread(threading.Event())
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|