diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java index ca4ac8aba..31280685b 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java @@ -9,9 +9,8 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Update; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; /** @@ -44,19 +43,14 @@ public interface JobLogMapper extends BaseMapperX { ); } - // 另外,timingJobCleanLog 的具体 sql 这么写,性能是比较差的;可以直接 delete * from job_log where create_time < xxx order by id limit 100; - /** - * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 - * 如果写xml的话就需要多写一个映射类 + * 物理删除指定时间之前的日志 * - * @param jobCleanRetainDay 时间限制 - * @param deleteLimit 删除次数的限制 - * @return + * @param createTime 最大时间 + * @param limit 删除条数,防止一次删除太多 + * @return 删除条数 */ - @Delete("DELETE FROM infra_job_log WHERE create_time < #{jobCleanRetainDay} LIMIT #{deleteLimit}") - Integer deleteByCreateTimeLt(@Param("jobCleanRetainDay") Date jobCleanRetainDay,@Param("deleteLimit")Integer deleteLimit); + @Delete("DELETE FROM infra_job_log WHERE create_time < #{createTime} LIMIT #{limit}") + Integer deleteByCreateTimeLt(@Param("createTime") LocalDateTime createTime, @Param("limit") Integer limit); - @Update("ALTER TABLE infra_job_log FORCE") - void optimizeTable(); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java index 11f04452c..77c479817 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java @@ -9,9 +9,8 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Update; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; /** @@ -49,16 +48,13 @@ public interface ApiAccessLogMapper extends BaseMapperX { } /** - * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 - * 如果写xml的话就需要多写一个映射类 + * 物理删除指定时间之前的日志 * - * @param accessLogExceedDay 时间限制 - * @param deleteLimit 删除次数的限制 - * @return + * @param createTime 最大时间 + * @param limit 删除条数,防止一次删除太多 + * @return 删除条数 */ - @Delete("DELETE FROM infra_api_access_log WHERE create_time < #{accessLogExceedDay} LIMIT #{deleteLimit}") - Integer deleteByCreateTimeLt(@Param("accessLogExceedDay") Date accessLogExceedDay,@Param("deleteLimit")Integer deleteLimit); + @Delete("DELETE FROM infra_api_access_log WHERE create_time < #{createTime} LIMIT #{limit}") + Integer deleteByCreateTimeLt(@Param("createTime") LocalDateTime createTime, @Param("limit") Integer limit); - @Update("ALTER TABLE infra_api_access_log FORCE") - void optimizeTable(); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java index d1f3db718..4a9432ef4 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java @@ -9,9 +9,8 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Update; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; /** @@ -47,16 +46,13 @@ public interface ApiErrorLogMapper extends BaseMapperX { } /** - * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 - * 如果写xml的话就需要多写一个映射类 + * 物理删除指定时间之前的日志 * - * @param errorLogExceedDay 时间限制 - * @param deleteLimit 删除次数的限制 - * @return + * @param createTime 最大时间 + * @param limit 删除条数,防止一次删除太多 + * @return 删除条数 */ - @Delete("DELETE FROM infra_api_error_log WHERE create_time < #{errorLogExceedDay} LIMIT #{deleteLimit}") - Integer deleteByCreateTimeLt(@Param("errorLogExceedDay") Date errorLogExceedDay,@Param("deleteLimit")Integer deleteLimit); + @Delete("DELETE FROM infra_api_error_log WHERE create_time < #{createTime} LIMIT #{limit}") + Integer deleteByCreateTimeLt(@Param("createTime") LocalDateTime createTime, @Param("limit")Integer limit); - @Update("ALTER TABLE infra_api_error_log FORCE") - void optimizeTable(); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/AccessLogCleanRecordJob.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/AccessLogCleanJob.java similarity index 55% rename from yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/AccessLogCleanRecordJob.java rename to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/AccessLogCleanJob.java index 90513fed8..2e7198de0 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/AccessLogCleanRecordJob.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/AccessLogCleanJob.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.module.infra.job; +package cn.iocoder.yudao.module.infra.job.job; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; @@ -9,33 +9,33 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; /** - * 定时 物理 删除访问日志的 Job + * 物理删除 N 天前的访问日志的 Job * - * @author: j-sentinel + * @author j-sentinel */ -@Slf4j @Component -public class AccessLogCleanRecordJob implements JobHandler { +@Slf4j +public class AccessLogCleanJob implements JobHandler { @Resource private ApiAccessLogService apiAccessLogService; /** - * 清理超过(7)天的日志 + * 清理超过(14)天的日志 */ - private static final Integer JOB_CLEAN_RETAIN_DAY = 7; + private static final Integer JOB_CLEAN_RETAIN_DAY = 14; /** - * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 + * 每次删除间隔的条数,如果值太高可能会造成数据库的压力过大 */ private static final Integer DELETE_LIMIT = 100; @Override @TenantIgnore - public String execute(String param) throws Exception { - Integer integer = apiAccessLogService.jobCleanAccessLog(JOB_CLEAN_RETAIN_DAY,DELETE_LIMIT); - log.info("定时执行清理访问日志数量({})个", integer); - return String.format("定时执行清理错误日志数量 %s 个", integer); + public String execute(String param) { + Integer count = apiAccessLogService.cleanAccessLog(JOB_CLEAN_RETAIN_DAY, DELETE_LIMIT); + log.info("[count][定时执行清理访问日志数量 ({}) 个]", count); + return String.format("定时执行清理错误日志数量 %s 个", count); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ErrorLogCleanRecordJob.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/ErrorLogCleanJob.java similarity index 52% rename from yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ErrorLogCleanRecordJob.java rename to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/ErrorLogCleanJob.java index 5800a3fae..7e69fd272 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ErrorLogCleanRecordJob.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/job/ErrorLogCleanJob.java @@ -1,8 +1,7 @@ -package cn.iocoder.yudao.module.infra.job; +package cn.iocoder.yudao.module.infra.job.job; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; -import cn.iocoder.yudao.module.infra.service.logger.ApiAccessLogService; import cn.iocoder.yudao.module.infra.service.logger.ApiErrorLogService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -10,33 +9,33 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; /** - * 定时 物理 删除错误日志的 Job + * 物理删除 N 天前的错误日志的 Job * - * @author: j-sentinel + * @author j-sentinel */ @Slf4j @Component -public class ErrorLogCleanRecordJob implements JobHandler { +public class ErrorLogCleanJob implements JobHandler { @Resource private ApiErrorLogService apiErrorLogService; /** - * 清理超过(7)天的日志 + * 清理超过(14)天的日志 */ - private static final Integer JOB_CLEAN_RETAIN_DAY = 7; + private static final Integer JOB_CLEAN_RETAIN_DAY = 14; /** - * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 + * 每次删除间隔的条数,如果值太高可能会造成数据库的压力过大 */ private static final Integer DELETE_LIMIT = 100; @Override @TenantIgnore - public String execute(String param) throws Exception { - Integer integer = apiErrorLogService.jobCleanErrorLog(JOB_CLEAN_RETAIN_DAY,DELETE_LIMIT); - log.info("定时执行清理错误日志数量({})个",integer); - return String.format("定时执行清理错误日志数量 %s 个", integer); + public String execute(String param) { + Integer count = apiErrorLogService.cleanErrorLog(JOB_CLEAN_RETAIN_DAY,DELETE_LIMIT); + log.info("[count][定时执行清理错误日志数量 ({}) 个]", count); + return String.format("定时执行清理错误日志数量 %s 个", count); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/JobCleanRecordJob.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/logger/JobLogCleanJob.java similarity index 62% rename from yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/JobCleanRecordJob.java rename to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/logger/JobLogCleanJob.java index fad19c692..4d8c955a1 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/JobCleanRecordJob.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/logger/JobLogCleanJob.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.module.infra.job; +package cn.iocoder.yudao.module.infra.job.logger; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; @@ -8,13 +8,13 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; /** - * 定时 物理 删除任务日志的 Job + * 物理删除 N 天前的任务日志的 Job * - * @author: j-sentinel + * @author j-sentinel */ @Slf4j @Component -public class JobCleanRecordJob implements JobHandler { +public class JobLogCleanJob implements JobHandler { @Resource private JobLogService jobLogService; @@ -25,16 +25,16 @@ public class JobCleanRecordJob implements JobHandler { private static final Integer JOB_CLEAN_RETAIN_DAY = 14; /** - * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 + * 每次删除间隔的条数,如果值太高可能会造成数据库的压力过大 */ private static final Integer DELETE_LIMIT = 100; @Override @TenantIgnore - public String execute(String param) throws Exception { - Integer integer = jobLogService.timingJobCleanLog(JOB_CLEAN_RETAIN_DAY,DELETE_LIMIT); - log.info("定时执行清理定时任务日志数量({})个",integer); - return String.format("定时执行清理定时任务日志数量 %s 个", integer); + public String execute(String param) { + Integer count = jobLogService.cleanJobLog(JOB_CLEAN_RETAIN_DAY, DELETE_LIMIT); + log.info("[count][定时执行清理定时任务日志数量 ({}) 个]", count); + return String.format("定时执行清理定时任务日志数量 %s 个", count); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogService.java index cfd2cabd0..87be870b4 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogService.java @@ -49,10 +49,11 @@ public interface JobLogService extends JobLogFrameworkService { List getJobLogList(JobLogExportReqVO exportReqVO); /** - * 清理 @param jobCleanRetainDay 天的访问日志 + * 清理 exceedDay 天前的任务日志 * - * @param jobLogExceedDay 超过多少天就进行清理 + * @param exceedDay 超过多少天就进行清理 * @param deleteLimit 清理的间隔条数 */ - Integer timingJobCleanLog(Integer jobLogExceedDay,Integer deleteLimit); + Integer cleanJobLog(Integer exceedDay, Integer deleteLimit); + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImpl.java index d2d635801..cab33079d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImpl.java @@ -1,8 +1,6 @@ package cn.iocoder.yudao.module.infra.service.job; -import cn.hutool.core.date.DateUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO; import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; @@ -16,7 +14,6 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.Collection; -import java.util.Date; import java.util.List; /** @@ -54,26 +51,19 @@ public class JobLogServiceImpl implements JobLogService { } @Override - public Integer timingJobCleanLog(Integer jobLogExceedDay,Integer deleteLimit) { - Integer result; + @SuppressWarnings("DuplicatedCode") + public Integer cleanJobLog(Integer exceedDay, Integer deleteLimit) { int count = 0; - Date currentDate = DateUtil.date(); - // 计算过期日期:正数向未来偏移,负数向历史偏移 - Date expireDate = DateUtil.offsetDay(currentDate, -jobLogExceedDay); + LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay); + // 循环删除,直到没有满足条件的数据 for (int i = 0; i < Short.MAX_VALUE; i++) { - result = jobLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); - count += result; - if (result < deleteLimit) { - // 达到删除预期条数 + int deleteCount = jobLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit); + count += deleteCount; + // 达到删除预期条数,说明到底了 + if (deleteCount < deleteLimit) { break; } } - if(count > 0){ - // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 - // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 - // 优化表语句官方文档:https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html - jobLogMapper.optimizeTable(); - } return count; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java index b6de0b397..3f2a005e1 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java @@ -39,10 +39,11 @@ public interface ApiAccessLogService { List getApiAccessLogList(ApiAccessLogExportReqVO exportReqVO); /** - * 清理 @param accessLogJobDay 天的访问日志 + * 清理 exceedDay 天前的访问日志 * - * @param accessLogExceedDay 超过多少天就进行清理 + * @param exceedDay 超过多少天就进行清理 * @param deleteLimit 清理的间隔条数 */ - Integer jobCleanAccessLog(Integer accessLogExceedDay,Integer deleteLimit); + Integer cleanAccessLog(Integer exceedDay, Integer deleteLimit); + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImpl.java index a81f4e115..12655542a 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImpl.java @@ -1,8 +1,6 @@ package cn.iocoder.yudao.module.infra.service.logger; -import cn.hutool.core.date.DateUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO; import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; @@ -14,7 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; /** @@ -47,26 +45,19 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { } @Override - public Integer jobCleanAccessLog(Integer accessLogExceedDay,Integer deleteLimit) { - Integer result; + @SuppressWarnings("DuplicatedCode") + public Integer cleanAccessLog(Integer exceedDay, Integer deleteLimit) { int count = 0; - Date currentDate = DateUtil.date(); - // 计算过期日期:正数向未来偏移,负数向历史偏移 - Date expireDate = DateUtil.offsetDay(currentDate, -accessLogExceedDay); + LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay); + // 循环删除,直到没有满足条件的数据 for (int i = 0; i < Short.MAX_VALUE; i++) { - result = apiAccessLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); - count += result; - if (result < deleteLimit) { - // 达到删除预期条数 + int deleteCount = apiAccessLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit); + count += deleteCount; + // 达到删除预期条数,说明到底了 + if (deleteCount < deleteLimit) { break; } } - if(count > 0){ - // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 - // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 - // 优化表语句官方文档:https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html - apiAccessLogMapper.optimizeTable(); - } return count; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java index c3adfaacb..04c1efd39 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java @@ -48,10 +48,11 @@ public interface ApiErrorLogService { void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId); /** - * 清理 @param errorLogJobDay 天的访问日志 + * 清理 exceedDay 天前的错误日志 * - * @param errorLogExceedDay 超过多少天就进行清理 + * @param exceedDay 超过多少天就进行清理 * @param deleteLimit 清理的间隔条数 */ - Integer jobCleanErrorLog(Integer errorLogExceedDay,Integer deleteLimit); + Integer cleanErrorLog(Integer exceedDay, Integer deleteLimit); + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java index 697b3f88b..14a0141ef 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java @@ -1,8 +1,6 @@ package cn.iocoder.yudao.module.infra.service.logger; -import cn.hutool.core.date.DateUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO; import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; @@ -16,7 +14,6 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.time.LocalDateTime; -import java.util.Date; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -68,26 +65,19 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { } @Override - public Integer jobCleanErrorLog(Integer errorLogExceedDay,Integer deleteLimit) { - Integer result; + @SuppressWarnings("DuplicatedCode") + public Integer cleanErrorLog(Integer exceedDay, Integer deleteLimit) { int count = 0; - Date currentDate = DateUtil.date(); - // 计算过期日期:正数向未来偏移,负数向历史偏移 - Date expireDate = DateUtil.offsetDay(currentDate, -errorLogExceedDay); + LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay); + // 循环删除,直到没有满足条件的数据 for (int i = 0; i < Short.MAX_VALUE; i++) { - result = apiErrorLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); - count += result; - if (result < deleteLimit) { - // 达到删除预期条数 + int deleteCount = apiErrorLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit); + count += deleteCount; + // 达到删除预期条数,说明到底了 + if (deleteCount < deleteLimit) { break; } } - if(count > 0){ - // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 - // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 - // 优化表语句官方文档:https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html - apiErrorLogMapper.optimizeTable(); - } return count; } diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 9761e0e05..fe588bc90 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -9,7 +9,7 @@ spring: exclude: - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源 - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration # 排除积木报表带来的 MongoDB 的自动配置 - - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置 +# - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置 - de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration # 禁用 Spring Boot Admin 的 Server 的自动配置 - de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration # 禁用 Spring Boot Admin 的 Server UI 的自动配置 - de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration # 禁用 Spring Boot Admin 的 Client 的自动配置