From d274bb1d7eb885226d84db10ef16c89a9eb23086 Mon Sep 17 00:00:00 2001 From: Mzaxd Date: Thu, 23 Mar 2023 14:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=87=8D=E5=A4=8D=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=82=AE=E4=BB=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../noodles/job/CheckInstancesStatus.java | 104 ++++++++++-------- .../noodles/NoodlesApplicationTests.java | 87 ++++++++++----- 2 files changed, 119 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java b/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java index 43ba89d..65f8ec4 100644 --- a/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java +++ b/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java @@ -12,6 +12,7 @@ import com.mzaxd.noodles.service.*; import com.mzaxd.noodles.util.CpuUtil; import com.mzaxd.noodles.util.RedisCache; import com.mzaxd.noodles.util.UrlUtil; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; @@ -54,61 +55,73 @@ public class CheckInstancesStatus { /** * 检查实例状态并且发送对应的提醒 */ + @SneakyThrows @Scheduled(cron = "* 0/1 * * * ?") public void checkInstancesStatus() { List containers = containerService.list(); - containers.forEach(container -> { - try { - if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { - container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); - containerService.saveOrUpdate(container); - return; - } - //通过Socket(IP+端口)判断是否在线 - if (StringUtils.hasText(container.getServerAddress())) { - if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { - container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + // 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 { + if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { + container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); containerService.saveOrUpdate(container); return; } - } - //通过HTTP请求判断是否在线 - HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); - log.info("[实例状态检测]:与{}建立连接成功", container.getName()); - container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); - containerService.saveOrUpdate(container); - } catch (Exception exception) { - log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); - container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); - // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 - Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); - if (CollectionUtils.isEmpty(set)) { - set = new HashSet<>(); - } - // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 - if (set.contains(container.getId().toString())) { - return; - } else { - // 根据实例对应的提醒方式进行提醒 - if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { + //通过Socket(IP+端口)判断是否在线 + if (StringUtils.hasText(container.getServerAddress())) { + if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { + container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + containerService.saveOrUpdate(container); + return; + } + } + //通过HTTP请求判断是否在线 + HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); + log.info("[实例状态检测]:与{}建立连接成功", container.getName()); + 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 set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } + // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 + if (set.contains(container.getId().toString())) { 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)) { - 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); } - 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 vmWrapper = new LambdaQueryWrapper<>(); @@ -138,6 +151,7 @@ public class CheckInstancesStatus { } catch (Exception exception) { log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", vm.getName()); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); + hostMachineService.saveOrUpdate(vm); //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS); if (CollectionUtils.isEmpty(set)) { diff --git a/src/test/java/com/mzaxd/noodles/NoodlesApplicationTests.java b/src/test/java/com/mzaxd/noodles/NoodlesApplicationTests.java index 667727c..483b837 100644 --- a/src/test/java/com/mzaxd/noodles/NoodlesApplicationTests.java +++ b/src/test/java/com/mzaxd/noodles/NoodlesApplicationTests.java @@ -632,34 +632,45 @@ class NoodlesApplicationTests { @Test - public void containerCheck() { + public void containerCheck() throws InterruptedException { + long startTime = System.nanoTime(); // 记录开始时间 + List containers = containerService.list(); - containers.forEach(container -> { - try { - if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { - container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); - return; - } - //通过Socket(IP+端口)判断是否在线 - if (StringUtils.hasText(container.getServerAddress())) { - if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { - container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + // 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 { + if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { + container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); + containerService.saveOrUpdate(container); return; } - } - //通过HTTP请求判断是否在线 - HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); - log.info("[实例状态检测]:与{}建立连接成功", container.getName()); - container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); - } catch (Exception exception) { - log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); - container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); - // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 - Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); - if (CollectionUtils.isEmpty(set)) { - set = new HashSet<>(); - } + //通过Socket(IP+端口)判断是否在线 + if (StringUtils.hasText(container.getServerAddress())) { + if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { + container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + containerService.saveOrUpdate(container); + return; + } + } + //通过HTTP请求判断是否在线 + HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); + log.info("[实例状态检测]:与{}建立连接成功", container.getName()); + 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 set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 if (set.contains(container.getId().toString())) { return; @@ -680,8 +691,30 @@ class NoodlesApplicationTests { 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 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); } }