feat: 在DeviceStats类中添加自动完成LOGGED_IN任务的逻辑,若设备空闲超过3分钟则标记为COMPLETED,并记录状态历史
This commit is contained in:
@@ -3,6 +3,8 @@ package com.gameplatform.server.device;
|
||||
import com.gameplatform.server.model.dto.device.DeviceStatusResponse;
|
||||
import com.gameplatform.server.model.entity.agent.LinkTask;
|
||||
import com.gameplatform.server.model.entity.history.DeviceStatusTransition;
|
||||
import com.gameplatform.server.mapper.history.LinkTaskStatusHistoryMapper;
|
||||
import com.gameplatform.server.model.entity.history.LinkTaskStatusHistory;
|
||||
import com.gameplatform.server.service.cooldown.MemoryMachineCooldownService;
|
||||
import com.gameplatform.server.service.link.DeviceAllocationService;
|
||||
import com.gameplatform.server.mapper.agent.LinkTaskMapper;
|
||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Duration;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
@@ -68,6 +71,7 @@ public class DeviceStats {
|
||||
private final MemoryMachineCooldownService cooldownService;
|
||||
private final DeviceStatusTransitionMapper transitionMapper;
|
||||
private final DeviceAllocationService deviceAllocationService;
|
||||
private final LinkTaskStatusHistoryMapper statusHistoryMapper;
|
||||
private final SystemConfigService systemConfigService;
|
||||
|
||||
// 记录上一次统计时每台设备的分类结果,用于检测状态变更
|
||||
@@ -77,12 +81,14 @@ public class DeviceStats {
|
||||
MemoryMachineCooldownService cooldownService,
|
||||
DeviceStatusTransitionMapper transitionMapper,
|
||||
DeviceAllocationService deviceAllocationService,
|
||||
SystemConfigService systemConfigService) {
|
||||
SystemConfigService systemConfigService,
|
||||
LinkTaskStatusHistoryMapper statusHistoryMapper) {
|
||||
this.linkTaskMapper = linkTaskMapper;
|
||||
this.cooldownService = cooldownService;
|
||||
this.transitionMapper = transitionMapper;
|
||||
this.deviceAllocationService = deviceAllocationService;
|
||||
this.systemConfigService = systemConfigService;
|
||||
this.statusHistoryMapper = statusHistoryMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,6 +127,19 @@ public class DeviceStats {
|
||||
// log.debug("设备[{}] 原始脚本值='{}' | LOGGED_IN={} USING={} COOLDOWN={} NUMERIC={}",
|
||||
// deviceId, v, loggedIn, usingTask, cooldown, numeric);
|
||||
|
||||
// 当脚本值为配置的空闲标识时,若设备上存在 LOGGED_IN 且登录超过3分钟的任务,则自动完成这些任务
|
||||
if (v != null && configuredIdle != null && configuredIdle.equals(v)) {
|
||||
try {
|
||||
int completed = autoCompleteLoggedInTasksIfIdleOver3m(deviceId);
|
||||
if (completed > 0) {
|
||||
// 任务状态可能已变化,重新评估 loggedIn
|
||||
loggedIn = hasLoggedInTask(deviceId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("autoCompleteLoggedInTasksIfIdleOver3m 执行异常 device={} err={}", deviceId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 分类与原因
|
||||
Category newCategory;
|
||||
String reason;
|
||||
@@ -276,6 +295,48 @@ public class DeviceStats {
|
||||
return selectedDeviceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当脚本回报为配置的空闲标识时,如果设备上仍有 LOGGED_IN 任务且运行超过3分钟,则将其自动标记为 COMPLETED。
|
||||
* 返回成功完成的任务数量。
|
||||
*/
|
||||
private int autoCompleteLoggedInTasksIfIdleOver3m(String deviceId) {
|
||||
List<LinkTask> loggedInTasks = linkTaskMapper.findByMachineIdAndStatus(deviceId, "LOGGED_IN");
|
||||
if (loggedInTasks == null || loggedInTasks.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
int completed = 0;
|
||||
for (LinkTask task : loggedInTasks) {
|
||||
LocalDateTime loginAt = task.getLoginAt();
|
||||
boolean over3m = loginAt != null && loginAt.isBefore(now.minusMinutes(3));
|
||||
if (!over3m) {
|
||||
continue;
|
||||
}
|
||||
String prev = task.getStatus();
|
||||
task.setStatus("COMPLETED");
|
||||
task.setReason("AUTO_COMPLETE_IDLE_3M");
|
||||
task.setUpdatedAt(now);
|
||||
try {
|
||||
int updated = linkTaskMapper.update(task);
|
||||
if (updated > 0) {
|
||||
Integer sinceLoginSeconds = loginAt != null ? (int) Duration.between(loginAt, now).getSeconds() : null;
|
||||
try {
|
||||
statusHistoryMapper.insert(new LinkTaskStatusHistory(
|
||||
task.getId(), task.getCodeNo(), deviceId,
|
||||
prev, "COMPLETED", "DEVICE_STATS", "idleConfiguredAndRunningOver3m",
|
||||
sinceLoginSeconds, null
|
||||
));
|
||||
} catch (Exception ignore) {}
|
||||
completed++;
|
||||
log.info("自动完成任务:codeNo={} device={} loginAt={} 超过3分钟", task.getCodeNo(), deviceId, loginAt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("自动完成任务失败:codeNo={} device={} err={}", task.getCodeNo(), deviceId, e.getMessage());
|
||||
}
|
||||
}
|
||||
return completed;
|
||||
}
|
||||
|
||||
private boolean hasLoggedInTask(String deviceId) {
|
||||
List<LinkTask> loggedInTasks = linkTaskMapper.findByMachineIdAndStatus(deviceId, "LOGGED_IN");
|
||||
return loggedInTasks != null && !loggedInTasks.isEmpty();
|
||||
|
||||
Reference in New Issue
Block a user