code review:访问日志、错误日志的清理 Job 实现

This commit is contained in:
YunaiV
2023-10-02 00:06:28 +08:00
parent d4417d2474
commit f8b4a08fae
14 changed files with 23 additions and 1 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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-serntineloptimize table infra_job_log 就可以啦?
void optimizeTable();
}

View File

@ -45,6 +45,7 @@ public interface ApiAccessLogMapper extends BaseMapperX<ApiAccessLogDO> {
);
}
// TODO @j-sentinel同 JobLogMapper 的一些优化点
Integer jobCleanAccessLog(@Param("accessLogJobDay") Integer accessLogJobDay);
void optimizeTable();

View File

@ -43,6 +43,7 @@ public interface ApiErrorLogMapper extends BaseMapperX<ApiErrorLogDO> {
);
}
// TODO @j-sentinel同 JobLogMapper 的一些优化点
Integer jobCleanErrorLog(@Param("errorLogJobDay") Integer errorLogJobDay);
void optimizeTable();

View File

@ -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;

View File

@ -47,6 +47,7 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService {
}
@Override
// TODO j-sentinel类似 JobLogServiceImpl 的建议
public void jobCleanAccessLog(Integer accessLogJobDay) {
TenantUtils.executeIgnore(() -> {
Integer result = null;

View File

@ -68,6 +68,7 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
}
@Override
// TODO j-sentinel类似 JobLogServiceImpl 的建议
public void jobCleanErrorLog(Integer errorLogJobDay) {
TenantUtils.executeIgnore(() -> {
Integer result = null;