mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix web script execution callback
This commit is contained in:
4 files changed
+19
-27
No files matched your search
@@ -77,10 +77,10 @@ export class JudgeResultCallbackContext {
|
|||||||
private finishPromise: Promise<any>;
|
private finishPromise: Promise<any>;
|
||||||
private operationPromise = Promise.resolve(null);
|
private operationPromise = Promise.resolve(null);
|
||||||
private relatedId = new ObjectId();
|
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
|
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.finishPromise = new Promise((resolve) => {
|
||||||
this.resolve = resolve;
|
this.resolve = resolve;
|
||||||
});
|
});
|
||||||
@@ -90,7 +90,7 @@ export class JudgeResultCallbackContext {
|
|||||||
const {
|
const {
|
||||||
$set, $push, $unset, $inc,
|
$set, $push, $unset, $inc,
|
||||||
} = processPayload(body);
|
} = processPayload(body);
|
||||||
if (this.meta.rejudge === 'controlled') {
|
if (this.meta?.rejudge === 'controlled') {
|
||||||
await record.collHistory.updateOne({
|
await record.collHistory.updateOne({
|
||||||
_id: this.relatedId,
|
_id: this.relatedId,
|
||||||
}, {
|
}, {
|
||||||
@@ -143,7 +143,7 @@ export class JudgeResultCallbackContext {
|
|||||||
$set.judgeAt = new Date();
|
$set.judgeAt = new Date();
|
||||||
$set.judger = body.judger ?? 1;
|
$set.judger = body.judger ?? 1;
|
||||||
|
|
||||||
if (this.meta.rejudge === 'controlled') {
|
if (this.meta?.rejudge === 'controlled') {
|
||||||
await record.collHistory.updateOne({
|
await record.collHistory.updateOne({
|
||||||
_id: this.relatedId,
|
_id: this.relatedId,
|
||||||
}, {
|
}, {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import user from '../model/user';
|
|||||||
import {
|
import {
|
||||||
ConnectionHandler, Handler, param, requireSudo, Types,
|
ConnectionHandler, Handler, param, requireSudo, Types,
|
||||||
} from '../service/server';
|
} from '../service/server';
|
||||||
import * as judge from './judge';
|
import { JudgeResultCallbackContext } from './judge';
|
||||||
|
|
||||||
const logger = new Logger('manage');
|
const logger = new Logger('manage');
|
||||||
|
|
||||||
@@ -104,33 +104,25 @@ class SystemScriptHandler extends SystemHandler {
|
|||||||
args = global.Hydro.script[id].validate(args);
|
args = global.Hydro.script[id].validate(args);
|
||||||
}
|
}
|
||||||
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: raw, type: 'pretest' });
|
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: raw, type: 'pretest' });
|
||||||
const report = (data) => judge.next({ domainId, rid, ...data });
|
const c = new JudgeResultCallbackContext(this.ctx, { type: 'judge', domainId, rid });
|
||||||
report({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
|
c.next({ message: `Running script: ${id} `, status: STATUS.STATUS_JUDGING });
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
// Maybe async?
|
// Maybe async?
|
||||||
global.Hydro.script[id].run(args, report)
|
global.Hydro.script[id].run(args, (data) => c.next(data))
|
||||||
.then((ret: any) => {
|
.then((ret: any) => c.end({
|
||||||
const time = new Date().getTime() - start;
|
status: STATUS.STATUS_ACCEPTED,
|
||||||
judge.end({
|
message: inspect(ret, false, 10, true),
|
||||||
domainId,
|
judger: 1,
|
||||||
rid: rid.toHexString(),
|
time: Date.now() - start,
|
||||||
status: STATUS.STATUS_ACCEPTED,
|
memory: 0,
|
||||||
message: inspect(ret, false, 10, true),
|
}))
|
||||||
judger: 1,
|
|
||||||
time,
|
|
||||||
memory: 0,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((err: Error) => {
|
.catch((err: Error) => {
|
||||||
const time = new Date().getTime() - start;
|
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
judge.end({
|
c.end({
|
||||||
domainId,
|
|
||||||
rid: rid.toHexString(),
|
|
||||||
status: STATUS.STATUS_SYSTEM_ERROR,
|
status: STATUS.STATUS_SYSTEM_ERROR,
|
||||||
message: `${err.message} \n${(err as any).params || []} \n${err.stack} `,
|
message: `${err.message} \n${(err as any).params || []} \n${err.stack} `,
|
||||||
judger: 1,
|
judger: 1,
|
||||||
time,
|
time: Date.now() - start,
|
||||||
memory: 0,
|
memory: 0,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 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]));
|
const allRevs: Record<string, Date> = Object.fromEntries(allRev.map((i) => [i._id.toString(), i.judgeAt]));
|
||||||
if (rev && allRevs[rev.toString()]) {
|
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;
|
let canViewDetail = true;
|
||||||
if (rdoc.contest?.toString().startsWith('0'.repeat(23))) {
|
if (rdoc.contest?.toString().startsWith('0'.repeat(23))) {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export class MongoService extends Service {
|
|||||||
const mongourl = await MongoService.getUrl();
|
const mongourl = await MongoService.getUrl();
|
||||||
const url = mongoUri.parse(mongourl);
|
const url = mongoUri.parse(mongourl);
|
||||||
this.client = await MongoClient.connect(mongourl);
|
this.client = await MongoClient.connect(mongourl);
|
||||||
yield () => { this.client.close(); };
|
yield () => this.client.close();
|
||||||
this.db = this.client.db(url.database || 'hydro');
|
this.db = this.client.db(url.database || 'hydro');
|
||||||
await bus.parallel('database/connect', this.db);
|
await bus.parallel('database/connect', this.db);
|
||||||
yield this.ctx.interval(() => this.fixExpireAfter(), Time.hour);
|
yield this.ctx.interval(() => this.fixExpireAfter(), Time.hour);
|
||||||
|
|||||||
Reference in new issue
Block a user