【修改】 服务器与学生端ws交互,学生端异常退出后,再次考试时间异常的情况

This commit is contained in:
dlaren
2025-09-10 09:55:21 +08:00
parent da0338111a
commit 0c7765b8aa
2 changed files with 72 additions and 77 deletions

View File

@@ -116,11 +116,7 @@ public class AutoToolsController {
securityProperties.getTokenHeader(), securityProperties.getTokenParameter()); securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
// 获取登录用户 // 获取登录用户
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
// 通过token获取用户redis信息获取refreshToken
OAuth2AccessTokenDO oAuth2AccessTokenDO = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get("oauth2_access_token:" + token), OAuth2AccessTokenDO.class);
int startTime = 0; int startTime = 0;
if (oAuth2AccessTokenDO != null) {
String refreshToken = oAuth2AccessTokenDO.getRefreshToken();
// 查找对应的task // 查找对应的task
EducationPaperParam educationPaperParam = educationPaperParamService.selectEducationPaperParamByTaskId(stuInTheExam.getTaskId()); EducationPaperParam educationPaperParam = educationPaperParamService.selectEducationPaperParamByTaskId(stuInTheExam.getTaskId());
String isSession = educationPaperParam.getIsSession(); String isSession = educationPaperParam.getIsSession();
@@ -170,9 +166,11 @@ public class AutoToolsController {
// 返回数据-网络状态 // 返回数据-网络状态
stuTheExamInfo.setNetwork(""); stuTheExamInfo.setNetwork("");
// 创建对应的线程池 // 创建对应的线程池
taskManager.startTask(stuInTheExam, stuTheExamInfo, refreshToken, countdown, new AtomicInteger(0)); if (loginUser != null) {
return CommonResult.success(refreshToken); taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), String.valueOf(loginUser.getId()));
} }
System.out.println("--------------token------------------" + token);
return CommonResult.success(token);
} }
return CommonResult.success("未登录"); return CommonResult.success("未登录");
} }
@@ -185,18 +183,13 @@ public class AutoToolsController {
@GetMapping("/stopExam") @GetMapping("/stopExam")
public CommonResult<Boolean> stopExam() { public CommonResult<Boolean> stopExam() {
HttpServletRequest request = ServletUtils.getRequest(); HttpServletRequest request = ServletUtils.getRequest();
String token = null; String userId = null;
if (request != null) { if (request != null) {
token = SecurityFrameworkUtils.obtainAuthorization(request, userId = String.valueOf(SecurityFrameworkUtils.getLoginUserId());
securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
OAuth2AccessTokenDO oAuth2AccessTokenDO = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get("oauth2_access_token:" + token), OAuth2AccessTokenDO.class);
if (oAuth2AccessTokenDO != null) {
// 删除对应的线程池 // 删除对应的线程池
String refreshToken = oAuth2AccessTokenDO.getRefreshToken(); taskManager.stopTask(userId);
taskManager.stopTask(refreshToken);
return CommonResult.success(true); return CommonResult.success(true);
} }
}
return CommonResult.success(false); return CommonResult.success(false);
} }

View File

@@ -27,28 +27,30 @@ public class TaskManager {
private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>(); private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
/** 开始任务(每秒执行一次) */ /** 开始任务(每秒执行一次) */
public void startTask(StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter) { public void startTask(StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter, String userId) {
// 判断 token 的线程是否存在,存在则不进行任何动作 // 判断 token 的线程是否存在,存在则不进行任何动作
if (tasks.containsKey(token)) { if (tasks.containsKey(userId)) {
log.info("任务 {} 已存在,未重复启动", token); log.info("任务 {} 已存在,未重复启动", userId);
return; // TODO 删除,重置倒计时
tasks.remove(userId);
// return;
} }
tasks.computeIfAbsent(token, k -> { tasks.computeIfAbsent(userId, k -> {
Runnable task = safe(() -> { Runnable task = safe(() -> {
int current = counter.incrementAndGet(); int current = counter.incrementAndGet();
int remaining = countdown.decrementAndGet(); int remaining = countdown.decrementAndGet();
log.debug("任务 {} tick at {}", token, System.currentTimeMillis()); log.debug("任务 {} tick at {}", userId, System.currentTimeMillis());
if (current == stuInTheExam.getTimes()) { if (current == stuInTheExam.getTimes()) {
stuTheExamInfo.setUpload(0); stuTheExamInfo.setUpload(0);
counter.set(0); counter.set(0);
} }
if (remaining <= 0) { if (remaining <= 0) {
ScheduledFuture<?> future = tasks.remove(token); ScheduledFuture<?> future = tasks.remove(userId);
if (future != null) { if (future != null) {
future.cancel(false); future.cancel(false);
stuTheExamInfo.setEndStatus(0); stuTheExamInfo.setEndStatus(0);
log.info("任务 {} 已完成并取消", token); log.info("任务 {} 已完成并取消", userId);
} }
} }
stuTheExamInfo.setTime(formatLongDuration(remaining)); stuTheExamInfo.setTime(formatLongDuration(remaining));
@@ -56,18 +58,18 @@ public class TaskManager {
}); });
return scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS); return scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
}); });
log.info("任务 {} 已启动", token); log.info("任务 {} 已启动", userId);
} }
/** 结束任务 */ /** 结束任务 */
public void stopTask(String token) { public void stopTask(String userId) {
ScheduledFuture<?> future = tasks.get(token); ScheduledFuture<?> future = tasks.get(userId);
if (future != null) { if (future != null) {
future.cancel(false); future.cancel(false);
tasks.remove(token); tasks.remove(userId);
log.info("任务 {} 已停止", token); log.info("任务 {} 已停止", userId);
} else { } else {
log.warn("任务 {} 不存在", token); log.warn("任务 {} 不存在", userId);
} }
} }