core: seperate task and schedule to enhance performance (#440)

This commit is contained in:
undefined authored and GitHub committed 2022-10-16 01:13:21 +08:00
1 parent 101fcaa1b5
commit 175609259a
19 files changed
+250 -144

No files matched your search

+10 -12
View File
@@ -17,9 +17,9 @@ import message from '../model/message';
import * as oplog from '../model/oplog';
import problem from '../model/problem';
import record from '../model/record';
import ScheduleModel from '../model/schedule';
import storage from '../model/storage';
import * as system from '../model/system';
import TaskModel from '../model/task';
import user from '../model/user';
import {
Handler, param, post, Types,
@@ -52,7 +52,7 @@ registerResolver(
'Get a contest by ID',
);
TaskModel.Worker.addHandler('contest', async (doc) => {
ScheduleModel.Worker.addHandler('contest', async (doc) => {
const tdoc = await contest.get(doc.domainId, doc.tid);
if (!tdoc) return;
const tasks = [];
@@ -194,7 +194,7 @@ export class ContestDetailHandler extends ContestDetailBaseHandler {
if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST);
await contest.del(domainId, tid);
await record.updateMulti(domainId, { domainId, contest: tid }, undefined, undefined, { contest: '' });
await TaskModel.deleteMany({
await ScheduleModel.deleteMany({
type: 'schedule', subType: 'contest', domainId, tid,
});
this.response.redirect = this.url('contest_main');
@@ -341,20 +341,18 @@ export class ContestEditHandler extends Handler {
const task = {
type: 'schedule', subType: 'contest', domainId, tid,
};
await TaskModel.deleteMany(task);
await ScheduleModel.deleteMany(task);
const operation = [];
if (Date.now() <= endAt.getTime() && autoHide) {
// eslint-disable-next-line no-await-in-loop
await Promise.all(pids.map((pid) => problem.edit(domainId, pid, { hidden: true })));
await TaskModel.add({
...task,
operation: ['unhide'],
executeAfter: endAt,
});
operation.push('unhide');
}
if (lock && lockAt <= endAt) {
await TaskModel.add({
if (lock && lockAt <= endAt) operation.push('unlock');
if (operation.length) {
await ScheduleModel.add({
...task,
operation: ['unlock'],
operation,
executeAfter: endAt,
});
}
+4 -5
View File
@@ -103,9 +103,7 @@ export async function postJudge(rdoc: RecordDoc) {
contest: { $ne: new ObjectID('0'.repeat(24)) },
}).project({ _id: 1, contest: 1 }).toArray();
const priority = await record.submissionPriority(rdoc.uid, -5000 - rdocs.length * 5 - 50);
await Promise.all(rdocs.map(
(r) => record.judge(rdoc.domainId, r._id, priority, r.contest ? { detail: false } : {}, { hackRejudge: input }),
));
await record.judge(rdoc.domainId, rdocs.map((r) => r._id), priority, {}, { hackRejudge: input });
} catch (e) {
next({
rid: rdoc._id, domainId: rdoc.domainId, key: 'next', message: { message: 'Unable to apply hack: {0}', params: [e.message] },
@@ -202,10 +200,11 @@ class JudgeConnectionHandler extends ConnectionHandler {
// eslint-disable-next-line no-await-in-loop
if (!t) await sleep(500);
}
this.send({ task: t });
let rdoc = await record.get(t.domainId, t.rid);
this.send({ task: { ...rdoc, ...t } });
this.processing = t;
const $set = { status: builtin.STATUS.STATUS_FETCHED };
const rdoc = await record.update(t.domainId, t.rid, $set, {});
rdoc = await record.update(t.domainId, t.rid, $set, {});
bus.broadcast('record/change', rdoc, $set, {});
}
+5 -5
View File
@@ -439,17 +439,17 @@ export class ProblemDetailHandler extends ContestDetailBaseHandler {
@param('pid', Types.UnsignedInt)
async postRejudge(domainId: string, pid: number) {
this.checkPerm(PERM.PERM_REJUDGE_PROBLEM);
// TODO maybe async?
const rdocs = await record.getMulti(domainId, {
pid,
contest: { $ne: new ObjectID('0'.repeat(24)) },
'files.hack': { $exists: false },
}).project({ _id: 1, contest: 1 }).toArray();
const priority = await record.submissionPriority(this.user._id, -10000 - rdocs.length * 5 - 50);
await Promise.all(rdocs.map(
(doc) => record.reset(domainId, doc._id, true)
.then(() => record.judge(domainId, doc._id, priority, doc.contest ? { detail: false } : {})),
));
await record.reset(domainId, rdocs.map((rdoc) => rdoc._id), true);
await Promise.all([
record.judge(domainId, rdocs.filter((i) => i.contest).map((i) => i._id), priority, { detail: false }),
record.judge(domainId, rdocs.filter((i) => !i.contest).map((i) => i._id), priority, {}),
]);
this.back();
}
+1 -4
View File
@@ -98,10 +98,7 @@ class RecordListHandler extends Handler {
filterStatus: status,
};
if (this.user.hasPriv(PRIV.PRIV_VIEW_JUDGE_STATISTICS) && !full) {
this.response.body.statistics = {
...await record.stat(all ? undefined : domainId),
delay: await TaskModel.getDelay({ type: 'judge' }),
};
this.response.body.statistics = await record.stat(all ? undefined : domainId);
}
}
}
+2 -2
View File
@@ -16,9 +16,9 @@ import domain from '../model/domain';
import oauth from '../model/oauth';
import * as oplog from '../model/oplog';
import problem, { ProblemDoc } from '../model/problem';
import ScheduleModel from '../model/schedule';
import SolutionModel from '../model/solution';
import * as system from '../model/system';
import task from '../model/task';
import token from '../model/token';
import user from '../model/user';
import {
@@ -376,7 +376,7 @@ class UserDetailHandler extends Handler {
class UserDeleteHandler extends Handler {
async post({ password }) {
this.user.checkPassword(password);
const tid = await task.add({
const tid = await ScheduleModel.add({
executeAfter: moment().add(7, 'days').toDate(),
type: 'script',
id: 'deleteUser',