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

View File

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