测试提交远程题库并修改bug

This commit is contained in:
Howie 2021-02-14 22:15:36 +08:00
parent 791970a177
commit 7c79bf01a5
5 changed files with 49 additions and 89 deletions

View File

@ -110,65 +110,39 @@ public class JudgeController {
} }
@PostMapping(value = "/remote-judge") @PostMapping(value = "/remote-judge")
public CommonResult remoteJudge() { public CommonResult remoteJudge(@RequestBody ToJudge toJudge) {
// if (!toJudge.getToken().equals(judgeToken)) { if (!toJudge.getToken().equals(judgeToken)) {
// return CommonResult.errorResponse("对不起!您使用的判题服务调用凭证不正确!访问受限!", CommonResult.STATUS_ACCESS_DENIED); return CommonResult.errorResponse("对不起!您使用的判题服务调用凭证不正确!访问受限!", CommonResult.STATUS_ACCESS_DENIED);
// } }
// Long submitId = toJudge.getJudge().getSubmitId(); Long submitId = toJudge.getJudge().getSubmitId();
// String uid = toJudge.getJudge().getUid(); String uid = toJudge.getJudge().getUid();
// Long cid = toJudge.getJudge().getCid(); Long cid = toJudge.getJudge().getCid();
// Long pid = toJudge.getJudge().getPid(); Long pid = toJudge.getJudge().getPid();
Long submitId = 1L;
String uid = "1";
Long cid = 1L;
Long pid = 1L;
// 发送消息 // 发送消息
try { try {
// String[] source = toJudge.getRemoteJudge().split("-"); String[] source = toJudge.getRemoteJudge().split("-");
// Long remotePid = Long.valueOf(source[1]); Long remotePid = Long.valueOf(source[1]);
// String remoteJudge = source[0]; String remoteJudge = source[0];
// String userCode = toJudge.getJudge().getCode(); String userCode = toJudge.getJudge().getCode();
// String language = toJudge.getJudge().getLanguage(); String language = toJudge.getJudge().getLanguage();
Long remotePid = 1090L;
String remoteJudge = "HDU";
String userCode = "#include<iostream>\n" +
"using namespace std;\n" +
"\n" +
"int main()\n" +
"{\n" +
"\tint t;\n" +
"\tcin >> t;\n" +
"\twhile (t--) {\n" +
"\t\tint a, b;\n" +
"\t\tcin >> a >> b;\n" +
"\t\tcout << a + b;\n" +
"\t\tif (t == 0){\n" +
"\t\t\tcout << endl;\n" +
"\t\t} else {\n" +
"\t\t\tcout << endl;\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn 1;\n" +
"}";
String language = "G++";
remoteJudgeSubmitDispatcher.sendTask(remoteJudge, remotePid, submitId, uid, cid, pid, language, userCode); remoteJudgeSubmitDispatcher.sendTask(remoteJudge, remotePid, submitId, uid, cid, pid, language, userCode);
return CommonResult.successResponse(null, "提交成功"); return CommonResult.successResponse(null, "提交成功");
} catch (Exception e) { } catch (Exception e) {
// TODO 如果为了测试是否可行请把数据库更新注释掉若正常运行则需要保证数据一致性 // TODO 如果为了测试是否可行请把数据库更新注释掉若正常运行则需要保证数据一致性
// Judge judge = new Judge(); Judge judge = new Judge();
// judge.setSubmitId(submitId) judge.setSubmitId(submitId)
// .setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()) .setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus())
// .setErrorMessage("Oops, something has gone wrong with the judgeServer. Please report this to administrator."); .setErrorMessage("Oops, something has gone wrong with the judgeServer. Please report this to administrator.");
// judgeService.updateById(judge); judgeService.updateById(judge);
// // 更新其它表 // 更新其它表
// judgeService.updateOtherTable(submitId, judgeService.updateOtherTable(submitId,
// Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(), Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(),
// cid, cid,
// uid, uid,
// pid, pid,
// null); null);
log.error("调用redis消息发布异常,此次远程判题任务判为系统错误--------------->{}", e.getMessage()); log.error("调用redis消息发布异常,此次远程判题任务判为系统错误--------------->{}", e.getMessage());
return CommonResult.errorResponse(e.getMessage(), CommonResult.STATUS_ERROR); return CommonResult.errorResponse(e.getMessage(), CommonResult.STATUS_ERROR);
} }

View File

@ -45,7 +45,6 @@ public class RemoteJudgeResultReceiver implements MessageListener {
return; return;
} }
JSONObject task = JSONUtil.parseObj(source); JSONObject task = JSONUtil.parseObj(source);
log.info(source);
Long resultSubmitId = task.getLong("resultSubmitId"); Long resultSubmitId = task.getLong("resultSubmitId");
Long submitId = task.getLong("submitId"); Long submitId = task.getLong("submitId");
String uid = task.getStr("uid"); String uid = task.getStr("uid");
@ -61,7 +60,6 @@ public class RemoteJudgeResultReceiver implements MessageListener {
// TODO 获取对应的result修改到数据库 // TODO 获取对应的result修改到数据库
try { try {
log.info("resultSubmitId:" + resultSubmitId);
Map<String, Object> result = remoteJudgeStrategy.result(resultSubmitId); Map<String, Object> result = remoteJudgeStrategy.result(resultSubmitId);
Judge judge = new Judge(); Judge judge = new Judge();
judge.setSubmitId(submitId); judge.setSubmitId(submitId);
@ -79,31 +77,30 @@ public class RemoteJudgeResultReceiver implements MessageListener {
Integer time = (Integer) result.getOrDefault("time", null); Integer time = (Integer) result.getOrDefault("time", null);
Integer memory = (Integer) result.getOrDefault("memory", null); Integer memory = (Integer) result.getOrDefault("memory", null);
String CEInfo = (String) result.getOrDefault("CEInfo", null); String CEInfo = (String) result.getOrDefault("CEInfo", null);
log.info(status.toString() + " " + time + " " + memory + " " + CEInfo); judge.setStatus(status)
// judge.setStatus(status) .setTime(time)
// .setTime(time) .setMemory(memory);
// .setMemory(memory);
// // 获取当前判题系统所在ip写入数据库
// // 获取当前判题系统所在ip写入数据库 if (ip.equals("-1")) {
// if (ip.equals("-1")) { judge.setJudger(IpUtils.getLocalIpv4Address());
// judge.setJudger(IpUtils.getLocalIpv4Address()); } else {
// } else { judge.setJudger(ip);
// judge.setJudger(ip); }
// }
// if (status.intValue() == Constants.Judge.STATUS_COMPILE_ERROR.getStatus()) {
// if (status.intValue() == Constants.Judge.STATUS_COMPILE_ERROR.getStatus()) { judge.setErrorMessage(CEInfo);
// judge.setErrorMessage(CEInfo); } else if (status.intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()) {
// } else if (status.intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()) { judge.setErrorMessage("There is something wrong with the " + remoteJudge + ", please try again later");
// judge.setErrorMessage("There is something wrong with the " + remoteJudge + ", please try again later"); }
// } // 写回数据库
// 写回数据库 judgeService.updateById(judge);
// judgeService.updateById(judge);
/** /**
* @Description TODO 注意 如果是OI题目肯定score得分若不是则请传入null该方法是为了更新关联表保持数据一致 * @Description TODO 注意 如果是OI题目肯定score得分若不是则请传入null该方法是为了更新关联表保持数据一致
* @Since 2021/2/12 * @Since 2021/2/12
**/ **/
// judgeService.updateOtherTable(submitId, status, cid, uid, pid, null); judgeService.updateOtherTable(submitId, status, cid, uid, pid, null);
} catch (Exception e) { } catch (Exception e) {
log.error("获取结果出错------------>{}", e.getMessage()); log.error("获取结果出错------------>{}", e.getMessage());

View File

@ -29,6 +29,5 @@ public class RemoteJudgeSubmitDispatcher {
task.set("language", language); task.set("language", language);
redisUtils.sendMessage(Constants.RemoteJudge.JUDGE_SUBMIT_HANDLER.getName(), "New Problem Added"); redisUtils.sendMessage(Constants.RemoteJudge.JUDGE_SUBMIT_HANDLER.getName(), "New Problem Added");
redisUtils.lrPush(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName(), JSONUtil.toJsonStr(task)); redisUtils.lrPush(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName(), JSONUtil.toJsonStr(task));
log.info("发送数据");
} }
} }

View File

@ -33,12 +33,9 @@ public class RemoteJudgeSubmitReceiver implements MessageListener {
@Override @Override
public void onMessage(Message message, byte[] bytes) { public void onMessage(Message message, byte[] bytes) {
// log.debug("RemoteJudgeSubmitReceiver获取到消息{}", Arrays.toString(message.getBody())); // log.debug("RemoteJudgeSubmitReceiver获取到消息{}", Arrays.toString(message.getBody()));
log.info("开始竞争");
String source = (String) redisUtils.lrPop(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName()); String source = (String) redisUtils.lrPop(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName());
// 如果竞争不到提交队列结束 // 如果竞争不到提交队列结束
log.info(source);
if (source == null) { if (source == null) {
log.info("竞争不到");
return; return;
} }
JSONObject task = JSONUtil.parseObj(source); JSONObject task = JSONUtil.parseObj(source);
@ -65,19 +62,17 @@ public class RemoteJudgeSubmitReceiver implements MessageListener {
// TODO 提交失败 前端手动按按钮再次提交 修改状态 STATUS_SUBMITTED_FAILED // TODO 提交失败 前端手动按按钮再次提交 修改状态 STATUS_SUBMITTED_FAILED
if (resultSubmitId < 0) { if (resultSubmitId < 0) {
// 更新此次提交状态为提交失败 // 更新此次提交状态为提交失败
log.info("查询id失败"); UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
// UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>(); judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus())
// judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus()) .eq("submit_id", submitId);
// .eq("submit_id", submitId); judgeService.update(judgeUpdateWrapper);
// judgeService.update(judgeUpdateWrapper);
log.error("网络错误---------------->获取不到提交ID"); log.error("网络错误---------------->获取不到提交ID");
return; return;
} }
// 提交成功顺便更新状态为-->STATUS_JUDGING 判题中... // 提交成功顺便更新状态为-->STATUS_JUDGING 判题中...
// judgeService.updateById(new Judge().setSubmitId(submitId).setStatus(Constants.Judge.STATUS_JUDGING.getStatus())); judgeService.updateById(new Judge().setSubmitId(submitId).setStatus(Constants.Judge.STATUS_JUDGING.getStatus()));
try { try {
remoteJudgeResultDispatcher.sendTask(remoteJudge, submitId, uid, cid, pid, resultSubmitId); remoteJudgeResultDispatcher.sendTask(remoteJudge, submitId, uid, cid, pid, resultSubmitId);
log.info("查询结果信息发送");
} catch (Exception e) { } catch (Exception e) {
log.error("调用redis消息发布异常,此次远程查询结果任务判为系统错误--------------->{}", e.getMessage()); log.error("调用redis消息发布异常,此次远程查询结果任务判为系统错误--------------->{}", e.getMessage());
} }

View File

@ -47,32 +47,27 @@ public class HduJudge implements RemoteJudgeStrategy {
.put("problemid", String.valueOf(problemId)) .put("problemid", String.valueOf(problemId))
.put("usercode", userCode) .put("usercode", userCode)
.map()); .map());
System.out.println("提交代码的状态码:-------" + response.statusCode());
if (response.statusCode() != 200) { if (response.statusCode() != 200) {
log.error("提交题目失败"); log.error("提交题目失败");
return -1L; return -1L;
} }
// 获取提交的题目id // 获取提交的题目id
Long maxRunId = getMaxRunId(connection, "2018030402055", problemId); Long maxRunId = getMaxRunId(connection, "2018030402055", problemId);
System.out.println(maxRunId);
return maxRunId; return maxRunId;
} }
@Override @Override
public Map<String, Object> result(Long submitId) throws Exception { public Map<String, Object> result(Long submitId) throws Exception {
String url = host + String.format(queryUrl, submitId); String url = host + String.format(queryUrl, submitId);
System.out.println(url);
Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null); Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null);
Connection.Response response = JsoupUtils.getResponse(connection, null); Connection.Response response = JsoupUtils.getResponse(connection, null);
// 1提交时间 2结果 3执行时间 4执行空间 5代码长度 // 1提交时间 2结果 3执行时间 4执行空间 5代码长度
// 一般情况下 代码长度和提交时间不需要想要也行自行添加 // 一般情况下 代码长度和提交时间不需要想要也行自行添加
Pattern pattern = Pattern.compile(">" + submitId + "</td><td>([\\s\\S]*?)</td><td>([\\s\\S]*?)</td><td>[\\s\\S]*?</td><td>(\\d*?)MS</td><td>(\\d*?)K</td><td>(\\d*?)B</td>"); Pattern pattern = Pattern.compile(">" + submitId + "</td><td>([\\s\\S]*?)</td><td>([\\s\\S]*?)</td><td>[\\s\\S]*?</td><td>(\\d*?)MS</td><td>(\\d*?)K</td><td>(\\d*?)B</td>");
Matcher matcher = pattern.matcher(response.body()); Matcher matcher = pattern.matcher(response.body());
// System.out.println(response.body());
// 找到时 // 找到时
Validate.isTrue(matcher.find()); Validate.isTrue(matcher.find());
String rawStatus = matcher.group(2).replaceAll("<[\\s\\S]*?>", "").trim(); String rawStatus = matcher.group(2).replaceAll("<[\\s\\S]*?>", "").trim();
System.out.println(rawStatus);
Constants.Judge statusType = statusTypeMap.get(rawStatus); Constants.Judge statusType = statusTypeMap.get(rawStatus);
if (statusType == Constants.Judge.STATUS_PENDING) { if (statusType == Constants.Judge.STATUS_PENDING) {
return MapUtil.builder(new HashMap<String, Object>()) return MapUtil.builder(new HashMap<String, Object>())