API数据表更新
This commit is contained in:
@@ -4,10 +4,7 @@ package com.mini.capi.job;
|
|||||||
import com.mini.capi.biz.domain.*;
|
import com.mini.capi.biz.domain.*;
|
||||||
import com.mini.capi.biz.service.*;
|
import com.mini.capi.biz.service.*;
|
||||||
import com.mini.capi.model.ApiResult;
|
import com.mini.capi.model.ApiResult;
|
||||||
import com.mini.capi.utils.HostInfo;
|
import com.mini.capi.utils.*;
|
||||||
import com.mini.capi.utils.vDate;
|
|
||||||
import com.mini.capi.utils.vId;
|
|
||||||
import com.mini.capi.utils.vToken;
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -21,7 +18,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/Sys/jobs")
|
@RequestMapping("/Sys/jobs")
|
||||||
public class taskHostDisk {
|
public class taskEnable {
|
||||||
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@@ -14,6 +14,7 @@ import com.mini.capi.utils.docker;
|
|||||||
import com.mini.capi.utils.vDate;
|
import com.mini.capi.utils.vDate;
|
||||||
import com.mini.capi.utils.vToken;
|
import com.mini.capi.utils.vToken;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -22,6 +23,8 @@ import java.time.Instant;
|
|||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -41,6 +44,10 @@ public class sysController {
|
|||||||
private DockerContainerInfoService dockerInfoService;
|
private DockerContainerInfoService dockerInfoService;
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private Executor hostExecutor;
|
||||||
|
|
||||||
|
|
||||||
public static class SnapshotDTO {
|
public static class SnapshotDTO {
|
||||||
public String hostName;
|
public String hostName;
|
||||||
public String timestamp;
|
public String timestamp;
|
||||||
@@ -199,6 +206,54 @@ public class sysController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void handleSingleDockerHost(DockerHost host, List<String> errorList) {
|
||||||
|
try {
|
||||||
|
/* 1. 一次性加载现有数据 */
|
||||||
|
List<DockerContainerInfo> oldList =
|
||||||
|
dockerInfoService.lambdaQuery()
|
||||||
|
.eq(DockerContainerInfo::getDokerHostId, host.getDokerHostId())
|
||||||
|
.list();
|
||||||
|
|
||||||
|
Map<String, DockerContainerInfo> oldMap = oldList.stream()
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
c -> buildKey(c.getDokerHostId(), c.getImageName(), c.getUnames()),
|
||||||
|
c -> c));
|
||||||
|
|
||||||
|
/* 2. 远程最新数据 */
|
||||||
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
|
List<docker.DockerInfo> remoteList =
|
||||||
|
docker.getDockerInfo(sshInfo.getHostIp(),
|
||||||
|
Long.valueOf(sshInfo.getHostPort()),
|
||||||
|
sshUser.getCUsername(),
|
||||||
|
sshUser.getCPassword());
|
||||||
|
|
||||||
|
/* 3. 新增或更新 */
|
||||||
|
addOrEdit(host.getDokerHostId(), oldMap, sshInfo, remoteList);
|
||||||
|
|
||||||
|
/* 4. 删除已失效容器 */
|
||||||
|
if (!oldMap.isEmpty()) {
|
||||||
|
List<String> idsToDel = oldMap.values()
|
||||||
|
.stream()
|
||||||
|
.map(DockerContainerInfo::getId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
dockerInfoService.removeByIds(idsToDel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 5. 更新运行数 */
|
||||||
|
long runningCnt = remoteList.stream()
|
||||||
|
.filter(r -> "1".equals(r.getStatus()))
|
||||||
|
.count();
|
||||||
|
host.setRunNum(runningCnt);
|
||||||
|
host.setUpdateTime(vDate.getNow());
|
||||||
|
dockerHostService.updateById(host);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorList.add(String.format("hostId=%s, error=%s",
|
||||||
|
host.getDokerHostId(), e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/getApiInfo")
|
@GetMapping("/getApiInfo")
|
||||||
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
@@ -222,45 +277,19 @@ public class sysController {
|
|||||||
* 获取容器列表
|
* 获取容器列表
|
||||||
*/
|
*/
|
||||||
@GetMapping("/getApiDockerInfo")
|
@GetMapping("/getApiDockerInfo")
|
||||||
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
|
public ApiResult<?> getDockerInfo(String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
DockerHost host = dockerHostService.getById(dockerHostId);
|
List<DockerHost> dockerHosts = dockerHostService.list();
|
||||||
/* 1. 现有数据一次性加载到内存 */
|
List<String> errorList = Collections.synchronizedList(new ArrayList<>());
|
||||||
List<DockerContainerInfo> oldList =
|
CompletableFuture<?>[] futures = dockerHosts.stream()
|
||||||
dockerInfoService.lambdaQuery()
|
.map(host -> CompletableFuture.runAsync(
|
||||||
.eq(DockerContainerInfo::getDokerHostId, dockerHostId)
|
() -> handleSingleDockerHost(host, errorList), hostExecutor))
|
||||||
.list();
|
.toArray(CompletableFuture[]::new);
|
||||||
|
// 等待全部执行完毕
|
||||||
Map<String, DockerContainerInfo> oldMap = oldList.stream()
|
CompletableFuture.allOf(futures).join();
|
||||||
.collect(Collectors.toMap(
|
return errorList.isEmpty()
|
||||||
c -> buildKey(c.getDokerHostId(), c.getImageName(), c.getUnames()),
|
? ApiResult.success()
|
||||||
c -> c));
|
: ApiResult.error();
|
||||||
/* 2. 远程最新数据 */
|
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
|
||||||
List<docker.DockerInfo> remoteList =
|
|
||||||
docker.getDockerInfo(sshInfo.getHostIp(),
|
|
||||||
Long.valueOf(sshInfo.getHostPort()),
|
|
||||||
sshUser.getCUsername(),
|
|
||||||
sshUser.getCPassword());
|
|
||||||
/* 3. 新增 or 更新 */
|
|
||||||
addOrEdit(dockerHostId, oldMap, sshInfo, remoteList);
|
|
||||||
/* 4. 本轮未命中的即为“已失效”,批量删除 */
|
|
||||||
if (!oldMap.isEmpty()) {
|
|
||||||
List<String> idsToDel = oldMap.values()
|
|
||||||
.stream()
|
|
||||||
.map(DockerContainerInfo::getId)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
dockerInfoService.removeByIds(idsToDel);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 5. 更新运行数 */
|
|
||||||
long runningCnt = remoteList.stream()
|
|
||||||
.filter(r -> "1".equals(r.getStatus()))
|
|
||||||
.count();
|
|
||||||
host.setRunNum(runningCnt);
|
|
||||||
host.setUpdateTime(vDate.getNow());
|
|
||||||
dockerHostService.updateById(host);
|
|
||||||
}
|
}
|
||||||
return ApiResult.error();
|
return ApiResult.error();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user