修改备份逻辑,方便测试。
This commit is contained in:
parent
b1f95e77cc
commit
b69132541a
|
|
@ -1,5 +1,6 @@
|
|||
package com.electromagnetic.industry.software.manage.tasks;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
|
|
@ -12,6 +13,7 @@ import cn.hutool.core.util.StrUtil;
|
|||
import cn.hutool.http.HttpUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.electromagnetic.industry.software.common.enums.EleDataStatusEnum;
|
||||
import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum;
|
||||
import com.electromagnetic.industry.software.common.enums.FileBackupSource;
|
||||
|
|
@ -32,15 +34,14 @@ import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
|||
import com.electromagnetic.industry.software.manage.service.serviceimpl.CommonService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
|
|
@ -61,8 +62,12 @@ public class BackupTask {
|
|||
private CommonService commonService;
|
||||
@Resource
|
||||
private BackupHandler backupHandler;
|
||||
@Value("${access.log.clear.hrs:2}")
|
||||
private int clearLogHrs;
|
||||
@Value("${app.user.operation.log.dir:\"\"}")
|
||||
private String userOperationLogDir;
|
||||
|
||||
@Scheduled(fixedDelayString = "#{${backup.interval.mins:720}}", timeUnit = TimeUnit.MINUTES)
|
||||
@Scheduled(fixedDelayString = "#{${backup.interval.mins:60}}", timeUnit = TimeUnit.MINUTES)
|
||||
public void backup() {
|
||||
if (!isRemotePortListening(elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort())) {
|
||||
log.info("备份环境连接失败,未备份文件");
|
||||
|
|
@ -283,8 +288,15 @@ public class BackupTask {
|
|||
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 2 * * ?")
|
||||
@Scheduled(cron = "0 1 * * * ?")
|
||||
// @Scheduled(cron = "0 */10 * * * ?")
|
||||
public void clearLog() {
|
||||
|
||||
int hour = DateUtil.hour(new Date(), true);
|
||||
if (hour != clearLogHrs) {
|
||||
return;
|
||||
}
|
||||
|
||||
DateTime dateTime = DateUtil.offsetMonth(DateUtil.date(), -6);
|
||||
LambdaQueryWrapper<UserAccessLog> le = Wrappers.lambdaQuery(UserAccessLog.class).select(UserAccessLog::getId)
|
||||
.orderByAsc(UserAccessLog::getCreatedTime)
|
||||
|
|
@ -294,6 +306,54 @@ public class BackupTask {
|
|||
if (CollUtil.isNotEmpty(ids)) {
|
||||
userAccessLogMapper.deleteByIds(ids);
|
||||
}
|
||||
exportUserAccessLog();
|
||||
}
|
||||
|
||||
private void exportUserAccessLog() {
|
||||
String accessLogExportFlag = "access_log_export_";
|
||||
if (StrUtil.isEmpty(userOperationLogDir)) {
|
||||
userOperationLogDir = elePropertyConfig.getEleTmpPath();
|
||||
}
|
||||
List<File> files = FileUtil.loopFiles(userOperationLogDir, file -> file.getName().startsWith(accessLogExportFlag) &&
|
||||
file.lastModified() < DateUtil.offsetDay(DateUtil.date(), -3).getTime());
|
||||
for (File file : files) {
|
||||
FileUtil.del(file.getAbsolutePath());
|
||||
}
|
||||
|
||||
StringJoiner sj = new StringJoiner(",");
|
||||
sj.add("id").add("action").add("operationModule").add("operationMsg").add("createdTime").add("remoteAddr").add("accessSuccess").add("accessDuration").add("requestIp");
|
||||
List<String> fields = List.of("id", "action", "operationModule", "operationMsg", "createdTime", "remoteAddr", "accessSuccess", "accessDuration", "requestIp");
|
||||
String filePath = userOperationLogDir + File.separator + accessLogExportFlag + EleCommonUtil.getNowTimeStr() + ".log";
|
||||
FileUtil.writeUtf8String(sj.toString(), filePath);
|
||||
|
||||
Long count = userAccessLogMapper.selectCount(Wrappers.lambdaQuery());
|
||||
int pageSize = 5000;
|
||||
long pageCount = (count / pageSize) + 1;
|
||||
for (int pageNum = 1; pageNum <= pageCount; pageNum++) {
|
||||
Page<UserAccessLog> userAccessLogPage = userAccessLogMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.lambdaQuery(UserAccessLog.class).select(
|
||||
UserAccessLog::getId, UserAccessLog::getAction, UserAccessLog::getOperationModule, UserAccessLog::getOperationMsg,
|
||||
UserAccessLog::getCreatedTime, UserAccessLog::getRemoteAddr, UserAccessLog::getAccessSuccess,
|
||||
UserAccessLog::getAccessDuration, UserAccessLog::getRequestIp
|
||||
).orderByAsc(UserAccessLog::getCreatedTime));
|
||||
List<UserAccessLog> userAccessLogs = userAccessLogPage.getRecords();
|
||||
List<String> tmpList = new ArrayList<>();
|
||||
for (UserAccessLog userAccessLog : userAccessLogs) {
|
||||
Map<String, Object> map = BeanUtil.beanToMap(userAccessLog);
|
||||
StringJoiner tmp = new StringJoiner(",");
|
||||
for (String field : fields) {
|
||||
Object value = map.get(field);
|
||||
if (value instanceof Date) {
|
||||
value = DateUtil.format((java.util.Date) value, "yyyy-MM-dd HH:mm:ss");
|
||||
} else if (value instanceof String) {
|
||||
value = ((String) value).replace("\n", "").replace("\r", "");
|
||||
}
|
||||
tmp.add(value + "");
|
||||
}
|
||||
tmpList.add(tmp.toString());
|
||||
}
|
||||
FileUtil.appendLines(tmpList, filePath, Charset.defaultCharset());
|
||||
}
|
||||
log.info("导出用户操作日志到 {}", filePath);
|
||||
}
|
||||
|
||||
// 每 2 分钟执行一次
|
||||
|
|
|
|||
Loading…
Reference in New Issue