core: experimental hot reload support

This commit is contained in:
undefined committed 2022-09-20 22:34:36 +08:00
1 parent 632ae27b88
commit fd7c9cbeb1
26 files changed
+429 -148

No files matched your search

+30 -27
View File
@@ -1,6 +1,6 @@
import { Client } from '@elastic/elasticsearch';
import { omit } from 'lodash';
import { addScript, Schema } from 'hydrooj';
import { Schema } from 'hydrooj';
import DomainModel from 'hydrooj/src/model/domain';
import ProblemModel, { ProblemDoc } from 'hydrooj/src/model/problem';
import * as system from 'hydrooj/src/model/system';
@@ -24,27 +24,6 @@ const processDocument = (doc: Partial<ProblemDoc>) => {
return omit(doc, indexOmit);
};
bus.on('problem/add', async (doc, docId) => {
await client.index({
index: 'problem',
id: `${doc.domainId}/${docId}`,
document: processDocument(doc),
});
});
bus.on('problem/edit', async (pdoc) => {
await client.index({
index: 'problem',
id: `${pdoc.domainId}/${pdoc.docId}`,
document: processDocument(pdoc),
});
});
bus.on('problem/del', async (domainId, docId) => {
await client.delete({
index: 'problem',
id: `${domainId}/${docId}`,
});
});
global.Hydro.lib.problemSearch = async (domainId, q, opts) => {
const allowedSize = system.get('elasic-search.indexSize') || 10000;
const size = opts?.limit || system.get('pagination.problem');
@@ -98,8 +77,32 @@ async function run({ domainId }, report) {
return true;
}
addScript('ensureElasticSearch', 'Elastic problem search re-index')
.args(Schema.object({
domainId: Schema.string(),
}))
.action(run);
export const apply = (ctx) => {
bus.on('problem/add', async (doc, docId) => {
await client.index({
index: 'problem',
id: `${doc.domainId}/${docId}`,
document: processDocument(doc),
});
});
bus.on('problem/edit', async (pdoc) => {
await client.index({
index: 'problem',
id: `${pdoc.domainId}/${pdoc.docId}`,
document: processDocument(pdoc),
});
});
bus.on('problem/del', async (domainId, docId) => {
await client.delete({
index: 'problem',
id: `${domainId}/${docId}`,
});
});
ctx.addScript(
'ensureElasticSearch', 'Elastic problem search re-index',
Schema.object({
domainId: Schema.string(),
}),
run,
);
};