解决重复发送邮件问题

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,11 +55,17 @@ 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
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Submit each container to the thread pool for processing
for (Container container : containers) {
executor.submit(() -> {
try { try {
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
@@ -81,6 +88,7 @@ public class CheckInstancesStatus {
} catch (Exception exception) { } catch (Exception exception) {
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
containerService.saveOrUpdate(container);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
@@ -109,6 +117,11 @@ public class CheckInstancesStatus {
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);
//检测虚拟机在线状态 //检测虚拟机在线状态
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,19 +632,28 @@ 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
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Submit each container to the thread pool for processing
for (Container container : containers) {
executor.submit(() -> {
try { try {
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
containerService.saveOrUpdate(container);
return; return;
} }
//通过Socket(IP+端口)判断是否在线 //通过Socket(IP+端口)判断是否在线
if (StringUtils.hasText(container.getServerAddress())) { if (StringUtils.hasText(container.getServerAddress())) {
if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
containerService.saveOrUpdate(container);
return; return;
} }
} }
@@ -652,9 +661,11 @@ class NoodlesApplicationTests {
HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true);
log.info("[实例状态检测]:与{}建立连接成功", container.getName()); log.info("[实例状态检测]:与{}建立连接成功", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
containerService.saveOrUpdate(container);
} catch (Exception exception) { } catch (Exception exception) {
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
containerService.saveOrUpdate(container);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
@@ -684,4 +695,26 @@ class NoodlesApplicationTests {
} }
}); });
} }
// 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);
}
} }