feat: 在Detection类中添加基于设备快照推进USING状态到LOGGED_IN的逻辑,删除UsingLinkCheckTask定时任务类以简化代码结构
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package com.gameplatform.server.device;
|
package com.gameplatform.server.device;
|
||||||
|
|
||||||
import com.gameplatform.server.model.dto.device.DeviceStatusResponse;
|
import com.gameplatform.server.model.dto.device.DeviceStatusResponse;
|
||||||
|
import com.gameplatform.server.model.entity.agent.LinkTask;
|
||||||
|
import com.gameplatform.server.service.link.LinkStatusService;
|
||||||
|
import com.gameplatform.server.mapper.agent.LinkTaskMapper;
|
||||||
import com.gameplatform.server.service.external.ScriptClient;
|
import com.gameplatform.server.service.external.ScriptClient;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -24,6 +27,8 @@ public class Detection {
|
|||||||
|
|
||||||
private final ScriptClient scriptClient;
|
private final ScriptClient scriptClient;
|
||||||
private final DeviceStats deviceStats;
|
private final DeviceStats deviceStats;
|
||||||
|
private final LinkStatusService linkStatusService;
|
||||||
|
private final LinkTaskMapper linkTaskMapper;
|
||||||
|
|
||||||
// 缓存配置(可通过 application.yml 覆盖)
|
// 缓存配置(可通过 application.yml 覆盖)
|
||||||
private final long listAllCacheTtlMs;
|
private final long listAllCacheTtlMs;
|
||||||
@@ -39,11 +44,15 @@ public class Detection {
|
|||||||
public Detection(
|
public Detection(
|
||||||
ScriptClient scriptClient,
|
ScriptClient scriptClient,
|
||||||
DeviceStats deviceStats,
|
DeviceStats deviceStats,
|
||||||
|
LinkStatusService linkStatusService,
|
||||||
|
LinkTaskMapper linkTaskMapper,
|
||||||
@Value("${detection.listAll.cacheTtlMs:500}") long listAllCacheTtlMs,
|
@Value("${detection.listAll.cacheTtlMs:500}") long listAllCacheTtlMs,
|
||||||
@Value("${detection.readOne.cacheTtlMs:200}") long readOneCacheTtlMs
|
@Value("${detection.readOne.cacheTtlMs:200}") long readOneCacheTtlMs
|
||||||
) {
|
) {
|
||||||
this.scriptClient = scriptClient;
|
this.scriptClient = scriptClient;
|
||||||
this.deviceStats = deviceStats;
|
this.deviceStats = deviceStats;
|
||||||
|
this.linkStatusService = linkStatusService;
|
||||||
|
this.linkTaskMapper = linkTaskMapper;
|
||||||
this.listAllCacheTtlMs = listAllCacheTtlMs;
|
this.listAllCacheTtlMs = listAllCacheTtlMs;
|
||||||
this.readOneCacheTtlMs = readOneCacheTtlMs;
|
this.readOneCacheTtlMs = readOneCacheTtlMs;
|
||||||
}
|
}
|
||||||
@@ -122,6 +131,30 @@ public class Detection {
|
|||||||
if (snapshot != null) {
|
if (snapshot != null) {
|
||||||
deviceStats.updateWithSnapshot(snapshot);
|
deviceStats.updateWithSnapshot(snapshot);
|
||||||
log.info("设备快照更新统计完成");
|
log.info("设备快照更新统计完成");
|
||||||
|
|
||||||
|
// 基于最新设备状态推进 USING→LOGGED_IN
|
||||||
|
try {
|
||||||
|
// 查询所有 USING 任务
|
||||||
|
java.util.List<LinkTask> usingTasks = linkTaskMapper.findByStatus("USING");
|
||||||
|
if (usingTasks != null && !usingTasks.isEmpty()) {
|
||||||
|
for (LinkTask task : usingTasks) {
|
||||||
|
String machineId = task.getMachineId();
|
||||||
|
if (machineId == null) { continue; }
|
||||||
|
DeviceStatusResponse.DeviceInfo info = snapshot.getDevices() != null ? snapshot.getDevices().get(machineId) : null;
|
||||||
|
String val = info != null ? info.getVal() : null;
|
||||||
|
if ("已运行".equals(val)) {
|
||||||
|
// 设备显示已运行,触发一次登录检测
|
||||||
|
linkStatusService.pollLogin(task.getCodeNo())
|
||||||
|
.subscribe(
|
||||||
|
r -> { if (r.isSuccess()) { log.info("检测到设备已运行,推进链接登录成功 codeNo={}", task.getCodeNo()); } },
|
||||||
|
e -> log.warn("基于快照推进登录失败 codeNo={} err={}", task.getCodeNo(), e.getMessage())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.warn("基于设备快照推进USING→LOGGED_IN时出现异常", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("定时拉取设备快照并更新统计失败", e);
|
log.error("定时拉取设备快照并更新统计失败", e);
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import com.gameplatform.server.model.entity.agent.LinkBatch;
|
|||||||
import com.gameplatform.server.model.entity.agent.LinkTask;
|
import com.gameplatform.server.model.entity.agent.LinkTask;
|
||||||
|
|
||||||
import com.gameplatform.server.service.external.ScriptClient;
|
import com.gameplatform.server.service.external.ScriptClient;
|
||||||
import com.gameplatform.server.device.Detection;
|
|
||||||
import com.gameplatform.server.device.DeviceStats;
|
import com.gameplatform.server.device.DeviceStats;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import com.gameplatform.server.service.device.DeviceStatusCheckService;
|
import com.gameplatform.server.service.device.DeviceStatusCheckService;
|
||||||
@@ -46,7 +45,6 @@ public class LinkStatusService {
|
|||||||
private final SystemConfigService systemConfigService;
|
private final SystemConfigService systemConfigService;
|
||||||
private final MemoryMachineCooldownService machineCooldownService;
|
private final MemoryMachineCooldownService machineCooldownService;
|
||||||
private final DeviceAllocationService deviceAllocationService;
|
private final DeviceAllocationService deviceAllocationService;
|
||||||
private final Detection detection;
|
|
||||||
private final DeviceStats deviceStats;
|
private final DeviceStats deviceStats;
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private com.gameplatform.server.service.detection.GameCompletionDetectionService completionDetectionService;
|
private com.gameplatform.server.service.detection.GameCompletionDetectionService completionDetectionService;
|
||||||
@@ -66,7 +64,7 @@ public class LinkStatusService {
|
|||||||
|
|
||||||
public LinkStatusService(LinkTaskMapper linkTaskMapper, LinkBatchMapper linkBatchMapper,
|
public LinkStatusService(LinkTaskMapper linkTaskMapper, LinkBatchMapper linkBatchMapper,
|
||||||
ScriptClient scriptClient,
|
ScriptClient scriptClient,
|
||||||
DeviceStatusCheckService deviceStatusCheckService, SystemConfigService systemConfigService, MemoryMachineCooldownService machineCooldownService, DeviceAllocationService deviceAllocationService, Detection detection, DeviceStats deviceStats) {
|
DeviceStatusCheckService deviceStatusCheckService, SystemConfigService systemConfigService, MemoryMachineCooldownService machineCooldownService, DeviceAllocationService deviceAllocationService, DeviceStats deviceStats) {
|
||||||
this.linkTaskMapper = linkTaskMapper;
|
this.linkTaskMapper = linkTaskMapper;
|
||||||
this.linkBatchMapper = linkBatchMapper;
|
this.linkBatchMapper = linkBatchMapper;
|
||||||
this.scriptClient = scriptClient;
|
this.scriptClient = scriptClient;
|
||||||
@@ -74,7 +72,6 @@ public class LinkStatusService {
|
|||||||
this.systemConfigService = systemConfigService;
|
this.systemConfigService = systemConfigService;
|
||||||
this.machineCooldownService = machineCooldownService;
|
this.machineCooldownService = machineCooldownService;
|
||||||
this.deviceAllocationService = deviceAllocationService;
|
this.deviceAllocationService = deviceAllocationService;
|
||||||
this.detection = detection;
|
|
||||||
this.deviceStats = deviceStats;
|
this.deviceStats = deviceStats;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,8 +702,8 @@ private UserLinkStatusResponse doGetUserLinkStatus(Long linkId, String codeNo) {
|
|||||||
linkTaskMapper.updateById(linkTask);
|
linkTaskMapper.updateById(linkTask);
|
||||||
}
|
}
|
||||||
log.info("首次选区: 开始检查和分配空闲设备");
|
log.info("首次选区: 开始检查和分配空闲设备");
|
||||||
// 使用 Detection 的短TTL缓存,避免频繁触发事件引发并发写
|
// 获取当前设备快照(直接从脚本端获取最新状态)
|
||||||
DeviceStatusResponse deviceStatus = detection.listAllDevices();
|
DeviceStatusResponse deviceStatus = scriptClient.checkAvailableDeviceStatus().block();
|
||||||
if (deviceStatus == null) {
|
if (deviceStatus == null) {
|
||||||
log.error("获取设备快照失败,无法进行选区");
|
log.error("获取设备快照失败,无法进行选区");
|
||||||
throw new RuntimeException("暂时无法获取设备状态,请稍后再试");
|
throw new RuntimeException("暂时无法获取设备状态,请稍后再试");
|
||||||
|
|||||||
@@ -1,104 +0,0 @@
|
|||||||
package com.gameplatform.server.task;
|
|
||||||
|
|
||||||
import com.gameplatform.server.mapper.agent.LinkTaskMapper;
|
|
||||||
import com.gameplatform.server.model.entity.agent.LinkTask;
|
|
||||||
import com.gameplatform.server.service.link.LinkStatusService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* USING状态链接检查定时任务
|
|
||||||
* 每30秒检测状态为USING的链接,调用checkAndHandleLoginStatus函数
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@Slf4j
|
|
||||||
public class UsingLinkCheckTask {
|
|
||||||
|
|
||||||
private final LinkTaskMapper linkTaskMapper;
|
|
||||||
private final LinkStatusService linkStatusService;
|
|
||||||
|
|
||||||
public UsingLinkCheckTask(LinkTaskMapper linkTaskMapper, LinkStatusService linkStatusService) {
|
|
||||||
this.linkTaskMapper = linkTaskMapper;
|
|
||||||
this.linkStatusService = linkStatusService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 每30秒检测一次USING状态的链接
|
|
||||||
*/
|
|
||||||
// @Scheduled(fixedRate = 30000) // 每30秒执行一次
|
|
||||||
public void checkUsingLinksAndHandleLoginStatus() {
|
|
||||||
log.debug("开始定时检查USING状态的链接");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 1. 查询所有USING状态的链接任务
|
|
||||||
List<LinkTask> usingTasks = linkTaskMapper.findByStatus("USING");
|
|
||||||
|
|
||||||
if (usingTasks.isEmpty()) {
|
|
||||||
log.debug("当前没有USING状态的链接");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("发现 {} 个USING状态的链接", usingTasks.size());
|
|
||||||
|
|
||||||
// 2. 对每个USING状态的链接调用checkAndHandleLoginStatus
|
|
||||||
for (LinkTask linkTask : usingTasks) {
|
|
||||||
try {
|
|
||||||
log.debug("检查链接 {} (设备: {}) 的登录状态", linkTask.getCodeNo(), linkTask.getMachineId());
|
|
||||||
|
|
||||||
// 调用checkAndHandleLoginStatus函数 - 这里需要使用私有方法的逻辑
|
|
||||||
if (linkTask.getMachineId() != null) {
|
|
||||||
checkAndHandleLoginStatus(linkTask);
|
|
||||||
} else {
|
|
||||||
log.warn("链接 {} 没有关联设备,跳过检查", linkTask.getCodeNo());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("检查链接 {} 登录状态时发生异常: {}", linkTask.getCodeNo(), e.getMessage());
|
|
||||||
// 继续检查下一个链接,不因为一个链接出错而中断整个流程
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("定时检查USING状态链接时发生异常", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("定时检查USING状态链接完成");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查并处理登录状态
|
|
||||||
* 复制LinkStatusService中checkAndHandleLoginStatus的逻辑
|
|
||||||
*/
|
|
||||||
private void checkAndHandleLoginStatus(LinkTask linkTask) {
|
|
||||||
try {
|
|
||||||
String deviceId = linkTask.getMachineId();
|
|
||||||
if (deviceId == null) {
|
|
||||||
log.warn("无法获取设备编号: codeNo={}", linkTask.getCodeNo());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("调用轮询上号接口检查链接 {} 的登录状态", linkTask.getCodeNo());
|
|
||||||
|
|
||||||
// 调用LinkStatusService的pollLogin方法来处理登录状态检查
|
|
||||||
linkStatusService.pollLogin(linkTask.getCodeNo())
|
|
||||||
.subscribe(
|
|
||||||
response -> {
|
|
||||||
if (response.isSuccess()) {
|
|
||||||
log.info("定时检查发现链接 {} 已成功登录,状态已更新", linkTask.getCodeNo());
|
|
||||||
} else {
|
|
||||||
log.debug("链接 {} 尚未登录,状态: {}", linkTask.getCodeNo(), response.getStatus());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error -> {
|
|
||||||
log.warn("检查链接 {} 登录状态失败: {}", linkTask.getCodeNo(), error.getMessage());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("处理链接 {} 登录状态检查时发生异常: {}", linkTask.getCodeNo(), e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user