添加problem/[get list edit]等钩子,添加task.priority

This commit is contained in:
undefined committed 2020-09-15 23:12:38 +08:00
1 parent 7d25830ea3
commit 829cbac937
9 files changed
+48 -27

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.13.10", "version": "2.13.11",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+4 -3
View File
@@ -72,6 +72,7 @@ class ProblemMainHandler extends ProblemHandler {
path.push([q, null, null, true]); path.push([q, null, null, true]);
} }
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) query.hidden = false; if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) query.hidden = false;
await bus.serial('problem/list', query, this);
const [pdocs, ppcount, pcount] = await paginate( const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, query).sort({ pid: 1, docId: 1 }), problem.getMulti(domainId, query).sort({ pid: 1, docId: 1 }),
page, page,
@@ -116,6 +117,7 @@ class ProblemCategoryHandler extends ProblemHandler {
} }
let psdict = {}; let psdict = {};
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false; if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
await bus.serial('problem/list', q, this);
const [pdocs, ppcount, pcount] = await paginate( const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, q).sort({ pid: 1, docId: 1 }), problem.getMulti(domainId, q).sort({ pid: 1, docId: 1 }),
page, page,
@@ -152,6 +154,7 @@ class ProblemRandomHandler extends ProblemHandler {
} }
} }
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false; if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
await bus.serial('problem/list', q, this);
const pid = await problem.random(domainId, q); const pid = await problem.random(domainId, q);
if (!pid) throw new NoProblemError(); if (!pid) throw new NoProblemError();
this.response.body = { pid }; this.response.body = { pid };
@@ -172,6 +175,7 @@ class ProblemDetailHandler extends ProblemHandler {
if (this.pdoc.hidden && this.pdoc.owner !== this.user._id) { if (this.pdoc.hidden && this.pdoc.owner !== this.user._id) {
this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN); this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
} }
await bus.serial('problem/get', this.pdoc, this);
this.udoc = await user.getById(domainId, this.pdoc.owner); this.udoc = await user.getById(domainId, this.pdoc.owner);
this.response.body = { this.response.body = {
pdoc: this.pdoc, pdoc: this.pdoc,
@@ -185,9 +189,6 @@ class ProblemDetailHandler extends ProblemHandler {
}; };
} }
// eslint-disable-next-line
async get(...args: any[]) { }
@param('pid', Types.UnsignedInt) @param('pid', Types.UnsignedInt)
@param('dest', Types.String) @param('dest', Types.String)
@param('hidden', Types.Boolean) @param('hidden', Types.Boolean)
+2 -1
View File
@@ -435,7 +435,8 @@ export interface JudgeResultBody {
export interface Task { export interface Task {
_id: ObjectID, _id: ObjectID,
type: string, type: string,
executeAfter?: Date executeAfter?: Date,
priority: number,
[key: string]: any [key: string]: any
} }
+13 -9
View File
@@ -1,14 +1,18 @@
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { ObjectID } from 'mongodb'; import { FilterQuery, ObjectID } from 'mongodb';
import { Task } from '../interface';
import * as db from '../service/db'; import * as db from '../service/db';
const coll = db.collection('task'); const coll = db.collection('task');
export async function add(task: any) { export async function add(task: Partial<Task> & { type: string }) {
const t = { ...task }; const t: Task = {
if (typeof t.executeAfter === 'object') t.executeAfter = t.executeAfter.getTime(); ...task,
t.count = t.count || 1; count: task.count || 1,
t.executeAfter = t.executeAfter || new Date().getTime(); priority: task.priority || 1,
executeAfter: task.executeAfter || new Date(),
_id: new ObjectID(),
};
const res = await coll.insertOne(t); const res = await coll.insertOne(t);
return res.insertedId; return res.insertedId;
} }
@@ -17,7 +21,7 @@ export function get(_id: ObjectID) {
return coll.findOne({ _id }); return coll.findOne({ _id });
} }
export function count(query: any) { export function count(query: FilterQuery<Task>) {
return coll.find(query).count(); return coll.find(query).count();
} }
@@ -25,9 +29,9 @@ export function del(_id: ObjectID) {
return coll.deleteOne({ _id }); return coll.deleteOne({ _id });
} }
export async function getFirst(query: any) { export async function getFirst(query: FilterQuery<Task>) {
const q = { ...query }; const q = { ...query };
q.executeAfter = q.executeAfter || { $lt: new Date().getTime() }; q.executeAfter = q.executeAfter || { $lt: new Date() };
const res = await coll.findOneAndDelete(q); const res = await coll.findOneAndDelete(q);
if (res.value) { if (res.value) {
if (res.value.interval) { if (res.value.interval) {
+1 -2
View File
@@ -1,9 +1,8 @@
import { Collection } from 'mongodb';
import { TokenDoc } from '../interface'; import { TokenDoc } from '../interface';
import * as db from '../service/db'; import * as db from '../service/db';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
export const coll: Collection<TokenDoc> = db.collection('token'); export const coll = db.collection('token');
export const TYPE_SESSION = 0; export const TYPE_SESSION = 0;
export const TYPE_REGISTRATION = 2; export const TYPE_REGISTRATION = 2;
+1 -1
View File
@@ -8,7 +8,7 @@ export function getStatus(domainId: string, tid: ObjectID, uid: number) {
return document.getStatus(domainId, document.TYPE_TRAINING, tid, uid); return document.getStatus(domainId, document.TYPE_TRAINING, tid, uid);
} }
export function getMultiStatus(domainId: string, query: any) { export function getMultiStatus(domainId: string, query: FilterQuery<TrainingDoc>) {
return document.getMultiStatus(domainId, document.TYPE_TRAINING, query); return document.getMultiStatus(domainId, document.TYPE_TRAINING, query);
} }
+2
View File
@@ -102,6 +102,8 @@ export class User implements _User {
this[key] = setting.DOMAIN_USER_SETTINGS_BY_KEY[key].value; this[key] = setting.DOMAIN_USER_SETTINGS_BY_KEY[key].value;
} }
} }
bus.serial('user/get', this);
} }
hasPerm(...perm: bigint[]) { hasPerm(...perm: bigint[]) {
+15 -5
View File
@@ -1,8 +1,10 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
import cluster from 'cluster'; import cluster from 'cluster';
import { Db } from 'mongodb'; import { Db, FilterQuery } from 'mongodb';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { Mdoc, Rdoc, User } from '../interface'; import {
Mdoc, Pdoc, Rdoc, User,
} from '../interface';
type DocType = import('../model/document').DocType; type DocType = import('../model/document').DocType;
@@ -14,6 +16,7 @@ function isBailed(value: any) {
} }
export type Disposable = () => void export type Disposable = () => void
export type VoidReturn = Promise<void> | void
export interface EventMap { export interface EventMap {
'app/started': () => void 'app/started': () => void
@@ -22,11 +25,18 @@ export interface EventMap {
'database/connect': (db: Db) => void 'database/connect': (db: Db) => void
'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void 'monitor/update': (type: 'server' | 'judger', $set: any) => VoidReturn
'document/add': (doc: any) => Promise<void> | void 'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void
'user/get': (udoc: User) => void
'document/add': (doc: any) => VoidReturn
'document/set': <T extends keyof DocType> 'document/set': <T extends keyof DocType>
(domainId: string, docType: T, docId: DocType[T], $set: any) => Promise<void> | void (domainId: string, docType: T, docId: DocType[T], $set: any) => VoidReturn
'problem/edit': (doc: Pdoc) => VoidReturn
'problem/list': (query: FilterQuery<Pdoc>, handler: any) => VoidReturn
'problem/get': (doc: Pdoc, handler: any) => VoidReturn
'record/change': (rdoc: Rdoc, $set?: any, $push?: any) => void 'record/change': (rdoc: Rdoc, $set?: any, $push?: any) => void
} }
+9 -5
View File
@@ -7,19 +7,23 @@ const coll = db.collection('status');
export async function update() { export async function update() {
const [mid, $set] = await sysinfo.update(); const [mid, $set] = await sysinfo.update();
$set.updateAt = new Date();
$set.reqCount = global.Hydro.stat.reqCount;
await bus.serial('monitor/update', 'server', $set);
await coll.updateOne( await coll.updateOne(
{ mid, type: 'server' }, { mid, type: 'server' },
{ $set: { ...$set, updateAt: new Date(), reqCount: global.Hydro.stat.reqCount } }, { $set },
{ upsert: true }, { upsert: true },
); );
global.Hydro.stat.reqCount = 0; global.Hydro.stat.reqCount = 0;
} }
export function updateJudger(args) { export async function updateJudger(args) {
args.type = 'judger'; const $set = { ...args, updateAt: new Date() };
return coll.updateOne( await bus.serial('monitor/update', 'judger', $set);
return await coll.updateOne(
{ mid: args.mid, type: 'judger' }, { mid: args.mid, type: 'judger' },
{ $set: { ...args, updateAt: new Date() } }, { $set },
{ upsert: true }, { upsert: true },
); );
} }