✨ 执行日志文件自动清理.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.orion.ops.module.asset.define.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 应用执行日志配置
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/4/25 13:36
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "app.exec-log")
|
||||
public class AppExecLogConfig {
|
||||
|
||||
/**
|
||||
* 是否拼接执行状态
|
||||
*/
|
||||
private Boolean appendStatus;
|
||||
|
||||
/**
|
||||
* 自动清理执行文件
|
||||
*/
|
||||
private Boolean autoClear;
|
||||
|
||||
/**
|
||||
* 保留周期 (天)
|
||||
*/
|
||||
private Integer keepPeriod;
|
||||
|
||||
public AppExecLogConfig() {
|
||||
this.appendStatus = true;
|
||||
this.autoClear = true;
|
||||
this.keepPeriod = 90;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.orion.ops.module.asset.task;
|
||||
|
||||
import com.orion.lang.utils.Strings;
|
||||
import com.orion.lang.utils.io.Files1;
|
||||
import com.orion.lang.utils.time.Dates;
|
||||
import com.orion.ops.framework.common.file.FileClient;
|
||||
import com.orion.ops.module.asset.dao.ExecHostLogDAO;
|
||||
import com.orion.ops.module.asset.define.config.AppExecLogConfig;
|
||||
import com.orion.ops.module.asset.entity.domain.ExecHostLogDO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 执行日志文件自动清理
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/4/24 23:50
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(value = "app.exec-log.auto-clear", havingValue = "true")
|
||||
public class ExecLogFileAutoClearTask {
|
||||
|
||||
/**
|
||||
* 分布式锁名称
|
||||
*/
|
||||
private static final String LOCK_KEY = "clear:elf:lock";
|
||||
|
||||
@Resource
|
||||
private AppExecLogConfig appExecLogConfig;
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Resource
|
||||
private FileClient logsFileClient;
|
||||
|
||||
@Resource
|
||||
private ExecHostLogDAO execHostLogDAO;
|
||||
|
||||
/**
|
||||
* 清理
|
||||
*/
|
||||
// @Scheduled(cron = "0 0 3 * * ?")
|
||||
@Scheduled(fixedRate = 20000)
|
||||
public void clear() {
|
||||
log.info("ExecLogFileAutoClearTask.clear start");
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(LOCK_KEY);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("ExecLogFileAutoClearTask.clear locked end");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 清理
|
||||
this.doClearFile();
|
||||
log.info("ExecLogFileAutoClearTask.clear finish");
|
||||
} catch (Exception e) {
|
||||
log.error("ExecLogFileAutoClearTask.clear error", e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行清理文件
|
||||
*/
|
||||
private void doClearFile() {
|
||||
// 删除的时间区间
|
||||
String maxPeriod = Dates.stream()
|
||||
.subDay(appExecLogConfig.getKeepPeriod())
|
||||
.format();
|
||||
// 获取需要删除的最大id
|
||||
ExecHostLogDO hostLog = execHostLogDAO.of()
|
||||
.createWrapper()
|
||||
.select(ExecHostLogDO::getLogId, ExecHostLogDO::getLogPath)
|
||||
.lt(ExecHostLogDO::getCreateTime, maxPeriod)
|
||||
.orderByDesc(ExecHostLogDO::getId)
|
||||
.then()
|
||||
.getOne();
|
||||
if (hostLog == null) {
|
||||
return;
|
||||
}
|
||||
// 获取执行日志根目录
|
||||
String hostLogPath = logsFileClient.getAbsolutePath(hostLog.getLogPath());
|
||||
String execLogPath = Files1.getParentPath(hostLogPath);
|
||||
String parentPath = Files1.getParentPath(execLogPath);
|
||||
// 获取需要删除的文件
|
||||
List<File> files = Files1.listFilesFilter(parentPath, s -> {
|
||||
if (!Strings.isInteger(s.getName())) {
|
||||
return false;
|
||||
}
|
||||
return Long.parseLong(s.getName()) <= hostLog.getLogId();
|
||||
}, false, true);
|
||||
if (files.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// 删除日志文件
|
||||
files.forEach(Files1::delete);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,11 @@
|
||||
"name": "app.sftp",
|
||||
"type": "com.orion.ops.module.asset.define.config.AppSftpConfig",
|
||||
"sourceType": "com.orion.ops.module.asset.define.config.AppSftpConfig"
|
||||
},
|
||||
{
|
||||
"name": "app.exec-log",
|
||||
"type": "com.orion.ops.module.asset.define.config.AppExecLogConfig",
|
||||
"sourceType": "com.orion.ops.module.asset.define.config.AppExecLogConfig"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
@@ -41,6 +46,24 @@
|
||||
"type": "java.lang.String",
|
||||
"description": "备份文件名称.",
|
||||
"defaultValue": "bk_${fileName}_${timestamp}"
|
||||
},
|
||||
{
|
||||
"name": "app.exec-log.append-status",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "是否拼接执行状态.",
|
||||
"defaultValue": "true"
|
||||
},
|
||||
{
|
||||
"name": "app.exec-log.auto-clear",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "自动清理执行文件.",
|
||||
"defaultValue": "true"
|
||||
},
|
||||
{
|
||||
"name": "app.exec-log.keep-period",
|
||||
"type": "java.lang.Integer",
|
||||
"description": "保留周期 (天)",
|
||||
"defaultValue": "90"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user