core: fix web script execution callback

This commit is contained in:
undefined committed 2025-07-18 09:30:52 +08:00
1 parent 7c38cc6be8
commit 43114ccf86
4 files changed
+19 -27

No files matched your search

+4 -4
View File
@@ -77,10 +77,10 @@ export class JudgeResultCallbackContext {
private finishPromise: Promise<any>;
private operationPromise = Promise.resolve(null);
private relatedId = new ObjectId();
private meta: JudgeMeta;
private meta: { rejudge?: JudgeMeta['rejudge'] };
constructor(public ctx: Context, public readonly task: Omit<Task, '_id'> & { type: string }) { // eslint-disable-line @typescript-eslint/no-shadow
this.meta = task.meta as JudgeMeta;
this.meta = task.meta as JudgeMeta || {};
this.finishPromise = new Promise((resolve) => {
this.resolve = resolve;
});
@@ -90,7 +90,7 @@ export class JudgeResultCallbackContext {
const {
$set, $push, $unset, $inc,
} = processPayload(body);
if (this.meta.rejudge === 'controlled') {
if (this.meta?.rejudge === 'controlled') {
await record.collHistory.updateOne({
_id: this.relatedId,
}, {
@@ -143,7 +143,7 @@ export class JudgeResultCallbackContext {
$set.judgeAt = new Date();
$set.judger = body.judger ?? 1;
if (this.meta.rejudge === 'controlled') {
if (this.meta?.rejudge === 'controlled') {
await record.collHistory.updateOne({
_id: this.relatedId,
}, {
+13 -21
View File
@@ -16,7 +16,7 @@ import user from '../model/user';
import {
ConnectionHandler, Handler, param, requireSudo, Types,
} from '../service/server';
import * as judge from './judge';
import { JudgeResultCallbackContext } from './judge';
const logger = new Logger('manage');
@@ -104,33 +104,25 @@ class SystemScriptHandler extends SystemHandler {
args = global.Hydro.script[id].validate(args);
}
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: raw, type: 'pretest' });
const report = (data) => judge.next({ domainId, rid, ...data });
report({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
const c = new JudgeResultCallbackContext(this.ctx, { type: 'judge', domainId, rid });
c.next({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
const start = Date.now();
// Maybe async?
global.Hydro.script[id].run(args, report)
.then((ret: any) => {
const time = new Date().getTime() - start;
judge.end({
domainId,
rid: rid.toHexString(),
status: STATUS.STATUS_ACCEPTED,
message: inspect(ret, false, 10, true),
judger: 1,
time,
memory: 0,
});
})
global.Hydro.script[id].run(args, (data) => c.next(data))
.then((ret: any) => c.end({
status: STATUS.STATUS_ACCEPTED,
message: inspect(ret, false, 10, true),
judger: 1,
time: Date.now() - start,
memory: 0,
}))
.catch((err: Error) => {
const time = new Date().getTime() - start;
logger.error(err);
judge.end({
domainId,
rid: rid.toHexString(),
c.end({
status: STATUS.STATUS_SYSTEM_ERROR,
message: `${err.message} \n${(err as any).params || []} \n${err.stack} `,
judger: 1,
time,
time: Date.now() - start,
memory: 0,
});
});
+1 -1
View File
@@ -160,7 +160,7 @@ class RecordDetailHandler extends ContestDetailBaseHandler {
const allRev = await record.collHistory.find({ rid }).project({ _id: 1, judgeAt: 1 }).sort({ _id: -1 }).toArray();
const allRevs: Record<string, Date> = Object.fromEntries(allRev.map((i) => [i._id.toString(), i.judgeAt]));
if (rev && allRevs[rev.toString()]) {
rdoc = { ...rdoc, ...omit(await record.collHistory.findOne({ _id: rev }), ['_id']) };
rdoc = { ...rdoc, ...omit(await record.collHistory.findOne({ _id: rev }), ['_id']), progress: null };
}
let canViewDetail = true;
if (rdoc.contest?.toString().startsWith('0'.repeat(23))) {
+1 -1
View File
@@ -59,7 +59,7 @@ export class MongoService extends Service {
const mongourl = await MongoService.getUrl();
const url = mongoUri.parse(mongourl);
this.client = await MongoClient.connect(mongourl);
yield () => { this.client.close(); };
yield () => this.client.close();
this.db = this.client.db(url.database || 'hydro');
await bus.parallel('database/connect', this.db);
yield this.ctx.interval(() => this.fixExpireAfter(), Time.hour);