/// <summary>
        /// The judging meet an internal error, require a rejudging.
        /// </summary>
        private async Task ReturnToQueue(Judging j, int?cid, int pid, int uid, DateTimeOffset subtime)
        {
            await Judgings.CreateAsync(new Judging
            {
                Active            = j.Active,
                Status            = Verdict.Pending,
                FullTest          = j.FullTest,
                RejudgeId         = j.RejudgeId,
                PreviousJudgingId = j.PreviousJudgingId,
                SubmissionId      = j.SubmissionId,
            });

            j.Active            = false;
            j.Status            = Verdict.UndefinedError;
            j.RejudgeId         = null;
            j.PreviousJudgingId = null;
            if (!j.StopTime.HasValue)
            {
                j.StopTime = DateTimeOffset.Now;
            }

            if (cid == 0)
            {
                cid = null;
            }
            await FinishJudging(j, cid, pid, uid, subtime);
        }
        public async Task <IActionResult> AddJudgingRun(
            [FromRoute] string hostname,
            [FromRoute] int judgingId,
            [FromForm, ModelBinder(typeof(JudgingRunBinder))] List <JudgingRunModel> batch)
        {
            if (batch is null)
            {
                return(BadRequest());
            }

            var host = await Judgehosts.FindAsync(hostname);

            if (host is null)
            {
                return(BadRequest());              // Unknown or inactive judgehost requested
            }
            await Judgehosts.NotifyPollAsync(host);

            var(judging, pid, cid, uid, time) = await Judgings.FindAsync(judgingId);

            if (judging is null)
            {
                Logger.LogError("Unknown judging result.");
                return(BadRequest());
            }

            foreach (var run in batch)
            {
                var detail = await Judgings.CreateAsync(run.ParseInfo(judgingId, host.PollTime));

                if (run.output_error is null || run.output_run is null)
                {
                    continue;
                }
                try
                {
                    var stderr = Convert.FromBase64String(run.output_error);
                    var stdout = Convert.FromBase64String(run.output_run);
                    await Judgings.SetRunFileAsync(judgingId, detail.TestId, "out", stdout);

                    await Judgings.SetRunFileAsync(judgingId, detail.TestId, "err", stderr);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "An error occurred when saving OutputError and OutputRun for j{judgingId}", judgingId);
                }
            }

            // Check for the final status
            var countTc = await HttpContext.RequestServices
                          .GetRequiredService <ITestcaseStore>()
                          .CountAsync(pid);

            var verdict = await Judgings.SummarizeAsync(judging);

            // testId for score, testcaseId for count of tested cases

            bool anyRejected = !judging.FullTest && verdict.Status != Verdict.Accepted;
            bool fullTested  = verdict.TestcaseId >= countTc && countTc > 0;

            if (anyRejected || fullTested)
            {
                judging.ExecuteMemory = verdict.ExecuteMemory;
                judging.ExecuteTime   = verdict.ExecuteTime;
                judging.Status        = verdict.Status;
                judging.StopTime      = host.PollTime;
                judging.TotalScore    = verdict.TestId;
                await FinishJudging(judging, cid, pid, uid, time);
            }

            return(Ok());
        }