Search in sources :

Example 1 with TestJudgeRes

use of top.hcode.hoj.pojo.dto.TestJudgeRes in project HOJ by HimitZH.

the class Dispatcher method dispatcherTestJudge.

public void dispatcherTestJudge(TestJudgeReq testJudgeReq, String path) {
    AtomicInteger count = new AtomicInteger(0);
    String key = testJudgeReq.getUniqueKey();
    Runnable getResultTask = () -> {
        if (count.get() > 300) {
            // 300次失败则判为提交失败
            Future future = futureTaskMap.get(key);
            if (future != null) {
                boolean isCanceled = future.cancel(true);
                if (isCanceled) {
                    futureTaskMap.remove(key);
                }
            }
            return;
        }
        count.getAndIncrement();
        JudgeServer judgeServer = chooseUtils.chooseServer(false);
        if (judgeServer != null) {
            // 获取到判题机资源
            try {
                JSONObject resultJson = restTemplate.postForObject("http://" + judgeServer.getUrl() + path, testJudgeReq, JSONObject.class);
                if (resultJson != null) {
                    if (resultJson.getInt("status") == ResultStatus.SUCCESS.getStatus()) {
                        TestJudgeRes testJudgeRes = resultJson.getBean("data", TestJudgeRes.class);
                        testJudgeRes.setInput(testJudgeReq.getTestCaseInput());
                        testJudgeRes.setExpectedOutput(testJudgeReq.getExpectedOutput());
                        testJudgeRes.setProblemJudgeMode(testJudgeReq.getProblemJudgeMode());
                        redisUtils.set(testJudgeReq.getUniqueKey(), testJudgeRes, 60);
                    } else {
                        TestJudgeRes testJudgeRes = TestJudgeRes.builder().status(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()).time(0L).memory(0L).stderr(resultJson.getStr("msg")).build();
                        redisUtils.set(testJudgeReq.getUniqueKey(), testJudgeRes, 60);
                    }
                }
            } catch (Exception e) {
                log.error("调用判题服务器[" + judgeServer.getUrl() + "]发送异常-------------->", e);
                TestJudgeRes testJudgeRes = TestJudgeRes.builder().status(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()).time(0L).memory(0L).stderr("Failed to connect the judgeServer. Please resubmit this submission again!").build();
                redisUtils.set(testJudgeReq.getUniqueKey(), testJudgeRes, 60);
            } finally {
                // 无论成功与否,都要将对应的当前判题机当前判题数减1
                reduceCurrentTaskNum(judgeServer.getId());
                Future future = futureTaskMap.get(key);
                if (future != null) {
                    boolean isCanceled = future.cancel(true);
                    if (isCanceled) {
                        futureTaskMap.remove(key);
                    }
                }
            }
        }
    };
    ScheduledFuture<?> scheduledFuture = scheduler.scheduleWithFixedDelay(getResultTask, 0, 1, TimeUnit.SECONDS);
    futureTaskMap.put(key, scheduledFuture);
}
Also used : JSONObject(cn.hutool.json.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JudgeServer(top.hcode.hoj.pojo.entity.judge.JudgeServer) TestJudgeRes(top.hcode.hoj.pojo.dto.TestJudgeRes)

Aggregations

JSONObject (cn.hutool.json.JSONObject)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 TestJudgeRes (top.hcode.hoj.pojo.dto.TestJudgeRes)1 JudgeServer (top.hcode.hoj.pojo.entity.judge.JudgeServer)1