解决重复发送邮件问题

This commit is contained in:
2023-03-23 14:35:38 +08:00
parent e0f1d27409
commit d274bb1d7e
2 changed files with 119 additions and 72 deletions
@@ -12,6 +12,7 @@ import com.mzaxd.noodles.service.*;
import com.mzaxd.noodles.util.CpuUtil; import com.mzaxd.noodles.util.CpuUtil;
import com.mzaxd.noodles.util.RedisCache; import com.mzaxd.noodles.util.RedisCache;
import com.mzaxd.noodles.util.UrlUtil; import com.mzaxd.noodles.util.UrlUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@@ -54,61 +55,73 @@ public class CheckInstancesStatus {
/** /**
* 检查实例状态并且发送对应的提醒 * 检查实例状态并且发送对应的提醒
*/ */
@SneakyThrows
@Scheduled(cron = "* 0/1 * * * ?") @Scheduled(cron = "* 0/1 * * * ?")
public void checkInstancesStatus() { public void checkInstancesStatus() {
List<Container> containers = containerService.list(); List<Container> containers = containerService.list();
containers.forEach(container -> { // Create a thread pool with a fixed number of threads
try { ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); // Submit each container to the thread pool for processing
containerService.saveOrUpdate(container); for (Container container : containers) {
return; executor.submit(() -> {
} try {
//通过Socket(IP+端口)判断是否在线 if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
if (StringUtils.hasText(container.getServerAddress())) { container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
containerService.saveOrUpdate(container); containerService.saveOrUpdate(container);
return; return;
} }
} //通过Socket(IP+端口)判断是否在线
//通过HTTP请求判断是否在线 if (StringUtils.hasText(container.getServerAddress())) {
HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
log.info("[实例状态检测]:与{}建立连接成功", container.getName()); container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); containerService.saveOrUpdate(container);
containerService.saveOrUpdate(container); return;
} catch (Exception exception) { }
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); }
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); //通过HTTP请求判断是否在线
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true);
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); log.info("[实例状态检测]:与{}建立连接成功", container.getName());
if (CollectionUtils.isEmpty(set)) { container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
set = new HashSet<>(); containerService.saveOrUpdate(container);
} } catch (Exception exception) {
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName());
if (set.contains(container.getId().toString())) { container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
return; containerService.saveOrUpdate(container);
} else { // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
// 根据实例对应的提醒方式进行提醒 Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
if (set.contains(container.getId().toString())) {
return; return;
} else {
// 根据实例对应的提醒方式进行提醒
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return;
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
notificationService.sendContainerOfflineNotification(container.getId());
set.add(container.getId().toString());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
notificationService.sendContainerOfflineEmail(container.getId());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(container.getId());
set.add(container.getId().toString());
}
} }
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) { redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set);
notificationService.sendContainerOfflineNotification(container.getId());
set.add(container.getId().toString());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
notificationService.sendContainerOfflineEmail(container.getId());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(container.getId());
set.add(container.getId().toString());
}
} }
redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set); });
} }
});
// Shutdown the thread pool and wait for all tasks to complete
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
//检测虚拟机在线状态 //检测虚拟机在线状态
LambdaQueryWrapper<HostMachine> vmWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<HostMachine> vmWrapper = new LambdaQueryWrapper<>();
@@ -138,6 +151,7 @@ public class CheckInstancesStatus {
} catch (Exception exception) { } catch (Exception exception) {
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", vm.getName()); log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", vm.getName());
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
hostMachineService.saveOrUpdate(vm);
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS);
if (CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
@@ -632,34 +632,45 @@ class NoodlesApplicationTests {
@Test @Test
public void containerCheck() { public void containerCheck() throws InterruptedException {
long startTime = System.nanoTime(); // 记录开始时间
List<Container> containers = containerService.list(); List<Container> containers = containerService.list();
containers.forEach(container -> { // Create a thread pool with a fixed number of threads
try { ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); // Submit each container to the thread pool for processing
return; for (Container container : containers) {
} executor.submit(() -> {
//通过Socket(IP+端口)判断是否在线 try {
if (StringUtils.hasText(container.getServerAddress())) { if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); containerService.saveOrUpdate(container);
return; return;
} }
} //通过Socket(IP+端口)判断是否在线
//通过HTTP请求判断是否在线 if (StringUtils.hasText(container.getServerAddress())) {
HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
log.info("[实例状态检测]:与{}建立连接成功", container.getName()); container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); containerService.saveOrUpdate(container);
} catch (Exception exception) { return;
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); }
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); }
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 //通过HTTP请求判断是否在线
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true);
if (CollectionUtils.isEmpty(set)) { log.info("[实例状态检测]:与{}建立连接成功", container.getName());
set = new HashSet<>(); container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
} containerService.saveOrUpdate(container);
} catch (Exception exception) {
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
containerService.saveOrUpdate(container);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
if (set.contains(container.getId().toString())) { if (set.contains(container.getId().toString())) {
return; return;
@@ -680,8 +691,30 @@ class NoodlesApplicationTests {
set.add(container.getId().toString()); set.add(container.getId().toString());
} }
} }
redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set); redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set);
} }
}); });
}
// Shutdown the thread pool and wait for all tasks to complete
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
long endTime = System.nanoTime(); // 记录结束时间
long elapsedTime = endTime - startTime;
double seconds = (double) elapsedTime / 1_000_000_000.0; // 将纳秒转换为秒
System.out.println("代码执行时间:" + seconds + "");
}
@Test
public void testRedis() {
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
System.out.println(set);
set.add("1");
redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set);
} }
} }