1. 清理对象:sourcedir,taskdir 2. 清理逻辑: 1. 删除超过时间限制的所有文件 2. 空闲空间低于底线则按时间清理文件,直到空闲空间达到规定 3. 删除非本地项目的项目中的代码文件
()
| 26 | """资源清理类""" |
| 27 | @staticmethod |
| 28 | def del_old_file(): |
| 29 | """ |
| 30 | 1. 清理对象:sourcedir,taskdir |
| 31 | 2. 清理逻辑: |
| 32 | 1. 删除超过时间限制的所有文件 |
| 33 | 2. 空闲空间低于底线则按时间清理文件,直到空闲空间达到规定 |
| 34 | 3. 删除非本地项目的项目中的代码文件 |
| 35 | """ |
| 36 | # logger.info('start del_old_file.') |
| 37 | # 将所有资源文件放入数组 |
| 38 | source_dir_path = settings.SOURCE_DIR |
| 39 | task_dir_path = settings.TASK_DIR |
| 40 | |
| 41 | def get_file_path_dict(dir_path): |
| 42 | if not os.path.exists(dir_path): |
| 43 | return {} |
| 44 | dir_path_dict = {} |
| 45 | for file_path in os.listdir(dir_path): |
| 46 | file_path = os.path.join(dir_path, file_path) |
| 47 | # 2020-09-10 bugfix 执行项目编译命令时,可能会在sourcedirs或taskdirs下创建软链接,直接删除,避免后续处理异常 |
| 48 | if os.path.islink(file_path): |
| 49 | logger.info("{} is a link, delete.".format(file_path)) |
| 50 | PathMgr().safe_rmpath(file_path) |
| 51 | continue |
| 52 | file_create_time = os.path.getctime(file_path) |
| 53 | dir_path_dict[file_create_time] = file_path |
| 54 | return dir_path_dict |
| 55 | |
| 56 | dir_dict = {} |
| 57 | dir_dict.update(get_file_path_dict(source_dir_path)) |
| 58 | dir_dict.update(get_file_path_dict(task_dir_path)) |
| 59 | |
| 60 | if not dir_dict: |
| 61 | # logger.info('done del_old_file.') |
| 62 | return |
| 63 | # logger.info('old_file_list check done.') |
| 64 | # (针对所有资源文件)删除超过时间限制的所有文件 |
| 65 | now = time.time() |
| 66 | expired_time = now - settings.SOURCE_RETAIN_TIME.total_seconds() |
| 67 | file_create_time_list = sorted(dir_dict) |
| 68 | index = 0 |
| 69 | while index < len(file_create_time_list): |
| 70 | if file_create_time_list[index] < expired_time: |
| 71 | file_path = dir_dict.pop(file_create_time_list[index]) |
| 72 | PathMgr().safe_rmpath(file_path) |
| 73 | else: |
| 74 | break |
| 75 | index += 1 |
| 76 | # logger.info('old file clean done.') |
| 77 | # (针对所有资源文件)空闲空间低于底线则按时间清理文件,直到空闲空间达到规定 |
| 78 | file_create_time_list = sorted(dir_dict) # 获取修改后 |
| 79 | if not file_create_time_list: |
| 80 | # logger.info('done del_old_file.') |
| 81 | return |
| 82 | index = 0 |
| 83 | # 如果不存在data目录,先创建,后续才能判断可用磁盘空间 |
| 84 | if not os.path.exists(settings.DATA_DIR): |
| 85 | os.makedirs(settings.DATA_DIR) |