diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 7488d91..896f350 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -14,6 +14,7 @@ + diff --git a/pom.xml b/pom.xml index 413d2ab..a44c125 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ Spring Boot WebFlux + MyBatis + MySQL backend - 21 + 17 3.5.8 diff --git a/src/main/java/com/gameplatform/server/service/device/DeviceStatusCheckService.java b/src/main/java/com/gameplatform/server/service/device/DeviceStatusCheckService.java index 43d08b9..04cd7bd 100644 --- a/src/main/java/com/gameplatform/server/service/device/DeviceStatusCheckService.java +++ b/src/main/java/com/gameplatform/server/service/device/DeviceStatusCheckService.java @@ -9,9 +9,14 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import reactor.core.Exceptions; +import reactor.core.publisher.Mono; + import java.time.LocalDateTime; +import java.time.Duration; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeoutException; @Service @Slf4j @@ -39,8 +44,24 @@ public class DeviceStatusCheckService { log.debug("检查设备状态: 设备ID={}, 检查原因={}", machineId, reason); try { - // 1. 获取设备状态 - Map deviceStatus = scriptClient.getDeviceStatus(machineId).block(); + // 1. 获取设备状态(增加超时与错误兜底) + Map deviceStatus = scriptClient + .getDeviceStatus(machineId) + .timeout(Duration.ofSeconds(5)) + .onErrorResume(throwable -> { + Throwable root = Exceptions.unwrap(throwable); + if (root instanceof TimeoutException) { + log.warn("获取设备状态超时,设备ID: {}", machineId); + } else if (root instanceof InterruptedException) { + Thread.currentThread().interrupt(); + log.warn("获取设备状态被中断,设备ID: {}", machineId); + } else { + log.error("获取设备状态失败,设备ID: {}", machineId, root); + } + return Mono.empty(); + }) + .blockOptional() + .orElse(null); if (deviceStatus == null || deviceStatus.isEmpty()) { log.warn("获取设备状态失败,设备ID: {}", machineId); return; @@ -61,6 +82,12 @@ public class DeviceStatusCheckService { } } catch (Exception e) { + Throwable root = Exceptions.unwrap(e); + if (root instanceof InterruptedException) { + Thread.currentThread().interrupt(); + log.warn("检查设备状态被中断,设备ID: {}", machineId); + return; + } log.error("检查设备状态时发生异常,设备ID: {}", machineId, e); } } diff --git a/src/main/java/com/gameplatform/server/task/DeviceStatusCheckTask.java b/src/main/java/com/gameplatform/server/task/DeviceStatusCheckTask.java index e09f3f3..c5691b1 100644 --- a/src/main/java/com/gameplatform/server/task/DeviceStatusCheckTask.java +++ b/src/main/java/com/gameplatform/server/task/DeviceStatusCheckTask.java @@ -4,9 +4,11 @@ import com.gameplatform.server.model.dto.device.DeviceStatusResponse; import com.gameplatform.server.service.device.DeviceStatusCheckService; import com.gameplatform.server.service.external.ScriptClient; import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import reactor.core.Exceptions; +import reactor.core.publisher.Mono; +import java.time.Duration; import java.util.List; /** @@ -32,8 +34,24 @@ public class DeviceStatusCheckTask { log.debug("开始定时检查空闲设备"); try { - // 1. 获取所有设备状态 - DeviceStatusResponse deviceStatus = scriptClient.checkAvailableDeviceStatus().block(); + // 1. 获取所有设备状态(增加超时与错误兜底) + DeviceStatusResponse deviceStatus = scriptClient + .checkAvailableDeviceStatus() + .timeout(Duration.ofSeconds(5)) + .onErrorResume(throwable -> { + Throwable root = Exceptions.unwrap(throwable); + if (root instanceof java.util.concurrent.TimeoutException) { + log.warn("获取所有设备状态超时,跳过本次检查"); + } else if (root instanceof InterruptedException) { + Thread.currentThread().interrupt(); + log.warn("获取所有设备状态被中断,跳过本次检查"); + } else { + log.error("获取所有设备状态失败", root); + } + return Mono.empty(); + }) + .blockOptional() + .orElse(null); if (deviceStatus == null) { log.warn("获取设备状态失败,跳过本次检查"); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 19dc840..e9d8806 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,7 +3,7 @@ spring: name: gameplatform-server datasource: - url: jdbc:mysql://120.46.74.24:3306/login_task_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&sessionVariables=innodb_lock_wait_timeout=30 + url: jdbc:mysql://192.140.164.137:3306/login_task_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&sessionVariables=innodb_lock_wait_timeout=30 username: login_task_db password: 3MaXfeWJ4d6cGMrL driver-class-name: com.mysql.cj.jdbc.Driver diff --git a/src/test/java/com/gameplatform/server/service/link/DeviceTaskUpdateServiceTest.java b/src/test/java/com/gameplatform/server/service/link/DeviceTaskUpdateServiceTest.java index 03734b0..e5b97a9 100644 --- a/src/test/java/com/gameplatform/server/service/link/DeviceTaskUpdateServiceTest.java +++ b/src/test/java/com/gameplatform/server/service/link/DeviceTaskUpdateServiceTest.java @@ -53,7 +53,7 @@ public class DeviceTaskUpdateServiceTest { List tasks = Arrays.asList(task1, task2); when(linkTaskMapper.findByMachineIdAndStatus("f1", "LOGGED_IN")).thenReturn(tasks); - when(linkTaskMapper.update(any(LinkTask.class))).thenReturn(1); + when(linkTaskMapper.updatePointsIfGreater(anyLong(), anyInt())).thenReturn(1); when(gameCompletionDetectionService.detectGameCompletion("f1", "5300", "EVENT_LISTENER")).thenReturn(false); // 执行测试 @@ -62,13 +62,11 @@ public class DeviceTaskUpdateServiceTest { // 验证结果 verify(gameCompletionDetectionService).detectGameCompletion("f1", "5300", "EVENT_LISTENER"); verify(linkTaskMapper).findByMachineIdAndStatus("f1", "LOGGED_IN"); - verify(linkTaskMapper, times(2)).update(any(LinkTask.class)); + verify(linkTaskMapper, times(2)).updatePointsIfGreater(anyLong(), eq(5300)); - // 验证任务状态仍为LOGGED_IN,但点数已更新 + // 验证任务状态仍为LOGGED_IN(点数更新通过数据库完成,不修改内存对象) assertEquals("LOGGED_IN", task1.getStatus()); assertEquals("LOGGED_IN", task2.getStatus()); - assertEquals(Integer.valueOf(5300), task1.getCompletedPoints()); - assertEquals(Integer.valueOf(5300), task2.getCompletedPoints()); } @Test @@ -126,7 +124,7 @@ public class DeviceTaskUpdateServiceTest { // 验证结果 verify(gameCompletionDetectionService).detectGameCompletion("f1", "5300", "EVENT_LISTENER"); verify(linkTaskMapper).findByMachineIdAndStatus("f1", "LOGGED_IN"); - verify(linkTaskMapper, never()).update(any(LinkTask.class)); + verify(linkTaskMapper, never()).updatePointsIfGreater(anyLong(), anyInt()); } @Test