code review:访问日志、错误日志的清理 Job 实现
This commit is contained in:
parent
d4417d2474
commit
f8b4a08fae
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -62,11 +62,11 @@
|
|||
</dependency>
|
||||
|
||||
<!-- Job 定时任务相关 -->
|
||||
<!-- TODO @j-sentinel:去掉这个依赖哈; -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-job</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -41,7 +41,12 @@ public interface JobLogMapper extends BaseMapperX<JobLogDO> {
|
|||
);
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ public interface ApiAccessLogMapper extends BaseMapperX<ApiAccessLogDO> {
|
|||
);
|
||||
}
|
||||
|
||||
// TODO @j-sentinel:同 JobLogMapper 的一些优化点
|
||||
Integer jobCleanAccessLog(@Param("accessLogJobDay") Integer accessLogJobDay);
|
||||
|
||||
void optimizeTable();
|
||||
|
|
|
@ -43,6 +43,7 @@ public interface ApiErrorLogMapper extends BaseMapperX<ApiErrorLogDO> {
|
|||
);
|
||||
}
|
||||
|
||||
// TODO @j-sentinel:同 JobLogMapper 的一些优化点
|
||||
Integer jobCleanErrorLog(@Param("errorLogJobDay") Integer errorLogJobDay);
|
||||
|
||||
void optimizeTable();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -47,6 +47,7 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService {
|
|||
}
|
||||
|
||||
@Override
|
||||
// TODO j-sentinel:类似 JobLogServiceImpl 的建议
|
||||
public void jobCleanAccessLog(Integer accessLogJobDay) {
|
||||
TenantUtils.executeIgnore(() -> {
|
||||
Integer result = null;
|
||||
|
|
|
@ -68,6 +68,7 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
|
|||
}
|
||||
|
||||
@Override
|
||||
// TODO j-sentinel:类似 JobLogServiceImpl 的建议
|
||||
public void jobCleanErrorLog(Integer errorLogJobDay) {
|
||||
TenantUtils.executeIgnore(() -> {
|
||||
Integer result = null;
|
||||
|
|
Loading…
Reference in New Issue