diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java index aaf4fe5c47..0f82712a2b 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java @@ -1,9 +1,12 @@ package cn.iocoder.yudao.framework.quartz.config; +import cn.iocoder.yudao.framework.quartz.core.job.JobLogJobHandler; +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; import lombok.extern.slf4j.Slf4j; import org.quartz.Scheduler; import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; @@ -15,6 +18,7 @@ import java.util.Optional; @AutoConfiguration @EnableScheduling // 开启 Spring 自带的定时任务 @Slf4j +@EnableConfigurationProperties(LogJobProperties.class) public class YudaoQuartzAutoConfiguration { @Bean @@ -26,4 +30,9 @@ public class YudaoQuartzAutoConfiguration { return new SchedulerManager(scheduler.get()); } + @Bean + public JobLogJobHandler jobLogJobHandler(LogJobProperties logJobProperties){ + return new JobLogJobHandler(logJobProperties); + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/JobLogJobHandler.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/JobLogJobHandler.java new file mode 100644 index 0000000000..53565102d3 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/JobLogJobHandler.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.framework.quartz.core.job; + +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; +import cn.iocoder.yudao.framework.quartz.core.service.JobLogFrameworkService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Resource; + +/** + * @Author: j-sentinel + * @Date: 2023/9/30 20:40 + */ +@Slf4j +@AllArgsConstructor +public class JobLogJobHandler implements JobHandler { + + private LogJobProperties logJobProperties; + + public JobLogJobHandler(LogJobProperties logJobProperties) { + this.logJobProperties = logJobProperties; + } + + @Resource + private JobLogFrameworkService jobLogFrameworkService; + + @Override + public String execute(String param) throws Exception { + Integer integer = jobLogFrameworkService.timingJobCleanLog(logJobProperties.getJobCleanRetainDay()); + log.info("定时执行清理定时任务日志数量({})个",integer); + return String.format("定时执行清理定时任务日志数量 %s 个", integer); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/LogJobProperties.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/LogJobProperties.java new file mode 100644 index 0000000000..f563fec193 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/LogJobProperties.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.framework.quartz.core.job; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @Author: j-sentinel + * @Date: 2023/9/30 16:17 + */ +@Data +@ConfigurationProperties(prefix = "yudao.clean-job") +public class LogJobProperties { + + private int accessRetainDay = 7; + + private int errorRetainDay = 8; + + private int jobCleanRetainDay = 7; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java index 889921dfd9..45de8cea00 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java @@ -41,4 +41,10 @@ public interface JobLogFrameworkService { @NotNull(message = "运行时长不能为空") Integer duration, boolean success, String result); + /** + * 清理 @param jobCleanRetainDay 天的访问日志 + * + * @param jobCleanRetainDay 超过多少天就进行清理 + */ + Integer timingJobCleanLog(Integer jobCleanRetainDay); } diff --git a/yudao-framework/yudao-spring-boot-starter-web/pom.xml b/yudao-framework/yudao-spring-boot-starter-web/pom.xml index 50d986c066..b345fb31ec 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-web/pom.xml @@ -61,6 +61,12 @@ jsoup + + + cn.iocoder.boot + yudao-spring-boot-starter-job + + diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java index 6ced68756e..b8e18b18d3 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java @@ -1,11 +1,14 @@ package cn.iocoder.yudao.framework.apilog.config; import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter; +import cn.iocoder.yudao.framework.apilog.core.job.ApiAccessLogJobHandler; +import cn.iocoder.yudao.framework.apilog.core.job.ApiErrorLogJobHandler; import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl; import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl; import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; import cn.iocoder.yudao.framework.web.config.WebProperties; import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration; import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; @@ -31,6 +34,16 @@ public class YudaoApiLogAutoConfiguration { return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi); } + @Bean + public ApiAccessLogJobHandler apiAccessLogJobHandler(LogJobProperties logJobProperties) { + return new ApiAccessLogJobHandler(logJobProperties); + } + + @Bean + public ApiErrorLogJobHandler apiErrorLogJobHandler(LogJobProperties logJobProperties) { + return new ApiErrorLogJobHandler(logJobProperties); + } + /** * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 */ diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java new file mode 100644 index 0000000000..f6d512fa01 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.framework.apilog.core.job; + +import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Resource; + +/** + * @Author: j-sentinel + * @Date: 2023/9/30 16:13 + */ +@Slf4j +@AllArgsConstructor +public class ApiAccessLogJobHandler implements JobHandler { + + private LogJobProperties logJobProperties; + + public ApiAccessLogJobHandler(LogJobProperties logJobProperties) { + this.logJobProperties = logJobProperties; + } + + @Resource + private ApiAccessLogFrameworkService apiAccessLogFrameworkService; + + @Override + public String execute(String param) throws Exception { + apiAccessLogFrameworkService.jobCleanAccessLog(logJobProperties.getAccessRetainDay()); + return ""; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiErrorLogJobHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiErrorLogJobHandler.java new file mode 100644 index 0000000000..adbfa45b6f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiErrorLogJobHandler.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.framework.apilog.core.job; + +import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Resource; + +/** + * @Author: j-sentinel + * @Date: 2023/9/30 16:13 + */ +@Slf4j +@AllArgsConstructor +public class ApiErrorLogJobHandler implements JobHandler { + + private LogJobProperties logJobProperties; + + public ApiErrorLogJobHandler(LogJobProperties logJobProperties) { + this.logJobProperties = logJobProperties; + } + + @Resource + private ApiErrorLogFrameworkService apiErrorLogFrameworkService; + + @Override + public String execute(String param) throws Exception { + apiErrorLogFrameworkService.jobCleanErrorLog(logJobProperties.getErrorRetainDay()); + return ""; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java index 9bfc3a573d..b9de0b680e 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java @@ -14,4 +14,10 @@ public interface ApiAccessLogFrameworkService { */ void createApiAccessLog(ApiAccessLog apiAccessLog); + /** + * 清理 @param accessLogJobDay 天的访问日志 + * + * @param accessLogJobDay 超过多少天就进行清理 + */ + void jobCleanAccessLog(Integer accessLogJobDay); } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java index 83162f1643..e8a948c145 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java @@ -25,4 +25,9 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe apiAccessLogApi.createApiAccessLog(reqDTO); } + @Override + public void jobCleanAccessLog(Integer accessLogJobDay) { + apiAccessLogApi.jobCleanAccessLog(accessLogJobDay); + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java index 403c574bc9..fb4e1b029c 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java @@ -14,4 +14,10 @@ public interface ApiErrorLogFrameworkService { */ void createApiErrorLog(ApiErrorLog apiErrorLog); + /** + * 清理 @param errorLogJobDay 天的访问日志 + * + * @param errorLogJobDay 超过多少天就进行清理 + */ + void jobCleanErrorLog(Integer errorLogJobDay); } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java index cb5abe3c28..dfcdc7eddf 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java @@ -25,4 +25,9 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ apiErrorLogApi.createApiErrorLog(reqDTO); } + @Override + public void jobCleanErrorLog(Integer errorLogJobDay) { + apiErrorLogApi.jobCleanErrorLog(errorLogJobDay); + } + } diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java index ed3f3ee1e5..63a1f38995 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java @@ -18,4 +18,10 @@ public interface ApiAccessLogApi { */ void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO); + /** + * 清理 @param accessLogJobDay 天的访问日志 + * + * @param accessLogJobDay 超过多少天就进行清理 + */ + void jobCleanAccessLog(Integer accessLogJobDay); } diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java index 9b53c66438..7554461be6 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java @@ -18,4 +18,10 @@ public interface ApiErrorLogApi { */ void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO); + /** + * 清理 @param errorLogJobDay 天的访问日志 + * + * @param errorLogJobDay 超过多少天就进行清理 + */ + void jobCleanErrorLog(Integer errorLogJobDay); } diff --git a/yudao-module-infra/yudao-module-infra-biz/pom.xml b/yudao-module-infra/yudao-module-infra-biz/pom.xml index 6d7ac0cfff..af5fb9ac29 100644 --- a/yudao-module-infra/yudao-module-infra-biz/pom.xml +++ b/yudao-module-infra/yudao-module-infra-biz/pom.xml @@ -35,6 +35,10 @@ cn.iocoder.boot yudao-spring-boot-starter-biz-operatelog + + cn.iocoder.boot + yudao-spring-boot-starter-biz-tenant + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java index 2daa4a1c46..283bfc12fc 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java @@ -24,4 +24,9 @@ public class ApiAccessLogApiImpl implements ApiAccessLogApi { apiAccessLogService.createApiAccessLog(createDTO); } + @Override + public void jobCleanAccessLog(Integer accessLogJobDay) { + apiAccessLogService.jobCleanAccessLog(accessLogJobDay); + } + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java index cdb0ec2936..e1043b7de5 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java @@ -24,4 +24,9 @@ public class ApiErrorLogApiImpl implements ApiErrorLogApi { apiErrorLogService.createApiErrorLog(createDTO); } + @Override + public void jobCleanErrorLog(Integer errorLogJobDay) { + apiErrorLogService.jobCleanErrorLog(errorLogJobDay); + } + } 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 c467498bfd..377136a826 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 @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReq import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -40,4 +41,7 @@ public interface JobLogMapper extends BaseMapperX { ); } + Integer timingJobCleanLog(@Param("jobCleanRetainDay") Integer jobCleanRetainDay); + + 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 4276756366..dd31074071 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 @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.Api import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -44,4 +45,7 @@ public interface ApiAccessLogMapper extends BaseMapperX { ); } + Integer jobCleanAccessLog(@Param("accessLogJobDay") Integer accessLogJobDay); + + 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 e9748b6247..617247d7d0 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 @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiE import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -42,4 +43,7 @@ public interface ApiErrorLogMapper extends BaseMapperX { ); } + Integer jobCleanErrorLog(@Param("errorLogJobDay") Integer errorLogJobDay); + + void optimizeTable(); } 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 dcf8d302bf..353860bd50 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 @@ -26,6 +26,8 @@ import java.util.List; @Slf4j public class JobLogServiceImpl implements JobLogService { + private static final Integer DELETE_LIMIT = 1; + @Resource private JobLogMapper jobLogMapper; @@ -50,6 +52,22 @@ public class JobLogServiceImpl implements JobLogService { } } + @Override + public Integer timingJobCleanLog(Integer jobCleanRetainDay) { + Integer result = null; + int count = 0; + while (result == null || DELETE_LIMIT.equals(result)){ + result = jobLogMapper.timingJobCleanLog(jobCleanRetainDay); + count += result; + } + if(count > 0){ + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 + jobLogMapper.optimizeTable(); + } + return count; + } + @Override public JobLogDO getJobLog(Long id) { return jobLogMapper.selectById(id); 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 e473b2322e..7292f12735 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 @@ -38,4 +38,10 @@ public interface ApiAccessLogService { */ List getApiAccessLogList(ApiAccessLogExportReqVO exportReqVO); + /** + * 清理 @param accessLogJobDay 天的访问日志 + * + * @param accessLogJobDay 超过多少天就进行清理 + */ + void jobCleanAccessLog(Integer accessLogJobDay); } 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 e3b3234ee8..8863aea058 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,12 +1,14 @@ package cn.iocoder.yudao.module.infra.service.logger; 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; import cn.iocoder.yudao.module.infra.convert.logger.ApiAccessLogConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO; import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiAccessLogMapper; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -18,10 +20,13 @@ import java.util.List; * * @author 芋道源码 */ +@Slf4j @Service @Validated public class ApiAccessLogServiceImpl implements ApiAccessLogService { + private static final Integer DELETE_LIMIT = 100; + @Resource private ApiAccessLogMapper apiAccessLogMapper; @@ -41,4 +46,22 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { return apiAccessLogMapper.selectList(exportReqVO); } + @Override + public void jobCleanAccessLog(Integer accessLogJobDay) { + TenantUtils.executeIgnore(() -> { + Integer result = null; + int count = 0; + while (result == null || DELETE_LIMIT.equals(result)) { + result = apiAccessLogMapper.jobCleanAccessLog(accessLogJobDay); + count += result; + } + if (count > 0) { + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 + apiAccessLogMapper.optimizeTable(); + } + log.info("定时执行清理访问日志数量({})个", 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 138c9bef40..4dd881e1f0 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 @@ -47,4 +47,10 @@ public interface ApiErrorLogService { */ void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId); + /** + * 清理 @param errorLogJobDay 天的访问日志 + * + * @param errorLogJobDay 超过多少天就进行清理 + */ + void jobCleanErrorLog(Integer errorLogJobDay); } 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 c0f9252af8..fc74c71129 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,6 +1,7 @@ package cn.iocoder.yudao.module.infra.service.logger; 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; @@ -8,6 +9,7 @@ import cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper; import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -24,10 +26,13 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_L * * @author 芋道源码 */ +@Slf4j @Service @Validated public class ApiErrorLogServiceImpl implements ApiErrorLogService { + private static final Integer DELETE_LIMIT = 100; + @Resource private ApiErrorLogMapper apiErrorLogMapper; @@ -62,4 +67,22 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { .processUserId(processUserId).processTime(LocalDateTime.now()).build()); } + @Override + public void jobCleanErrorLog(Integer errorLogJobDay) { + TenantUtils.executeIgnore(() -> { + Integer result = null; + int count = 0; + while (result == null || DELETE_LIMIT.equals(result)) { + result = apiErrorLogMapper.jobCleanErrorLog(errorLogJobDay); + count += result; + } + if (count > 0) { + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 + apiErrorLogMapper.optimizeTable(); + } + log.info("定时执行清理错误日志数量({})个",count); + }); + } + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiAccessLogMapper.xml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiAccessLogMapper.xml new file mode 100644 index 0000000000..ae7694d565 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiAccessLogMapper.xml @@ -0,0 +1,32 @@ + + + + + + + + DELETE FROM infra_api_access_log + WHERE id IN ( + SELECT * FROM ( + SELECT id FROM infra_api_access_log + WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{accessLogJobDay} DAY) + ORDER BY id ASC LIMIT 100 + ) AS a + ) + + + + + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiErrorLogMapper.xml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiErrorLogMapper.xml new file mode 100644 index 0000000000..842b45ee52 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiErrorLogMapper.xml @@ -0,0 +1,32 @@ + + + + + + + + DELETE FROM infra_api_error_log + WHERE id IN ( + SELECT * FROM ( + SELECT id FROM infra_api_error_log + WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{errorLogJobDay} DAY) + ORDER BY id ASC LIMIT 100 + ) AS a + ) + + + + + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/JobLogMapper.xml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/JobLogMapper.xml new file mode 100644 index 0000000000..e8d9d5ab8f --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/JobLogMapper.xml @@ -0,0 +1,32 @@ + + + + + + + + DELETE FROM infra_job_log + WHERE id IN ( + SELECT * FROM ( + SELECT id FROM infra_job_log + WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{jobCleanRetainDay} DAY) + ORDER BY id ASC LIMIT 100 + ) AS a + ) + + + + + diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 2908a4166c..4f7d1a18a7 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -213,6 +213,10 @@ yudao: error-code: # 错误码相关配置项 enable: false demo: false # 关闭演示模式 + clean-job: + access-retain-day: 7 + error-retain-day: 8 + job-clean-retain-day: 8 justauth: enabled: true