mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 01:38:19 +08:00
core: optimize database operation
This commit is contained in:
1 parent
7a164483ba
commit
0726dba492
3 files changed
+18
-2
No files matched your search
@@ -74,5 +74,5 @@ export async function apply(ctx: Context) {
|
||||
ctx.Route('status', '/status', StatusHandler);
|
||||
ctx.Route('status_admin', '/.status', AdminStatusHandler);
|
||||
ctx.Route('status_update', '/status/update', StatusUpdateHandler);
|
||||
await db.ensureIndexes(coll, { key: { updateAt: 1 }, expireAfterSeconds: 24 * 2600 });
|
||||
await db.ensureIndexes(coll, { name: 'expire', key: { updateAt: 1 }, expireAfterSeconds: 24 * 2600 });
|
||||
}
|
||||
@@ -130,7 +130,7 @@ export async function apply(ctx: Context) {
|
||||
while (true) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const res = await collEvent.findOneAndUpdate(
|
||||
{ ack: { $nin: [id] } },
|
||||
{ expire: { $gt: new Date() }, ack: { $nin: [id] } },
|
||||
{ $push: { ack: id } },
|
||||
);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import {
|
||||
Collection, Db, IndexSpecification, MongoClient,
|
||||
} from 'mongodb';
|
||||
import { Time } from '@hydrooj/utils';
|
||||
import { Logger } from '../logger';
|
||||
import options from '../options';
|
||||
import * as bus from './bus';
|
||||
@@ -51,6 +52,7 @@ class MongoService {
|
||||
});
|
||||
this.db = this.client.db(opts.name || 'hydro');
|
||||
await bus.parallel('database/connect', this.db);
|
||||
setInterval(() => this.fixExpireAfter(), Time.hour);
|
||||
}
|
||||
|
||||
public collection<K extends keyof Collections>(c: K): Collection<Collections[K]> {
|
||||
@@ -58,6 +60,20 @@ class MongoService {
|
||||
return this.db.collection(c);
|
||||
}
|
||||
|
||||
public async fixExpireAfter() {
|
||||
// Sometimes mongo's expireAfterSeconds is not working in non-replica set mode;
|
||||
const collections = await this.db.listCollections().toArray();
|
||||
for (const c of collections) {
|
||||
const coll = this.db.collection(c.name);
|
||||
const indexes = await coll.listIndexes().toArray();
|
||||
for (const i of indexes) {
|
||||
if (!i.expireAfterSeconds) continue;
|
||||
const key = Object.keys(i.key)[0];
|
||||
await coll.deleteMany({ [key]: { $lt: new Date(Date.now() - i.expireAfterSeconds * 1000) } });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async ensureIndexes<T>(coll: Collection<T>, ...args: IndexSpecification[]) {
|
||||
if (process.env.NODE_APP_INSTANCE !== '0') return;
|
||||
let existed: any[];
|
||||
|
||||
Reference in new issue
Block a user