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