优化重试日志

This commit is contained in:
byteblogs168 2023-01-14 19:56:43 +08:00
parent 3c47d71dde
commit 7f4af6c307
5 changed files with 38 additions and 19 deletions

View File

@ -49,7 +49,6 @@ public class ExistsTransactionalRetryServiceTest {
log.error("", e);
}
// await().atLeast(1, TimeUnit.MINUTES);
Thread.sleep(90000);
}

View File

@ -90,7 +90,12 @@ public class ExecUnitActor extends AbstractActor {
}catch (Exception e) {
LogUtils.error("回调客户端失败 retryTask:[{}]", JsonUtil.toJsonString(retryTask), e);
retryTaskLog.setErrorMessage(e.getMessage());
retryTaskLog.setErrorMessage(StringUtils.isBlank(e.getMessage()) ? StringUtils.EMPTY : e.getMessage());
} finally {
// 清除幂等标识位
idempotentStrategy.clear(retryTask.getGroupName(), retryTask.getId().intValue());
getContext().stop(getSelf());
// 记录重试日志
BeanUtils.copyProperties(retryTask, retryTaskLog);
@ -98,11 +103,6 @@ public class ExecUnitActor extends AbstractActor {
retryTaskLog.setId(null);
Assert.isTrue(1 == retryTaskLogMapper.insert(retryTaskLog),
new XRetryServerException("新增重试日志失败"));
} finally {
// 清除幂等标识位
idempotentStrategy.clear(retryTask.getGroupName(), retryTask.getId().intValue());
getContext().stop(getSelf());
}

View File

@ -2,6 +2,7 @@ package com.x.retry.server.support.dispatch.actor.result;
import akka.actor.AbstractActor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.x.retry.common.core.enums.RetryStatusEnum;
import com.x.retry.common.core.log.LogUtils;
import com.x.retry.common.core.util.Assert;
@ -19,8 +20,10 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
/**
* 重试完成执行器
@ -69,11 +72,19 @@ public class FailureActor extends AbstractActor {
getContext().stop(getSelf());
// 记录重试日志
RetryTaskLog retryTaskLog = new RetryTaskLog();
retryTaskLog.setRetryStatus(retryTask.getRetryStatus());
Assert.isTrue(1 == retryTaskLogMapper.update(retryTaskLog,
new LambdaQueryWrapper<RetryTaskLog>().eq(RetryTaskLog::getBizId, retryTask.getBizId())),
new XRetryServerException("更新重试日志失败"));
PageDTO<RetryTaskLog> retryTaskLogPageDTO = retryTaskLogMapper.selectPage(new PageDTO<>(1, 1),
new LambdaQueryWrapper<RetryTaskLog>()
.eq(RetryTaskLog::getBizId, retryTask.getBizId())
.orderByDesc(RetryTaskLog::getId));
List<RetryTaskLog> records = retryTaskLogPageDTO.getRecords();
if (!CollectionUtils.isEmpty(records)) {
RetryTaskLog retryTaskLog = records.get(0);
retryTaskLog.setRetryStatus(retryTask.getRetryStatus());
Assert.isTrue(1 == retryTaskLogMapper.updateById(retryTaskLog),
new XRetryServerException("更新重试日志失败"));
}
}
}).build();

View File

@ -2,6 +2,7 @@ package com.x.retry.server.support.dispatch.actor.result;
import akka.actor.AbstractActor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.x.retry.common.core.enums.RetryStatusEnum;
import com.x.retry.common.core.log.LogUtils;
import com.x.retry.common.core.util.Assert;
@ -17,8 +18,10 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
/**
* 重试完成执行器
@ -49,7 +52,6 @@ public class FinishActor extends AbstractActor {
retryTask.setRetryStatus(RetryStatusEnum.FINISH.getLevel());
try {
retryTaskAccess.updateRetryTask(retryTask);
}catch (Exception e) {
@ -59,11 +61,18 @@ public class FinishActor extends AbstractActor {
getContext().stop(getSelf());
// 记录重试日志
RetryTaskLog retryTaskLog = new RetryTaskLog();
retryTaskLog.setRetryStatus(retryTask.getRetryStatus());
Assert.isTrue(1 == retryTaskLogMapper.update(retryTaskLog,
new LambdaQueryWrapper<RetryTaskLog>().eq(RetryTaskLog::getBizId, retryTask.getBizId())),
new XRetryServerException("更新重试日志失败"));
PageDTO<RetryTaskLog> retryTaskLogPageDTO = retryTaskLogMapper.selectPage(new PageDTO<>(1, 1),
new LambdaQueryWrapper<RetryTaskLog>()
.eq(RetryTaskLog::getBizId, retryTask.getBizId())
.orderByDesc(RetryTaskLog::getId));
List<RetryTaskLog> records = retryTaskLogPageDTO.getRecords();
if (!CollectionUtils.isEmpty(records)) {
RetryTaskLog retryTaskLog = records.get(0);
retryTaskLog.setRetryStatus(retryTask.getRetryStatus());
Assert.isTrue(1 == retryTaskLogMapper.updateById(retryTaskLog),
new XRetryServerException("更新重试日志失败"));
}
}

View File

@ -59,7 +59,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
if (loginRequired.required()) {
// 执行认证
if (token == null) {
throw new XRetryServerException("无效token");
throw new XRetryServerException("登陆过期,请重新登陆");
}
// 获取 token 中的 user id