zhangqiyang
7 months ago
1 changed files with 98 additions and 0 deletions
@ -0,0 +1,98 @@ |
|||||||
|
package com.system.log.corn; |
||||||
|
|
||||||
|
import Log.LogConfig; |
||||||
|
import Log.LogEntity; |
||||||
|
import Log.repo.LogConfigRepository; |
||||||
|
import Log.repo.LogEntityRepository; |
||||||
|
import com.google.inject.Inject; |
||||||
|
import com.system.log.service.LogService; |
||||||
|
import groovy.util.logging.Slf4j; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import module.LogStatus; |
||||||
|
import org.quartz.Job; |
||||||
|
import org.quartz.JobExecutionContext; |
||||||
|
import org.quartz.JobExecutionException; |
||||||
|
import org.quartz.SchedulerException; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author zhangqiyang |
||||||
|
* @description LogManage |
||||||
|
* @date 2024/4/10 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class LogBackupJob implements Job { |
||||||
|
|
||||||
|
@Inject LogConfigRepository logConfigRepository; |
||||||
|
|
||||||
|
@Inject LogEntityRepository logEntityRepository; |
||||||
|
|
||||||
|
@Inject LogService logService; |
||||||
|
|
||||||
|
private boolean isRunning(JobExecutionContext context) { |
||||||
|
try { |
||||||
|
return context.getScheduler().getCurrentlyExecutingJobs().stream() |
||||||
|
.filter(j -> j.getTrigger().equals(context.getTrigger())) |
||||||
|
.filter(j -> !j.getFireInstanceId().equals(context.getFireInstanceId())) |
||||||
|
.findFirst() |
||||||
|
.isPresent(); |
||||||
|
} catch (SchedulerException e) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void execute(JobExecutionContext context) throws JobExecutionException { |
||||||
|
if (isRunning(context)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
// 查出表中所有的数据,计算是否到达备份时间,如果到达则进行备份
|
||||||
|
List<LogConfig> list = logConfigRepository.all().fetch(); |
||||||
|
String backupTime = null; |
||||||
|
String filePath = null; |
||||||
|
LogConfig logConfig = null; |
||||||
|
for (LogConfig config : list) { |
||||||
|
logConfig = logConfigRepository.find(config.getId()); |
||||||
|
if ((System.currentTimeMillis() > logService.getLongTime(logConfig.getLogEndTime())) |
||||||
|
&& (LogStatus.waitBackup.getValue().equals(logConfig.getStatus().getValue()) |
||||||
|
|| LogStatus.backupFail.getValue().equals(logConfig.getStatus().getValue()))) { |
||||||
|
// 此条记录过了备份时间并且状态为待备份或备份失败,开始备份,首先查出所有符合时间的记录
|
||||||
|
Map<String, List<LogEntity>> logEntityListMap = logService.selectRecord(logConfig); |
||||||
|
try { |
||||||
|
filePath = logService.backupLogExcelZip(logEntityListMap); |
||||||
|
} catch (IOException e) { |
||||||
|
// 如果出现异常,将状态改为备份失败
|
||||||
|
logConfig.setStatus(LogStatus.backupFail); |
||||||
|
logService.saveConfig(logConfig); |
||||||
|
continue; |
||||||
|
} |
||||||
|
// 需要将状态改为已备份,并添加备份时间,计算备份大小,将当前配置改为历史配置
|
||||||
|
logConfig.setStatus(LogStatus.alreadyBackup); |
||||||
|
backupTime = logService.getStringTime(System.currentTimeMillis()); |
||||||
|
logConfig.setBackupStartTime(backupTime); |
||||||
|
logConfig.setBackupTime(backupTime); |
||||||
|
// 计算文件大小,单位以M表示,精确小数点后两位
|
||||||
|
Map<String, String> fileSizeMap = logService.getSize(filePath); |
||||||
|
logConfig.setThresholdSystemManageUserSize(fileSizeMap.get("16")); |
||||||
|
logConfig.setThresholdSecurityUserSize(fileSizeMap.get("1")); |
||||||
|
logConfig.setThresholdSecurityAuditUserSize(fileSizeMap.get("2")); |
||||||
|
logConfig.setThresholdServiceUserSize(fileSizeMap.get("3")); |
||||||
|
|
||||||
|
logConfig.setFilePath(filePath); |
||||||
|
if (logConfig.getRecordType().equals("Current")) { |
||||||
|
logConfig.setRecordType("History"); |
||||||
|
// 如果是当前配置,要添加一条新的记录,将记录改为待配置,因为当前是主配置,历史配置的话不需要,备份一次即可
|
||||||
|
LogConfig newRecord = new LogConfig(); |
||||||
|
newRecord.setRecordType("Current"); |
||||||
|
// 日志开始时间就是上次备份时间,根据备份周期计算日志结束时间
|
||||||
|
newRecord.setLogStartTime(backupTime); |
||||||
|
newRecord.setLogEndTime(logService.getEndTime(backupTime, logConfig.getBackupCycle())); |
||||||
|
newRecord.setStatus(LogStatus.waitBackup); |
||||||
|
logService.saveConfig(newRecord); |
||||||
|
} |
||||||
|
logService.saveConfig(logConfig); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue