diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java index cba14003..9af70b2b 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java @@ -116,63 +116,61 @@ 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(); - if (isSession.equals("0")) { - // 设置了场次 - // 剩余时间需要继续计算 - EducationPaperPerson educationPaperPerson = educationPaperPersonMapper.selectByTaskIdAndPersonId(stuInTheExam.getTaskId(), String.valueOf(loginUser.getId())); - EducationPaperSession educationPaperSession = educationPaperSessionMapper.selectEducationPaperSessionBySessionId(educationPaperPerson.getSessionId()); - //结束时间 - Date endTime = educationPaperSession.getEndTime(); - Date nowTime = new Date(); - // 将 Date 转换为 Instant - Instant endInstant = endTime.toInstant(); - Instant nowInstant = nowTime.toInstant(); - // 计算秒数差 - long diffInSeconds = Duration.between(nowInstant, endInstant).getSeconds(); - startTime = (int) diffInSeconds; - } else { - // 没有设置场次 + // 查找对应的task + EducationPaperParam educationPaperParam = educationPaperParamService.selectEducationPaperParamByTaskId(stuInTheExam.getTaskId()); + String isSession = educationPaperParam.getIsSession(); + if (isSession.equals("0")) { + // 设置了场次 + // 剩余时间需要继续计算 + EducationPaperPerson educationPaperPerson = educationPaperPersonMapper.selectByTaskIdAndPersonId(stuInTheExam.getTaskId(), String.valueOf(loginUser.getId())); + EducationPaperSession educationPaperSession = educationPaperSessionMapper.selectEducationPaperSessionBySessionId(educationPaperPerson.getSessionId()); + //结束时间 + Date endTime = educationPaperSession.getEndTime(); + Date nowTime = new Date(); + // 将 Date 转换为 Instant + Instant endInstant = endTime.toInstant(); + Instant nowInstant = nowTime.toInstant(); + // 计算秒数差 + long diffInSeconds = Duration.between(nowInstant, endInstant).getSeconds(); + startTime = (int) diffInSeconds; + } else { + // 没有设置场次 + // 剩余时间直接读取数据 + String isTime = educationPaperParam.getIsTime(); + if (isTime.equals("0")) { + // 设置了时间 // 剩余时间直接读取数据 - String isTime = educationPaperParam.getIsTime(); - if (isTime.equals("0")) { - // 设置了时间 - // 剩余时间直接读取数据 - long seconds = educationPaperParam.getExamTime().toLocalTime().toSecondOfDay(); - startTime = (int) seconds; - } else { - // 没有设置时间 默认1小时 = 3600秒 - startTime = 3600; - } + long seconds = educationPaperParam.getExamTime().toLocalTime().toSecondOfDay(); + startTime = (int) seconds; + } else { + // 没有设置时间 默认1小时 = 3600秒 + startTime = 3600; } - // 判断是否存在对应的场次 - // 定时上传文件时间 - String time = educationPaperParam.getUploadTime(); - // 将分钟继续转换成秒 - stuInTheExam.setTimes(Integer.parseInt(time) * 60); - // 倒计时 - AtomicInteger countdown = new AtomicInteger(startTime); - // 创建初始返回数据 - StuTheExamInfo stuTheExamInfo = new StuTheExamInfo(); - // 返回数据-剩余时间 - stuTheExamInfo.setTime(formatLongDuration(countdown.get())); - // 返回数据-上传文件状态 0:上传;1:不上传 - stuTheExamInfo.setUpload(1); - // 返回数据-上传文件状态 0:结束;1:不结束 - stuTheExamInfo.setEndStatus(1); - // 返回数据-网络状态 - stuTheExamInfo.setNetwork("强"); - // 创建对应的线程池 - taskManager.startTask(stuInTheExam, stuTheExamInfo, refreshToken, countdown, new AtomicInteger(0)); - return CommonResult.success(refreshToken); } + // 判断是否存在对应的场次 + // 定时上传文件时间 + String time = educationPaperParam.getUploadTime(); + // 将分钟继续转换成秒 + stuInTheExam.setTimes(Integer.parseInt(time) * 60); + // 倒计时 + AtomicInteger countdown = new AtomicInteger(startTime); + // 创建初始返回数据 + StuTheExamInfo stuTheExamInfo = new StuTheExamInfo(); + // 返回数据-剩余时间 + stuTheExamInfo.setTime(formatLongDuration(countdown.get())); + // 返回数据-上传文件状态 0:上传;1:不上传 + stuTheExamInfo.setUpload(1); + // 返回数据-上传文件状态 0:结束;1:不结束 + stuTheExamInfo.setEndStatus(1); + // 返回数据-网络状态 + stuTheExamInfo.setNetwork("强"); + // 创建对应的线程池 + 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,17 +183,12 @@ public class AutoToolsController { @GetMapping("/stopExam") public CommonResult 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) { - // 删除对应的线程池 - String refreshToken = oAuth2AccessTokenDO.getRefreshToken(); - taskManager.stopTask(refreshToken); - return CommonResult.success(true); - } + userId = String.valueOf(SecurityFrameworkUtils.getLoginUserId()); + // 删除对应的线程池 + taskManager.stopTask(userId); + return CommonResult.success(true); } return CommonResult.success(false); } diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java index 93650603..0266304f 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java @@ -27,28 +27,30 @@ public class TaskManager { private final Map> 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); } }