Skip to content

Commit 43114cc

Browse files
committed
core: fix web script execution callback
1 parent 7c38cc6 commit 43114cc

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

packages/hydrooj/src/handler/judge.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ export class JudgeResultCallbackContext {
7777
private finishPromise: Promise<any>;
7878
private operationPromise = Promise.resolve(null);
7979
private relatedId = new ObjectId();
80-
private meta: JudgeMeta;
80+
private meta: { rejudge?: JudgeMeta['rejudge'] };
8181

8282
constructor(public ctx: Context, public readonly task: Omit<Task, '_id'> & { type: string }) { // eslint-disable-line @typescript-eslint/no-shadow
83-
this.meta = task.meta as JudgeMeta;
83+
this.meta = task.meta as JudgeMeta || {};
8484
this.finishPromise = new Promise((resolve) => {
8585
this.resolve = resolve;
8686
});
@@ -90,7 +90,7 @@ export class JudgeResultCallbackContext {
9090
const {
9191
$set, $push, $unset, $inc,
9292
} = processPayload(body);
93-
if (this.meta.rejudge === 'controlled') {
93+
if (this.meta?.rejudge === 'controlled') {
9494
await record.collHistory.updateOne({
9595
_id: this.relatedId,
9696
}, {
@@ -143,7 +143,7 @@ export class JudgeResultCallbackContext {
143143
$set.judgeAt = new Date();
144144
$set.judger = body.judger ?? 1;
145145

146-
if (this.meta.rejudge === 'controlled') {
146+
if (this.meta?.rejudge === 'controlled') {
147147
await record.collHistory.updateOne({
148148
_id: this.relatedId,
149149
}, {

packages/hydrooj/src/handler/manage.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import user from '../model/user';
1616
import {
1717
ConnectionHandler, Handler, param, requireSudo, Types,
1818
} from '../service/server';
19-
import * as judge from './judge';
19+
import { JudgeResultCallbackContext } from './judge';
2020

2121
const logger = new Logger('manage');
2222

@@ -104,33 +104,25 @@ class SystemScriptHandler extends SystemHandler {
104104
args = global.Hydro.script[id].validate(args);
105105
}
106106
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: raw, type: 'pretest' });
107-
const report = (data) => judge.next({ domainId, rid, ...data });
108-
report({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
107+
const c = new JudgeResultCallbackContext(this.ctx, { type: 'judge', domainId, rid });
108+
c.next({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
109109
const start = Date.now();
110110
// Maybe async?
111-
global.Hydro.script[id].run(args, report)
112-
.then((ret: any) => {
113-
const time = new Date().getTime() - start;
114-
judge.end({
115-
domainId,
116-
rid: rid.toHexString(),
117-
status: STATUS.STATUS_ACCEPTED,
118-
message: inspect(ret, false, 10, true),
119-
judger: 1,
120-
time,
121-
memory: 0,
122-
});
123-
})
111+
global.Hydro.script[id].run(args, (data) => c.next(data))
112+
.then((ret: any) => c.end({
113+
status: STATUS.STATUS_ACCEPTED,
114+
message: inspect(ret, false, 10, true),
115+
judger: 1,
116+
time: Date.now() - start,
117+
memory: 0,
118+
}))
124119
.catch((err: Error) => {
125-
const time = new Date().getTime() - start;
126120
logger.error(err);
127-
judge.end({
128-
domainId,
129-
rid: rid.toHexString(),
121+
c.end({
130122
status: STATUS.STATUS_SYSTEM_ERROR,
131123
message: `${err.message} \n${(err as any).params || []} \n${err.stack} `,
132124
judger: 1,
133-
time,
125+
time: Date.now() - start,
134126
memory: 0,
135127
});
136128
});

packages/hydrooj/src/handler/record.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class RecordDetailHandler extends ContestDetailBaseHandler {
160160
const allRev = await record.collHistory.find({ rid }).project({ _id: 1, judgeAt: 1 }).sort({ _id: -1 }).toArray();
161161
const allRevs: Record<string, Date> = Object.fromEntries(allRev.map((i) => [i._id.toString(), i.judgeAt]));
162162
if (rev && allRevs[rev.toString()]) {
163-
rdoc = { ...rdoc, ...omit(await record.collHistory.findOne({ _id: rev }), ['_id']) };
163+
rdoc = { ...rdoc, ...omit(await record.collHistory.findOne({ _id: rev }), ['_id']), progress: null };
164164
}
165165
let canViewDetail = true;
166166
if (rdoc.contest?.toString().startsWith('0'.repeat(23))) {

packages/hydrooj/src/service/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MongoService extends Service {
5959
const mongourl = await MongoService.getUrl();
6060
const url = mongoUri.parse(mongourl);
6161
this.client = await MongoClient.connect(mongourl);
62-
yield () => { this.client.close(); };
62+
yield () => this.client.close();
6363
this.db = this.client.db(url.database || 'hydro');
6464
await bus.parallel('database/connect', this.db);
6565
yield this.ctx.interval(() => this.fixExpireAfter(), Time.hour);

0 commit comments

Comments
 (0)