|
@@ -12,6 +12,7 @@ import com.kym.service.FaultRecordService;
|
|
|
import com.kym.service.FaultSubscriberService;
|
|
import com.kym.service.FaultSubscriberService;
|
|
|
import com.kym.service.MpMsgTemplateService;
|
|
import com.kym.service.MpMsgTemplateService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -33,6 +34,9 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
private static final Duration ANTI_DUP_TTL = Duration.ofHours(2);
|
|
private static final Duration ANTI_DUP_TTL = Duration.ofHours(2);
|
|
|
|
|
|
|
|
|
|
+ @Value("${fault.notify-delay:3}")
|
|
|
|
|
+ private int faultNotifyDelayMinutes;
|
|
|
|
|
+
|
|
|
private final FaultRecordService faultRecordService;
|
|
private final FaultRecordService faultRecordService;
|
|
|
private final FaultSubscriberService faultSubscriberService;
|
|
private final FaultSubscriberService faultSubscriberService;
|
|
|
private final MpMsgTemplateService mpMsgTemplateService;
|
|
private final MpMsgTemplateService mpMsgTemplateService;
|
|
@@ -81,8 +85,18 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
.eq(FaultRecord::getIsRecovered, FaultRecord.IS_RECOVERED_未恢复)
|
|
.eq(FaultRecord::getIsRecovered, FaultRecord.IS_RECOVERED_未恢复)
|
|
|
.list();
|
|
.list();
|
|
|
|
|
|
|
|
|
|
+ var now = LocalDateTime.now();
|
|
|
|
|
+ var delayThreshold = now.minusMinutes(faultNotifyDelayMinutes);
|
|
|
|
|
+
|
|
|
for (var record : unnotifiedList) {
|
|
for (var record : unnotifiedList) {
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 延迟未到,暂不通知
|
|
|
|
|
+ if (record.getFaultTime() != null && record.getFaultTime().isAfter(delayThreshold)) {
|
|
|
|
|
+ log.debug("故障通知延迟未到,跳过: recordId={}, faultTime={}, delayMinutes={}",
|
|
|
|
|
+ record.getId(), record.getFaultTime(), faultNotifyDelayMinutes);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
var redisKey = buildAntiDupKey(record.getFaultType(), record.getDeviceName(), record.getStationId());
|
|
var redisKey = buildAntiDupKey(record.getFaultType(), record.getDeviceName(), record.getStationId());
|
|
|
if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey))) {
|
|
if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey))) {
|
|
|
faultRecordService.markNotified(record.getId());
|
|
faultRecordService.markNotified(record.getId());
|
|
@@ -114,13 +128,8 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
.setIsRecovered(FaultRecord.IS_RECOVERED_未恢复);
|
|
.setIsRecovered(FaultRecord.IS_RECOVERED_未恢复);
|
|
|
faultRecordService.save(record);
|
|
faultRecordService.save(record);
|
|
|
|
|
|
|
|
- var redisKey = buildAntiDupKey(faultType.getCode(), deviceName, stationId);
|
|
|
|
|
- if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey))) {
|
|
|
|
|
- log.debug("防重复key存在,跳过通知: {}", redisKey);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- doNotify(record);
|
|
|
|
|
|
|
+ log.info("故障已记录,延迟{}分钟后发送通知: stationId={}, deviceName={}, faultType={}",
|
|
|
|
|
+ faultNotifyDelayMinutes, stationId, deviceName, faultType.getCode());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void handleRecovery(String stationId, String deviceName, FaultType faultType) {
|
|
private void handleRecovery(String stationId, String deviceName, FaultType faultType) {
|
|
@@ -130,9 +139,17 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ boolean wasNotified = record.getIsNotified() != null && record.getIsNotified() == FaultRecord.IS_NOTIFIED_已通知;
|
|
|
|
|
+
|
|
|
faultRecordService.markRecovered(record.getId());
|
|
faultRecordService.markRecovered(record.getId());
|
|
|
stringRedisTemplate.delete(buildAntiDupKey(faultType.getCode(), deviceName, stationId));
|
|
stringRedisTemplate.delete(buildAntiDupKey(faultType.getCode(), deviceName, stationId));
|
|
|
- sendRecoveryNotification(record);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (wasNotified) {
|
|
|
|
|
+ sendRecoveryNotification(record);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("故障在通知延迟内自动恢复,跳过通知: stationId={}, deviceName={}, faultType={}",
|
|
|
|
|
+ stationId, deviceName, faultType.getCode());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void doNotify(FaultRecord record) {
|
|
private void doNotify(FaultRecord record) {
|