From f8b4a08faef7f2f82a6b8899ec889a2caa49d870 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 2 Oct 2023 00:06:28 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=EF=BC=9A=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E3=80=81=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9A=84=E6=B8=85=E7=90=86=20Job=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quartz/config/YudaoQuartzAutoConfiguration.java | 1 + .../yudao/framework/quartz/core/job/JobLogJobHandler.java | 2 ++ .../yudao/framework/quartz/core/job/LogJobProperties.java | 1 + yudao-framework/yudao-spring-boot-starter-web/pom.xml | 2 +- .../framework/apilog/core/job/ApiAccessLogJobHandler.java | 1 + .../framework/apilog/core/job/ApiErrorLogJobHandler.java | 1 + .../yudao/module/infra/api/logger/ApiAccessLogApi.java | 2 ++ .../yudao/module/infra/api/logger/ApiErrorLogApi.java | 2 ++ .../yudao/module/infra/dal/mysql/job/JobLogMapper.java | 5 +++++ .../module/infra/dal/mysql/logger/ApiAccessLogMapper.java | 1 + .../module/infra/dal/mysql/logger/ApiErrorLogMapper.java | 1 + .../yudao/module/infra/service/job/JobLogServiceImpl.java | 3 +++ .../module/infra/service/logger/ApiAccessLogServiceImpl.java | 1 + .../module/infra/service/logger/ApiErrorLogServiceImpl.java | 1 + 14 files changed, 23 insertions(+), 1 deletion(-) 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 0f82712a2..ed2a80b41 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 @@ -30,6 +30,7 @@ public class YudaoQuartzAutoConfiguration { return new SchedulerManager(scheduler.get()); } + // TODO @j-sentinel:这个 job,先拿到 infra biz 里面实现哈; @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 index 53565102d..b2c2d5799 100644 --- 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 @@ -7,7 +7,9 @@ import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; +// TODO @j-sentinel:名字和项目里其它保持统一,可以叫 JobLogCleanJob,不用带 handler 哈 /** + * // TODO @j-sentinel:要写下类注释噢,就是这个类要干啥;然后下面两个应该是 @author 和 @since * @Author: j-sentinel * @Date: 2023/9/30 20:40 */ 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 index f563fec19..f031f051d 100644 --- 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 @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.quartz.core.job; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +// TODO @j-sentinel:这个配置类,先暂时不做,每个 Job 里面定一个静态类。其实不是所有的变量,都需要配置化,因为它本身基本也不会改动。 /** * @Author: j-sentinel * @Date: 2023/9/30 16:17 diff --git a/yudao-framework/yudao-spring-boot-starter-web/pom.xml b/yudao-framework/yudao-spring-boot-starter-web/pom.xml index b345fb31e..6b52c5b62 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-web/pom.xml @@ -62,11 +62,11 @@ + 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/core/job/ApiAccessLogJobHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java index f6d512fa0..ae4c5155c 100644 --- 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 @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; +// TODO @j-sentinel:同 JobLogJobHandler /** * @Author: j-sentinel * @Date: 2023/9/30 16:13 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 index adbfa45b6..77c97190d 100644 --- 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 @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; +// TODO @j-sentinel:同 JobLogJobHandler /** * @Author: j-sentinel * @Date: 2023/9/30 16:13 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 63a1f3899..7e76e22ea 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,10 +18,12 @@ public interface ApiAccessLogApi { */ void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO); + // TODO @j-sentinel:这个我们先提供接口在 API,而是 infra 模块自己清理先哈; /** * 清理 @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 7554461be..25ca974f0 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,10 +18,12 @@ public interface ApiErrorLogApi { */ void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO); + // TODO @j-sentinel:这个我们先提供接口在 API,而是 infra 模块自己清理先哈; /** * 清理 @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/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 377136a82..6f46b8288 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 @@ -41,7 +41,12 @@ public interface JobLogMapper extends BaseMapperX { ); } + // TODO @j-sentinel:一般来说,我们 mapper 只提供 crud 的操作,所以这个方法的命名,建议是 deleteByCreateTimeLt; + // 然后,参数是 (crateTime, count),由 service 传入 + // 这里为什么有 lt 呢,这个其实是延续 spring data 的 method 描述的命名习惯,lt 表示小于; + // 另外,timingJobCleanLog 的具体 sql 这么写,性能是比较差的;可以直接 delete * from job_log where create_time < xxx order by id limit 100; Integer timingJobCleanLog(@Param("jobCleanRetainDay") Integer jobCleanRetainDay); + // TODO @j-serntinel:optimize table infra_job_log 就可以啦? 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 dd3107407..b18583b56 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 @@ -45,6 +45,7 @@ public interface ApiAccessLogMapper extends BaseMapperX { ); } + // TODO @j-sentinel:同 JobLogMapper 的一些优化点 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 617247d7d..b62b7aa47 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 @@ -43,6 +43,7 @@ public interface ApiErrorLogMapper extends BaseMapperX { ); } + // TODO @j-sentinel:同 JobLogMapper 的一些优化点 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 353860bd5..1be62f321 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 @@ -52,10 +52,13 @@ public class JobLogServiceImpl implements JobLogService { } } + // TODO @j-sentinel:这个 job,也可以忽略租户哈;可以直接使用 @TenantIgnore 注解; @Override public Integer timingJobCleanLog(Integer jobCleanRetainDay) { Integer result = null; int count = 0; + // TODO @j-sentinel:一般我们在写逻辑时,尽量避免用 while true 这种“死循环”,而是 for (int i = 0; i < Short.MAX) 类似这种;避免里面真的发生一些意外的情况,无限执行; + // 然后 for 里面,可以有个 if count < 100 未到达删除的预期条数,说明已经到底,可以 break 了; while (result == null || DELETE_LIMIT.equals(result)){ result = jobLogMapper.timingJobCleanLog(jobCleanRetainDay); count += result; 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 8863aea05..b327ee4f6 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 @@ -47,6 +47,7 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { } @Override + // TODO j-sentinel:类似 JobLogServiceImpl 的建议 public void jobCleanAccessLog(Integer accessLogJobDay) { TenantUtils.executeIgnore(() -> { Integer result = null; 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 fc74c7112..1dc10fdf3 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 @@ -68,6 +68,7 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { } @Override + // TODO j-sentinel:类似 JobLogServiceImpl 的建议 public void jobCleanErrorLog(Integer errorLogJobDay) { TenantUtils.executeIgnore(() -> { Integer result = null;