修复实例修改不生效的Bug
This commit is contained in:
@@ -57,90 +57,58 @@ public class CheckInstancesStatus {
|
|||||||
@Scheduled(cron = "* 0/1 * * * ?")
|
@Scheduled(cron = "* 0/1 * * * ?")
|
||||||
public void checkInstancesStatus() {
|
public void checkInstancesStatus() {
|
||||||
List<Container> containers = containerService.list();
|
List<Container> containers = containerService.list();
|
||||||
int thread = CpuUtil.getLogicProcessorCount();
|
|
||||||
int containerCount = containerService.count();
|
|
||||||
int corePoolSize = Math.min(thread + 1, containerCount);
|
|
||||||
int maxPoolSize = Math.max(thread + 1, containerCount);
|
|
||||||
|
|
||||||
// 创建一个线程池
|
containers.forEach(container -> {
|
||||||
ExecutorService executor = new ThreadPoolExecutor(
|
|
||||||
corePoolSize,
|
|
||||||
maxPoolSize,
|
|
||||||
1,
|
|
||||||
TimeUnit.SECONDS,
|
|
||||||
new LinkedBlockingQueue<>(),
|
|
||||||
new ThreadPoolExecutor.AbortPolicy());
|
|
||||||
|
|
||||||
// 创建一个 Future 列表,用于存储每个容器检查的结果
|
|
||||||
List<Future<Container>> futures = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Container container : containers) {
|
|
||||||
futures.add(executor.submit(() -> {
|
|
||||||
try {
|
|
||||||
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
|
|
||||||
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
//通过Socket(IP+端口)判断是否在线
|
|
||||||
if (StringUtils.hasText(container.getServerAddress())) {
|
|
||||||
if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
|
|
||||||
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//通过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<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
|
|
||||||
if (CollectionUtils.isEmpty(set)) {
|
|
||||||
set = new HashSet<>();
|
|
||||||
}
|
|
||||||
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
|
|
||||||
if (set.contains(container.getId().toString())) {
|
|
||||||
return container;
|
|
||||||
} else {
|
|
||||||
// 根据实例对应的提醒方式进行提醒
|
|
||||||
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) {
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return container;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 等待所有线程执行完成,并收集更新后的容器列表
|
|
||||||
List<Container> updatedContainers = new ArrayList<>();
|
|
||||||
for (Future<Container> future : futures) {
|
|
||||||
try {
|
try {
|
||||||
Container container = future.get();
|
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
|
||||||
updatedContainers.add(container);
|
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
containerService.saveOrUpdate(container);
|
||||||
log.error("检查容器状态时出错:{}", e.getMessage());
|
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);
|
||||||
|
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<String> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// 将更新后的容器列表保存到数据库中
|
|
||||||
containerService.saveOrUpdateBatch(updatedContainers);
|
|
||||||
// 关闭线程池
|
|
||||||
executor.shutdown();
|
|
||||||
|
|
||||||
//检测虚拟机在线状态
|
//检测虚拟机在线状态
|
||||||
LambdaQueryWrapper<HostMachine> vmWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<HostMachine> vmWrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -150,6 +118,7 @@ public class CheckInstancesStatus {
|
|||||||
try {
|
try {
|
||||||
if (!StringUtils.hasText(vm.getServerAddress()) && !StringUtils.hasText(vm.getServerAddress())) {
|
if (!StringUtils.hasText(vm.getServerAddress()) && !StringUtils.hasText(vm.getServerAddress())) {
|
||||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_UNKNOWN);
|
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_UNKNOWN);
|
||||||
|
hostMachineService.saveOrUpdate(vm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//通过Ping的方式判断是否在线
|
//通过Ping的方式判断是否在线
|
||||||
@@ -157,6 +126,7 @@ public class CheckInstancesStatus {
|
|||||||
if (UrlUtil.isHostOnline(vm.getServerAddress())) {
|
if (UrlUtil.isHostOnline(vm.getServerAddress())) {
|
||||||
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
||||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
||||||
|
hostMachineService.saveOrUpdate(vm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +134,7 @@ public class CheckInstancesStatus {
|
|||||||
HttpRequest.get(vm.getManageIp()).setConnectionTimeout(5000).execute(true);
|
HttpRequest.get(vm.getManageIp()).setConnectionTimeout(5000).execute(true);
|
||||||
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
||||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
||||||
|
hostMachineService.saveOrUpdate(vm);
|
||||||
} 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);
|
||||||
@@ -195,7 +166,6 @@ public class CheckInstancesStatus {
|
|||||||
redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set);
|
redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
hostMachineService.saveOrUpdateBatch(vms);
|
|
||||||
|
|
||||||
//检测物理机在线状态(检测物理机要用探测器的isTureUrl接口)
|
//检测物理机在线状态(检测物理机要用探测器的isTureUrl接口)
|
||||||
LambdaQueryWrapper<HostDetector> detectorWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<HostDetector> detectorWrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -207,13 +177,13 @@ public class CheckInstancesStatus {
|
|||||||
HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId());
|
HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId());
|
||||||
if (Objects.nonNull(hostMachine)) {
|
if (Objects.nonNull(hostMachine)) {
|
||||||
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
||||||
hostMachines.add(hostMachine);
|
hostMachineService.saveOrUpdate(hostMachine);
|
||||||
}
|
}
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId());
|
HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId());
|
||||||
if (Objects.nonNull(hostMachine)) {
|
if (Objects.nonNull(hostMachine)) {
|
||||||
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
|
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
|
||||||
hostMachines.add(hostMachine);
|
hostMachineService.saveOrUpdate(hostMachine);
|
||||||
}
|
}
|
||||||
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
|
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
|
||||||
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
|
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
|
||||||
@@ -243,6 +213,5 @@ public class CheckInstancesStatus {
|
|||||||
redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set);
|
redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
hostMachineService.saveOrUpdateBatch(hostMachines);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user