Commit 2f0483ce9f113082ede1cc2d7499025302f91925
1 parent
fdb10aec
[新增][定时任务]重新申请 ApiAccessLog、ApiErrorLog、JobLog 3个日志的定时清理
Showing
32 changed files
with
238 additions
and
369 deletions
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java
| 1 | package cn.iocoder.yudao.framework.quartz.config; | 1 | package cn.iocoder.yudao.framework.quartz.config; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.quartz.core.job.JobLogJobHandler; | ||
| 4 | -import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; | ||
| 5 | import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; | 3 | import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; |
| 6 | import lombok.extern.slf4j.Slf4j; | 4 | import lombok.extern.slf4j.Slf4j; |
| 7 | import org.quartz.Scheduler; | 5 | import org.quartz.Scheduler; |
| 8 | import org.springframework.boot.autoconfigure.AutoConfiguration; | 6 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 9 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| 10 | import org.springframework.context.annotation.Bean; | 7 | import org.springframework.context.annotation.Bean; |
| 11 | import org.springframework.scheduling.annotation.EnableScheduling; | 8 | import org.springframework.scheduling.annotation.EnableScheduling; |
| 12 | 9 | ||
| @@ -18,7 +15,6 @@ import java.util.Optional; | @@ -18,7 +15,6 @@ import java.util.Optional; | ||
| 18 | @AutoConfiguration | 15 | @AutoConfiguration |
| 19 | @EnableScheduling // 开启 Spring 自带的定时任务 | 16 | @EnableScheduling // 开启 Spring 自带的定时任务 |
| 20 | @Slf4j | 17 | @Slf4j |
| 21 | -@EnableConfigurationProperties(LogJobProperties.class) | ||
| 22 | public class YudaoQuartzAutoConfiguration { | 18 | public class YudaoQuartzAutoConfiguration { |
| 23 | 19 | ||
| 24 | @Bean | 20 | @Bean |
| @@ -30,10 +26,4 @@ public class YudaoQuartzAutoConfiguration { | @@ -30,10 +26,4 @@ public class YudaoQuartzAutoConfiguration { | ||
| 30 | return new SchedulerManager(scheduler.get()); | 26 | return new SchedulerManager(scheduler.get()); |
| 31 | } | 27 | } |
| 32 | 28 | ||
| 33 | - // TODO @j-sentinel:这个 job,先拿到 infra biz 里面实现哈; | ||
| 34 | - @Bean | ||
| 35 | - public JobLogJobHandler jobLogJobHandler(LogJobProperties logJobProperties){ | ||
| 36 | - return new JobLogJobHandler(logJobProperties); | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | } | 29 | } |
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/JobLogJobHandler.java deleted
| 1 | -package cn.iocoder.yudao.framework.quartz.core.job; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 4 | -import cn.iocoder.yudao.framework.quartz.core.service.JobLogFrameworkService; | ||
| 5 | -import lombok.AllArgsConstructor; | ||
| 6 | -import lombok.extern.slf4j.Slf4j; | ||
| 7 | - | ||
| 8 | -import javax.annotation.Resource; | ||
| 9 | - | ||
| 10 | -// TODO @j-sentinel:名字和项目里其它保持统一,可以叫 JobLogCleanJob,不用带 handler 哈 | ||
| 11 | -/** | ||
| 12 | - * // TODO @j-sentinel:要写下类注释噢,就是这个类要干啥;然后下面两个应该是 @author 和 @since | ||
| 13 | - * @Author: j-sentinel | ||
| 14 | - * @Date: 2023/9/30 20:40 | ||
| 15 | - */ | ||
| 16 | -@Slf4j | ||
| 17 | -@AllArgsConstructor | ||
| 18 | -public class JobLogJobHandler implements JobHandler { | ||
| 19 | - | ||
| 20 | - private LogJobProperties logJobProperties; | ||
| 21 | - | ||
| 22 | - public JobLogJobHandler(LogJobProperties logJobProperties) { | ||
| 23 | - this.logJobProperties = logJobProperties; | ||
| 24 | - } | ||
| 25 | - | ||
| 26 | - @Resource | ||
| 27 | - private JobLogFrameworkService jobLogFrameworkService; | ||
| 28 | - | ||
| 29 | - @Override | ||
| 30 | - public String execute(String param) throws Exception { | ||
| 31 | - Integer integer = jobLogFrameworkService.timingJobCleanLog(logJobProperties.getJobCleanRetainDay()); | ||
| 32 | - log.info("定时执行清理定时任务日志数量({})个",integer); | ||
| 33 | - return String.format("定时执行清理定时任务日志数量 %s 个", integer); | ||
| 34 | - } | ||
| 35 | -} |
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/LogJobProperties.java deleted
| 1 | -package cn.iocoder.yudao.framework.quartz.core.job; | ||
| 2 | - | ||
| 3 | -import lombok.Data; | ||
| 4 | -import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| 5 | - | ||
| 6 | -// TODO @j-sentinel:这个配置类,先暂时不做,每个 Job 里面定一个静态类。其实不是所有的变量,都需要配置化,因为它本身基本也不会改动。 | ||
| 7 | -/** | ||
| 8 | - * @Author: j-sentinel | ||
| 9 | - * @Date: 2023/9/30 16:17 | ||
| 10 | - */ | ||
| 11 | -@Data | ||
| 12 | -@ConfigurationProperties(prefix = "yudao.clean-job") | ||
| 13 | -public class LogJobProperties { | ||
| 14 | - | ||
| 15 | - private int accessRetainDay = 7; | ||
| 16 | - | ||
| 17 | - private int errorRetainDay = 8; | ||
| 18 | - | ||
| 19 | - private int jobCleanRetainDay = 7; | ||
| 20 | - | ||
| 21 | -} |
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java
| @@ -40,11 +40,4 @@ public interface JobLogFrameworkService { | @@ -40,11 +40,4 @@ public interface JobLogFrameworkService { | ||
| 40 | @NotNull(message = "结束时间不能为空") LocalDateTime endTime, | 40 | @NotNull(message = "结束时间不能为空") LocalDateTime endTime, |
| 41 | @NotNull(message = "运行时长不能为空") Integer duration, | 41 | @NotNull(message = "运行时长不能为空") Integer duration, |
| 42 | boolean success, String result); | 42 | boolean success, String result); |
| 43 | - | ||
| 44 | - /** | ||
| 45 | - * 清理 @param jobCleanRetainDay 天的访问日志 | ||
| 46 | - * | ||
| 47 | - * @param jobCleanRetainDay 超过多少天就进行清理 | ||
| 48 | - */ | ||
| 49 | - Integer timingJobCleanLog(Integer jobCleanRetainDay); | ||
| 50 | } | 43 | } |
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| @@ -61,12 +61,6 @@ | @@ -61,12 +61,6 @@ | ||
| 61 | <artifactId>jsoup</artifactId> | 61 | <artifactId>jsoup</artifactId> |
| 62 | </dependency> | 62 | </dependency> |
| 63 | 63 | ||
| 64 | - <!-- Job 定时任务相关 --> | ||
| 65 | - <!-- TODO @j-sentinel:去掉这个依赖哈; --> | ||
| 66 | - <dependency> | ||
| 67 | - <groupId>cn.iocoder.boot</groupId> | ||
| 68 | - <artifactId>yudao-spring-boot-starter-job</artifactId> | ||
| 69 | - </dependency> | ||
| 70 | </dependencies> | 64 | </dependencies> |
| 71 | 65 | ||
| 72 | </project> | 66 | </project> |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java
| 1 | package cn.iocoder.yudao.framework.apilog.config; | 1 | package cn.iocoder.yudao.framework.apilog.config; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter; | 3 | import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter; |
| 4 | -import cn.iocoder.yudao.framework.apilog.core.job.ApiAccessLogJobHandler; | ||
| 5 | -import cn.iocoder.yudao.framework.apilog.core.job.ApiErrorLogJobHandler; | ||
| 6 | import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; | 4 | import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; |
| 7 | import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl; | 5 | import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl; |
| 8 | import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; | 6 | import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; |
| 9 | import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl; | 7 | import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl; |
| 10 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | 8 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; |
| 11 | -import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; | ||
| 12 | import cn.iocoder.yudao.framework.web.config.WebProperties; | 9 | import cn.iocoder.yudao.framework.web.config.WebProperties; |
| 13 | import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration; | 10 | import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration; |
| 14 | import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; | 11 | import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; |
| @@ -34,16 +31,6 @@ public class YudaoApiLogAutoConfiguration { | @@ -34,16 +31,6 @@ public class YudaoApiLogAutoConfiguration { | ||
| 34 | return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi); | 31 | return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi); |
| 35 | } | 32 | } |
| 36 | 33 | ||
| 37 | - @Bean | ||
| 38 | - public ApiAccessLogJobHandler apiAccessLogJobHandler(LogJobProperties logJobProperties) { | ||
| 39 | - return new ApiAccessLogJobHandler(logJobProperties); | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - @Bean | ||
| 43 | - public ApiErrorLogJobHandler apiErrorLogJobHandler(LogJobProperties logJobProperties) { | ||
| 44 | - return new ApiErrorLogJobHandler(logJobProperties); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | /** | 34 | /** |
| 48 | * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 | 35 | * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 |
| 49 | */ | 36 | */ |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java deleted
| 1 | -package cn.iocoder.yudao.framework.apilog.core.job; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; | ||
| 4 | -import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 5 | -import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; | ||
| 6 | -import lombok.AllArgsConstructor; | ||
| 7 | -import lombok.extern.slf4j.Slf4j; | ||
| 8 | - | ||
| 9 | -import javax.annotation.Resource; | ||
| 10 | - | ||
| 11 | -// TODO @j-sentinel:同 JobLogJobHandler | ||
| 12 | -/** | ||
| 13 | - * @Author: j-sentinel | ||
| 14 | - * @Date: 2023/9/30 16:13 | ||
| 15 | - */ | ||
| 16 | -@Slf4j | ||
| 17 | -@AllArgsConstructor | ||
| 18 | -public class ApiAccessLogJobHandler implements JobHandler { | ||
| 19 | - | ||
| 20 | - private LogJobProperties logJobProperties; | ||
| 21 | - | ||
| 22 | - public ApiAccessLogJobHandler(LogJobProperties logJobProperties) { | ||
| 23 | - this.logJobProperties = logJobProperties; | ||
| 24 | - } | ||
| 25 | - | ||
| 26 | - @Resource | ||
| 27 | - private ApiAccessLogFrameworkService apiAccessLogFrameworkService; | ||
| 28 | - | ||
| 29 | - @Override | ||
| 30 | - public String execute(String param) throws Exception { | ||
| 31 | - apiAccessLogFrameworkService.jobCleanAccessLog(logJobProperties.getAccessRetainDay()); | ||
| 32 | - return ""; | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiErrorLogJobHandler.java deleted
| 1 | -package cn.iocoder.yudao.framework.apilog.core.job; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; | ||
| 4 | -import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 5 | -import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties; | ||
| 6 | -import lombok.AllArgsConstructor; | ||
| 7 | -import lombok.extern.slf4j.Slf4j; | ||
| 8 | - | ||
| 9 | -import javax.annotation.Resource; | ||
| 10 | - | ||
| 11 | -// TODO @j-sentinel:同 JobLogJobHandler | ||
| 12 | -/** | ||
| 13 | - * @Author: j-sentinel | ||
| 14 | - * @Date: 2023/9/30 16:13 | ||
| 15 | - */ | ||
| 16 | -@Slf4j | ||
| 17 | -@AllArgsConstructor | ||
| 18 | -public class ApiErrorLogJobHandler implements JobHandler { | ||
| 19 | - | ||
| 20 | - private LogJobProperties logJobProperties; | ||
| 21 | - | ||
| 22 | - public ApiErrorLogJobHandler(LogJobProperties logJobProperties) { | ||
| 23 | - this.logJobProperties = logJobProperties; | ||
| 24 | - } | ||
| 25 | - | ||
| 26 | - @Resource | ||
| 27 | - private ApiErrorLogFrameworkService apiErrorLogFrameworkService; | ||
| 28 | - | ||
| 29 | - @Override | ||
| 30 | - public String execute(String param) throws Exception { | ||
| 31 | - apiErrorLogFrameworkService.jobCleanErrorLog(logJobProperties.getErrorRetainDay()); | ||
| 32 | - return ""; | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java
| @@ -13,11 +13,4 @@ public interface ApiAccessLogFrameworkService { | @@ -13,11 +13,4 @@ public interface ApiAccessLogFrameworkService { | ||
| 13 | * @param apiAccessLog API 访问日志 | 13 | * @param apiAccessLog API 访问日志 |
| 14 | */ | 14 | */ |
| 15 | void createApiAccessLog(ApiAccessLog apiAccessLog); | 15 | void createApiAccessLog(ApiAccessLog apiAccessLog); |
| 16 | - | ||
| 17 | - /** | ||
| 18 | - * 清理 @param accessLogJobDay 天的访问日志 | ||
| 19 | - * | ||
| 20 | - * @param accessLogJobDay 超过多少天就进行清理 | ||
| 21 | - */ | ||
| 22 | - void jobCleanAccessLog(Integer accessLogJobDay); | ||
| 23 | } | 16 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java
| @@ -25,9 +25,4 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe | @@ -25,9 +25,4 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe | ||
| 25 | apiAccessLogApi.createApiAccessLog(reqDTO); | 25 | apiAccessLogApi.createApiAccessLog(reqDTO); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | - @Override | ||
| 29 | - public void jobCleanAccessLog(Integer accessLogJobDay) { | ||
| 30 | - apiAccessLogApi.jobCleanAccessLog(accessLogJobDay); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | } | 28 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java
| @@ -13,11 +13,4 @@ public interface ApiErrorLogFrameworkService { | @@ -13,11 +13,4 @@ public interface ApiErrorLogFrameworkService { | ||
| 13 | * @param apiErrorLog API 错误日志 | 13 | * @param apiErrorLog API 错误日志 |
| 14 | */ | 14 | */ |
| 15 | void createApiErrorLog(ApiErrorLog apiErrorLog); | 15 | void createApiErrorLog(ApiErrorLog apiErrorLog); |
| 16 | - | ||
| 17 | - /** | ||
| 18 | - * 清理 @param errorLogJobDay 天的访问日志 | ||
| 19 | - * | ||
| 20 | - * @param errorLogJobDay 超过多少天就进行清理 | ||
| 21 | - */ | ||
| 22 | - void jobCleanErrorLog(Integer errorLogJobDay); | ||
| 23 | } | 16 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java
| @@ -25,9 +25,4 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ | @@ -25,9 +25,4 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ | ||
| 25 | apiErrorLogApi.createApiErrorLog(reqDTO); | 25 | apiErrorLogApi.createApiErrorLog(reqDTO); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | - @Override | ||
| 29 | - public void jobCleanErrorLog(Integer errorLogJobDay) { | ||
| 30 | - apiErrorLogApi.jobCleanErrorLog(errorLogJobDay); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | } | 28 | } |
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java
| @@ -18,12 +18,4 @@ public interface ApiAccessLogApi { | @@ -18,12 +18,4 @@ public interface ApiAccessLogApi { | ||
| 18 | */ | 18 | */ |
| 19 | void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO); | 19 | void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO); |
| 20 | 20 | ||
| 21 | - // TODO @j-sentinel:这个我们先提供接口在 API,而是 infra 模块自己清理先哈; | ||
| 22 | - /** | ||
| 23 | - * 清理 @param accessLogJobDay 天的访问日志 | ||
| 24 | - * | ||
| 25 | - * @param accessLogJobDay 超过多少天就进行清理 | ||
| 26 | - */ | ||
| 27 | - void jobCleanAccessLog(Integer accessLogJobDay); | ||
| 28 | - | ||
| 29 | } | 21 | } |
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java
| @@ -18,12 +18,4 @@ public interface ApiErrorLogApi { | @@ -18,12 +18,4 @@ public interface ApiErrorLogApi { | ||
| 18 | */ | 18 | */ |
| 19 | void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO); | 19 | void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO); |
| 20 | 20 | ||
| 21 | - // TODO @j-sentinel:这个我们先提供接口在 API,而是 infra 模块自己清理先哈; | ||
| 22 | - /** | ||
| 23 | - * 清理 @param errorLogJobDay 天的访问日志 | ||
| 24 | - * | ||
| 25 | - * @param errorLogJobDay 超过多少天就进行清理 | ||
| 26 | - */ | ||
| 27 | - void jobCleanErrorLog(Integer errorLogJobDay); | ||
| 28 | - | ||
| 29 | } | 21 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java
| @@ -24,9 +24,4 @@ public class ApiAccessLogApiImpl implements ApiAccessLogApi { | @@ -24,9 +24,4 @@ public class ApiAccessLogApiImpl implements ApiAccessLogApi { | ||
| 24 | apiAccessLogService.createApiAccessLog(createDTO); | 24 | apiAccessLogService.createApiAccessLog(createDTO); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | - @Override | ||
| 28 | - public void jobCleanAccessLog(Integer accessLogJobDay) { | ||
| 29 | - apiAccessLogService.jobCleanAccessLog(accessLogJobDay); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | } | 27 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java
| @@ -24,9 +24,4 @@ public class ApiErrorLogApiImpl implements ApiErrorLogApi { | @@ -24,9 +24,4 @@ public class ApiErrorLogApiImpl implements ApiErrorLogApi { | ||
| 24 | apiErrorLogService.createApiErrorLog(createDTO); | 24 | apiErrorLogService.createApiErrorLog(createDTO); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | - @Override | ||
| 28 | - public void jobCleanErrorLog(Integer errorLogJobDay) { | ||
| 29 | - apiErrorLogService.jobCleanErrorLog(errorLogJobDay); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | } | 27 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java
| @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 6 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO; | 6 | 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; | 7 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO; |
| 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; | 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; |
| 9 | +import org.apache.ibatis.annotations.Delete; | ||
| 9 | import org.apache.ibatis.annotations.Mapper; | 10 | import org.apache.ibatis.annotations.Mapper; |
| 10 | import org.apache.ibatis.annotations.Param; | 11 | import org.apache.ibatis.annotations.Param; |
| 12 | +import org.apache.ibatis.annotations.Update; | ||
| 11 | 13 | ||
| 14 | +import java.util.Date; | ||
| 12 | import java.util.List; | 15 | import java.util.List; |
| 13 | 16 | ||
| 14 | /** | 17 | /** |
| @@ -41,12 +44,19 @@ public interface JobLogMapper extends BaseMapperX<JobLogDO> { | @@ -41,12 +44,19 @@ public interface JobLogMapper extends BaseMapperX<JobLogDO> { | ||
| 41 | ); | 44 | ); |
| 42 | } | 45 | } |
| 43 | 46 | ||
| 44 | - // TODO @j-sentinel:一般来说,我们 mapper 只提供 crud 的操作,所以这个方法的命名,建议是 deleteByCreateTimeLt; | ||
| 45 | - // 然后,参数是 (crateTime, count),由 service 传入 | ||
| 46 | - // 这里为什么有 lt 呢,这个其实是延续 spring data 的 method 描述的命名习惯,lt 表示小于; | ||
| 47 | // 另外,timingJobCleanLog 的具体 sql 这么写,性能是比较差的;可以直接 delete * from job_log where create_time < xxx order by id limit 100; | 47 | // 另外,timingJobCleanLog 的具体 sql 这么写,性能是比较差的;可以直接 delete * from job_log where create_time < xxx order by id limit 100; |
| 48 | - Integer timingJobCleanLog(@Param("jobCleanRetainDay") Integer jobCleanRetainDay); | ||
| 49 | 48 | ||
| 50 | - // TODO @j-serntinel:optimize table infra_job_log 就可以啦? | 49 | + /** |
| 50 | + * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 | ||
| 51 | + * 如果写xml的话就需要多写一个映射类 | ||
| 52 | + * | ||
| 53 | + * @param jobCleanRetainDay 时间限制 | ||
| 54 | + * @param deleteLimit 删除次数的限制 | ||
| 55 | + * @return | ||
| 56 | + */ | ||
| 57 | + @Delete("DELETE FROM infra_job_log WHERE create_time < #{jobCleanRetainDay} LIMIT #{deleteLimit}") | ||
| 58 | + Integer deleteByCreateTimeLt(@Param("jobCleanRetainDay") Date jobCleanRetainDay,@Param("deleteLimit")Integer deleteLimit); | ||
| 59 | + | ||
| 60 | + @Update("ALTER TABLE infra_job_log FORCE") | ||
| 51 | void optimizeTable(); | 61 | void optimizeTable(); |
| 52 | } | 62 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java
| @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 6 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO; | 6 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO; |
| 7 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; | 7 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; |
| 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO; | 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO; |
| 9 | +import org.apache.ibatis.annotations.Delete; | ||
| 9 | import org.apache.ibatis.annotations.Mapper; | 10 | import org.apache.ibatis.annotations.Mapper; |
| 10 | import org.apache.ibatis.annotations.Param; | 11 | import org.apache.ibatis.annotations.Param; |
| 12 | +import org.apache.ibatis.annotations.Update; | ||
| 11 | 13 | ||
| 14 | +import java.util.Date; | ||
| 12 | import java.util.List; | 15 | import java.util.List; |
| 13 | 16 | ||
| 14 | /** | 17 | /** |
| @@ -45,8 +48,17 @@ public interface ApiAccessLogMapper extends BaseMapperX<ApiAccessLogDO> { | @@ -45,8 +48,17 @@ public interface ApiAccessLogMapper extends BaseMapperX<ApiAccessLogDO> { | ||
| 45 | ); | 48 | ); |
| 46 | } | 49 | } |
| 47 | 50 | ||
| 48 | - // TODO @j-sentinel:同 JobLogMapper 的一些优化点 | ||
| 49 | - Integer jobCleanAccessLog(@Param("accessLogJobDay") Integer accessLogJobDay); | 51 | + /** |
| 52 | + * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 | ||
| 53 | + * 如果写xml的话就需要多写一个映射类 | ||
| 54 | + * | ||
| 55 | + * @param accessLogExceedDay 时间限制 | ||
| 56 | + * @param deleteLimit 删除次数的限制 | ||
| 57 | + * @return | ||
| 58 | + */ | ||
| 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); | ||
| 50 | 61 | ||
| 62 | + @Update("ALTER TABLE infra_api_access_log FORCE") | ||
| 51 | void optimizeTable(); | 63 | void optimizeTable(); |
| 52 | } | 64 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java
| @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | @@ -6,9 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 6 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO; | 6 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO; |
| 7 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; | 7 | import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; |
| 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; | 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; |
| 9 | +import org.apache.ibatis.annotations.Delete; | ||
| 9 | import org.apache.ibatis.annotations.Mapper; | 10 | import org.apache.ibatis.annotations.Mapper; |
| 10 | import org.apache.ibatis.annotations.Param; | 11 | import org.apache.ibatis.annotations.Param; |
| 12 | +import org.apache.ibatis.annotations.Update; | ||
| 11 | 13 | ||
| 14 | +import java.util.Date; | ||
| 12 | import java.util.List; | 15 | import java.util.List; |
| 13 | 16 | ||
| 14 | /** | 17 | /** |
| @@ -43,8 +46,17 @@ public interface ApiErrorLogMapper extends BaseMapperX<ApiErrorLogDO> { | @@ -43,8 +46,17 @@ public interface ApiErrorLogMapper extends BaseMapperX<ApiErrorLogDO> { | ||
| 43 | ); | 46 | ); |
| 44 | } | 47 | } |
| 45 | 48 | ||
| 46 | - // TODO @j-sentinel:同 JobLogMapper 的一些优化点 | ||
| 47 | - Integer jobCleanErrorLog(@Param("errorLogJobDay") Integer errorLogJobDay); | 49 | + /** |
| 50 | + * 目前物理删除只能通过mybatis-plus的注解实现 or mybatis的xml实现 | ||
| 51 | + * 如果写xml的话就需要多写一个映射类 | ||
| 52 | + * | ||
| 53 | + * @param errorLogExceedDay 时间限制 | ||
| 54 | + * @param deleteLimit 删除次数的限制 | ||
| 55 | + * @return | ||
| 56 | + */ | ||
| 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); | ||
| 48 | 59 | ||
| 60 | + @Update("ALTER TABLE infra_api_error_log FORCE") | ||
| 49 | void optimizeTable(); | 61 | void optimizeTable(); |
| 50 | } | 62 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/AccessLogCleanRecordJob.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.job; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 4 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | ||
| 5 | +import cn.iocoder.yudao.module.infra.service.logger.ApiAccessLogService; | ||
| 6 | +import lombok.extern.slf4j.Slf4j; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | + | ||
| 9 | +import javax.annotation.Resource; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * 定时 物理 删除访问日志的 Job | ||
| 13 | + * | ||
| 14 | + * @author: j-sentinel | ||
| 15 | + */ | ||
| 16 | +@Slf4j | ||
| 17 | +@Component | ||
| 18 | +public class AccessLogCleanRecordJob implements JobHandler { | ||
| 19 | + | ||
| 20 | + @Resource | ||
| 21 | + private ApiAccessLogService apiAccessLogService; | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 清理超过(7)天的日志 | ||
| 25 | + */ | ||
| 26 | + private static final Integer JOB_CLEAN_RETAIN_DAY = 7; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 | ||
| 30 | + */ | ||
| 31 | + private static final Integer DELETE_LIMIT = 100; | ||
| 32 | + | ||
| 33 | + @Override | ||
| 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); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | +} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ErrorLogCleanRecordJob.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.job; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 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; | ||
| 7 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | +import org.springframework.stereotype.Component; | ||
| 9 | + | ||
| 10 | +import javax.annotation.Resource; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 定时 物理 删除错误日志的 Job | ||
| 14 | + * | ||
| 15 | + * @author: j-sentinel | ||
| 16 | + */ | ||
| 17 | +@Slf4j | ||
| 18 | +@Component | ||
| 19 | +public class ErrorLogCleanRecordJob implements JobHandler { | ||
| 20 | + | ||
| 21 | + @Resource | ||
| 22 | + private ApiErrorLogService apiErrorLogService; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * 清理超过(7)天的日志 | ||
| 26 | + */ | ||
| 27 | + private static final Integer JOB_CLEAN_RETAIN_DAY = 7; | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 | ||
| 31 | + */ | ||
| 32 | + private static final Integer DELETE_LIMIT = 100; | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + @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); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | +} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/JobCleanRecordJob.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.job; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | ||
| 4 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | ||
| 5 | +import cn.iocoder.yudao.module.infra.service.job.JobLogService; | ||
| 6 | +import lombok.extern.slf4j.Slf4j; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | +import javax.annotation.Resource; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 定时 物理 删除任务日志的 Job | ||
| 12 | + * | ||
| 13 | + * @author: j-sentinel | ||
| 14 | + */ | ||
| 15 | +@Slf4j | ||
| 16 | +@Component | ||
| 17 | +public class JobCleanRecordJob implements JobHandler { | ||
| 18 | + | ||
| 19 | + @Resource | ||
| 20 | + private JobLogService jobLogService; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 清理超过(14)天的日志 | ||
| 24 | + */ | ||
| 25 | + private static final Integer JOB_CLEAN_RETAIN_DAY = 14; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 每次删除间隔的条数,如果值太高可能会造成数据库的宕机 | ||
| 29 | + */ | ||
| 30 | + private static final Integer DELETE_LIMIT = 100; | ||
| 31 | + | ||
| 32 | + @Override | ||
| 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); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | +} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogService.java
| @@ -48,4 +48,11 @@ public interface JobLogService extends JobLogFrameworkService { | @@ -48,4 +48,11 @@ public interface JobLogService extends JobLogFrameworkService { | ||
| 48 | */ | 48 | */ |
| 49 | List<JobLogDO> getJobLogList(JobLogExportReqVO exportReqVO); | 49 | List<JobLogDO> getJobLogList(JobLogExportReqVO exportReqVO); |
| 50 | 50 | ||
| 51 | + /** | ||
| 52 | + * 清理 @param jobCleanRetainDay 天的访问日志 | ||
| 53 | + * | ||
| 54 | + * @param jobLogExceedDay 超过多少天就进行清理 | ||
| 55 | + * @param deleteLimit 清理的间隔条数 | ||
| 56 | + */ | ||
| 57 | + Integer timingJobCleanLog(Integer jobLogExceedDay,Integer deleteLimit); | ||
| 51 | } | 58 | } |
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; | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | ||
| 4 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO; | 6 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO; |
| 5 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO; | 7 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO; |
| 6 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; | 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO; |
| @@ -14,6 +16,7 @@ import org.springframework.validation.annotation.Validated; | @@ -14,6 +16,7 @@ import org.springframework.validation.annotation.Validated; | ||
| 14 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 15 | import java.time.LocalDateTime; | 17 | import java.time.LocalDateTime; |
| 16 | import java.util.Collection; | 18 | import java.util.Collection; |
| 19 | +import java.util.Date; | ||
| 17 | import java.util.List; | 20 | import java.util.List; |
| 18 | 21 | ||
| 19 | /** | 22 | /** |
| @@ -26,8 +29,6 @@ import java.util.List; | @@ -26,8 +29,6 @@ import java.util.List; | ||
| 26 | @Slf4j | 29 | @Slf4j |
| 27 | public class JobLogServiceImpl implements JobLogService { | 30 | public class JobLogServiceImpl implements JobLogService { |
| 28 | 31 | ||
| 29 | - private static final Integer DELETE_LIMIT = 1; | ||
| 30 | - | ||
| 31 | @Resource | 32 | @Resource |
| 32 | private JobLogMapper jobLogMapper; | 33 | private JobLogMapper jobLogMapper; |
| 33 | 34 | ||
| @@ -52,20 +53,25 @@ public class JobLogServiceImpl implements JobLogService { | @@ -52,20 +53,25 @@ public class JobLogServiceImpl implements JobLogService { | ||
| 52 | } | 53 | } |
| 53 | } | 54 | } |
| 54 | 55 | ||
| 55 | - // TODO @j-sentinel:这个 job,也可以忽略租户哈;可以直接使用 @TenantIgnore 注解; | ||
| 56 | @Override | 56 | @Override |
| 57 | - public Integer timingJobCleanLog(Integer jobCleanRetainDay) { | ||
| 58 | - Integer result = null; | 57 | + public Integer timingJobCleanLog(Integer jobLogExceedDay,Integer deleteLimit) { |
| 58 | + Integer result; | ||
| 59 | int count = 0; | 59 | int count = 0; |
| 60 | - // TODO @j-sentinel:一般我们在写逻辑时,尽量避免用 while true 这种“死循环”,而是 for (int i = 0; i < Short.MAX) 类似这种;避免里面真的发生一些意外的情况,无限执行; | ||
| 61 | - // 然后 for 里面,可以有个 if count < 100 未到达删除的预期条数,说明已经到底,可以 break 了; | ||
| 62 | - while (result == null || DELETE_LIMIT.equals(result)){ | ||
| 63 | - result = jobLogMapper.timingJobCleanLog(jobCleanRetainDay); | 60 | + Date currentDate = DateUtil.date(); |
| 61 | + // 计算过期日期:正数向未来偏移,负数向历史偏移 | ||
| 62 | + Date expireDate = DateUtil.offsetDay(currentDate, -jobLogExceedDay); | ||
| 63 | + for (int i = 0; i < Short.MAX_VALUE; i++) { | ||
| 64 | + result = jobLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); | ||
| 64 | count += result; | 65 | count += result; |
| 66 | + if (result < deleteLimit) { | ||
| 67 | + // 达到删除预期条数 | ||
| 68 | + break; | ||
| 69 | + } | ||
| 65 | } | 70 | } |
| 66 | if(count > 0){ | 71 | if(count > 0){ |
| 67 | // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 | 72 | // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 |
| 68 | // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 | 73 | // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 |
| 74 | + // 优化表语句官方文档:https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html | ||
| 69 | jobLogMapper.optimizeTable(); | 75 | jobLogMapper.optimizeTable(); |
| 70 | } | 76 | } |
| 71 | return count; | 77 | return count; |
| @@ -91,5 +97,4 @@ public class JobLogServiceImpl implements JobLogService { | @@ -91,5 +97,4 @@ public class JobLogServiceImpl implements JobLogService { | ||
| 91 | return jobLogMapper.selectList(exportReqVO); | 97 | return jobLogMapper.selectList(exportReqVO); |
| 92 | } | 98 | } |
| 93 | 99 | ||
| 94 | - // TODO @小吉祥:每天 0 点的时候,清理超过 14 天的日志; | ||
| 95 | } | 100 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java
| @@ -41,7 +41,8 @@ public interface ApiAccessLogService { | @@ -41,7 +41,8 @@ public interface ApiAccessLogService { | ||
| 41 | /** | 41 | /** |
| 42 | * 清理 @param accessLogJobDay 天的访问日志 | 42 | * 清理 @param accessLogJobDay 天的访问日志 |
| 43 | * | 43 | * |
| 44 | - * @param accessLogJobDay 超过多少天就进行清理 | 44 | + * @param accessLogExceedDay 超过多少天就进行清理 |
| 45 | + * @param deleteLimit 清理的间隔条数 | ||
| 45 | */ | 46 | */ |
| 46 | - void jobCleanAccessLog(Integer accessLogJobDay); | 47 | + Integer jobCleanAccessLog(Integer accessLogExceedDay,Integer deleteLimit); |
| 47 | } | 48 | } |
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; | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | 5 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 5 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; | 6 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; |
| @@ -13,6 +14,7 @@ import org.springframework.stereotype.Service; | @@ -13,6 +14,7 @@ import org.springframework.stereotype.Service; | ||
| 13 | import org.springframework.validation.annotation.Validated; | 14 | import org.springframework.validation.annotation.Validated; |
| 14 | 15 | ||
| 15 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 17 | +import java.util.Date; | ||
| 16 | import java.util.List; | 18 | import java.util.List; |
| 17 | 19 | ||
| 18 | /** | 20 | /** |
| @@ -25,8 +27,6 @@ import java.util.List; | @@ -25,8 +27,6 @@ import java.util.List; | ||
| 25 | @Validated | 27 | @Validated |
| 26 | public class ApiAccessLogServiceImpl implements ApiAccessLogService { | 28 | public class ApiAccessLogServiceImpl implements ApiAccessLogService { |
| 27 | 29 | ||
| 28 | - private static final Integer DELETE_LIMIT = 100; | ||
| 29 | - | ||
| 30 | @Resource | 30 | @Resource |
| 31 | private ApiAccessLogMapper apiAccessLogMapper; | 31 | private ApiAccessLogMapper apiAccessLogMapper; |
| 32 | 32 | ||
| @@ -47,22 +47,27 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { | @@ -47,22 +47,27 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { | ||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | @Override | 49 | @Override |
| 50 | - // TODO j-sentinel:类似 JobLogServiceImpl 的建议 | ||
| 51 | - public void jobCleanAccessLog(Integer accessLogJobDay) { | ||
| 52 | - TenantUtils.executeIgnore(() -> { | ||
| 53 | - Integer result = null; | ||
| 54 | - int count = 0; | ||
| 55 | - while (result == null || DELETE_LIMIT.equals(result)) { | ||
| 56 | - result = apiAccessLogMapper.jobCleanAccessLog(accessLogJobDay); | ||
| 57 | - count += result; | ||
| 58 | - } | ||
| 59 | - if (count > 0) { | ||
| 60 | - // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 | ||
| 61 | - // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 | ||
| 62 | - apiAccessLogMapper.optimizeTable(); | 50 | + public Integer jobCleanAccessLog(Integer accessLogExceedDay,Integer deleteLimit) { |
| 51 | + Integer result; | ||
| 52 | + int count = 0; | ||
| 53 | + Date currentDate = DateUtil.date(); | ||
| 54 | + // 计算过期日期:正数向未来偏移,负数向历史偏移 | ||
| 55 | + Date expireDate = DateUtil.offsetDay(currentDate, -accessLogExceedDay); | ||
| 56 | + for (int i = 0; i < Short.MAX_VALUE; i++) { | ||
| 57 | + result = apiAccessLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); | ||
| 58 | + count += result; | ||
| 59 | + if (result < deleteLimit) { | ||
| 60 | + // 达到删除预期条数 | ||
| 61 | + break; | ||
| 63 | } | 62 | } |
| 64 | - log.info("定时执行清理访问日志数量({})个", count); | ||
| 65 | - }); | 63 | + } |
| 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; | ||
| 66 | } | 71 | } |
| 67 | 72 | ||
| 68 | } | 73 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java
| @@ -50,7 +50,8 @@ public interface ApiErrorLogService { | @@ -50,7 +50,8 @@ public interface ApiErrorLogService { | ||
| 50 | /** | 50 | /** |
| 51 | * 清理 @param errorLogJobDay 天的访问日志 | 51 | * 清理 @param errorLogJobDay 天的访问日志 |
| 52 | * | 52 | * |
| 53 | - * @param errorLogJobDay 超过多少天就进行清理 | 53 | + * @param errorLogExceedDay 超过多少天就进行清理 |
| 54 | + * @param deleteLimit 清理的间隔条数 | ||
| 54 | */ | 55 | */ |
| 55 | - void jobCleanErrorLog(Integer errorLogJobDay); | 56 | + Integer jobCleanErrorLog(Integer errorLogExceedDay,Integer deleteLimit); |
| 56 | } | 57 | } |
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; | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | 5 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 5 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; | 6 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; |
| @@ -15,6 +16,7 @@ import org.springframework.validation.annotation.Validated; | @@ -15,6 +16,7 @@ import org.springframework.validation.annotation.Validated; | ||
| 15 | 16 | ||
| 16 | import javax.annotation.Resource; | 17 | import javax.annotation.Resource; |
| 17 | import java.time.LocalDateTime; | 18 | import java.time.LocalDateTime; |
| 19 | +import java.util.Date; | ||
| 18 | import java.util.List; | 20 | import java.util.List; |
| 19 | 21 | ||
| 20 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 22 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| @@ -31,8 +33,6 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_L | @@ -31,8 +33,6 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_L | ||
| 31 | @Validated | 33 | @Validated |
| 32 | public class ApiErrorLogServiceImpl implements ApiErrorLogService { | 34 | public class ApiErrorLogServiceImpl implements ApiErrorLogService { |
| 33 | 35 | ||
| 34 | - private static final Integer DELETE_LIMIT = 100; | ||
| 35 | - | ||
| 36 | @Resource | 36 | @Resource |
| 37 | private ApiErrorLogMapper apiErrorLogMapper; | 37 | private ApiErrorLogMapper apiErrorLogMapper; |
| 38 | 38 | ||
| @@ -68,22 +68,27 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { | @@ -68,22 +68,27 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { | ||
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | @Override | 70 | @Override |
| 71 | - // TODO j-sentinel:类似 JobLogServiceImpl 的建议 | ||
| 72 | - public void jobCleanErrorLog(Integer errorLogJobDay) { | ||
| 73 | - TenantUtils.executeIgnore(() -> { | ||
| 74 | - Integer result = null; | ||
| 75 | - int count = 0; | ||
| 76 | - while (result == null || DELETE_LIMIT.equals(result)) { | ||
| 77 | - result = apiErrorLogMapper.jobCleanErrorLog(errorLogJobDay); | ||
| 78 | - count += result; | ||
| 79 | - } | ||
| 80 | - if (count > 0) { | ||
| 81 | - // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。 | ||
| 82 | - // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。 | ||
| 83 | - apiErrorLogMapper.optimizeTable(); | 71 | + public Integer jobCleanErrorLog(Integer errorLogExceedDay,Integer deleteLimit) { |
| 72 | + Integer result; | ||
| 73 | + int count = 0; | ||
| 74 | + Date currentDate = DateUtil.date(); | ||
| 75 | + // 计算过期日期:正数向未来偏移,负数向历史偏移 | ||
| 76 | + Date expireDate = DateUtil.offsetDay(currentDate, -errorLogExceedDay); | ||
| 77 | + for (int i = 0; i < Short.MAX_VALUE; i++) { | ||
| 78 | + result = apiErrorLogMapper.deleteByCreateTimeLt(expireDate,deleteLimit); | ||
| 79 | + count += result; | ||
| 80 | + if (result < deleteLimit) { | ||
| 81 | + // 达到删除预期条数 | ||
| 82 | + break; | ||
| 84 | } | 83 | } |
| 85 | - log.info("定时执行清理错误日志数量({})个",count); | ||
| 86 | - }); | 84 | + } |
| 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; | ||
| 87 | } | 92 | } |
| 88 | 93 | ||
| 89 | } | 94 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiAccessLogMapper.xml deleted
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| 3 | -<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiAccessLogMapper"> | ||
| 4 | - | ||
| 5 | - <!-- | ||
| 6 | - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||
| 7 | - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||
| 8 | - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||
| 9 | - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||
| 10 | - --> | ||
| 11 | - | ||
| 12 | - <delete id="jobCleanAccessLog" parameterType="integer"> | ||
| 13 | - DELETE FROM infra_api_access_log | ||
| 14 | - WHERE id IN ( | ||
| 15 | - SELECT * FROM ( | ||
| 16 | - SELECT id FROM infra_api_access_log | ||
| 17 | - WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{accessLogJobDay} DAY) | ||
| 18 | - ORDER BY id ASC LIMIT 100 | ||
| 19 | - ) AS a | ||
| 20 | - ) | ||
| 21 | - </delete> | ||
| 22 | - | ||
| 23 | - <select id="optimizeTable"> | ||
| 24 | - ALTER TABLE infra_job_log FORCE | ||
| 25 | - /* | ||
| 26 | - ALTER TABLE infra_job_log ENGINE = INNODB, | ||
| 27 | - ALGORITHM = INPLACE, | ||
| 28 | - LOCK = NONE | ||
| 29 | - */ | ||
| 30 | - </select> | ||
| 31 | - | ||
| 32 | -</mapper> |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiErrorLogMapper.xml deleted
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| 3 | -<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper"> | ||
| 4 | - | ||
| 5 | - <!-- | ||
| 6 | - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||
| 7 | - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||
| 8 | - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||
| 9 | - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||
| 10 | - --> | ||
| 11 | - | ||
| 12 | - <delete id="jobCleanErrorLog" parameterType="integer"> | ||
| 13 | - DELETE FROM infra_api_error_log | ||
| 14 | - WHERE id IN ( | ||
| 15 | - SELECT * FROM ( | ||
| 16 | - SELECT id FROM infra_api_error_log | ||
| 17 | - WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{errorLogJobDay} DAY) | ||
| 18 | - ORDER BY id ASC LIMIT 100 | ||
| 19 | - ) AS a | ||
| 20 | - ) | ||
| 21 | - </delete> | ||
| 22 | - | ||
| 23 | - <select id="optimizeTable"> | ||
| 24 | - ALTER TABLE infra_api_error_log FORCE | ||
| 25 | - /* | ||
| 26 | - ALTER TABLE infra_api_error_log ENGINE = INNODB, | ||
| 27 | - ALGORITHM = INPLACE, | ||
| 28 | - LOCK = NONE | ||
| 29 | - */ | ||
| 30 | - </select> | ||
| 31 | - | ||
| 32 | -</mapper> |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/JobLogMapper.xml deleted
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| 3 | -<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper"> | ||
| 4 | - | ||
| 5 | - <!-- | ||
| 6 | - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||
| 7 | - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||
| 8 | - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||
| 9 | - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||
| 10 | - --> | ||
| 11 | - | ||
| 12 | - <delete id="timingJobCleanLog" parameterType="integer"> | ||
| 13 | - DELETE FROM infra_job_log | ||
| 14 | - WHERE id IN ( | ||
| 15 | - SELECT * FROM ( | ||
| 16 | - SELECT id FROM infra_job_log | ||
| 17 | - WHERE create_time < DATE_SUB(CURDATE(), INTERVAL #{jobCleanRetainDay} DAY) | ||
| 18 | - ORDER BY id ASC LIMIT 100 | ||
| 19 | - ) AS a | ||
| 20 | - ) | ||
| 21 | - </delete> | ||
| 22 | - | ||
| 23 | - <select id="optimizeTable"> | ||
| 24 | - ALTER TABLE infra_job_log FORCE | ||
| 25 | - /* | ||
| 26 | - ALTER TABLE infra_job_log ENGINE = INNODB, | ||
| 27 | - ALGORITHM = INPLACE, | ||
| 28 | - LOCK = NONE | ||
| 29 | - */ | ||
| 30 | - </select> | ||
| 31 | - | ||
| 32 | -</mapper> |
yudao-server/src/main/resources/application-local.yaml
| @@ -213,10 +213,6 @@ yudao: | @@ -213,10 +213,6 @@ yudao: | ||
| 213 | error-code: # 错误码相关配置项 | 213 | error-code: # 错误码相关配置项 |
| 214 | enable: false | 214 | enable: false |
| 215 | demo: false # 关闭演示模式 | 215 | demo: false # 关闭演示模式 |
| 216 | - clean-job: | ||
| 217 | - access-retain-day: 7 | ||
| 218 | - error-retain-day: 8 | ||
| 219 | - job-clean-retain-day: 8 | ||
| 220 | 216 | ||
| 221 | justauth: | 217 | justauth: |
| 222 | enabled: true | 218 | enabled: true |