Commit ed95cf01 authored by nanahira's avatar nanahira

use service to reload database

parent af6e963d
......@@ -24,7 +24,7 @@
"koishi-nestjs": "^6.0.16",
"koishi-plugin-adapter-wechat-official": "^1.0.2",
"koishi-plugin-puppeteer": "^3.3.1",
"koishi-plugin-ygocard": "^10.4.7",
"koishi-plugin-ygocard": "^10.4.8",
"nicot": "^1.0.17",
"pg": "^8.8.0",
"pg-native": "^3.0.1",
......@@ -6939,9 +6939,9 @@
}
},
"node_modules/koishi-plugin-ygocard": {
"version": "10.4.7",
"resolved": "https://registry.npmjs.org/koishi-plugin-ygocard/-/koishi-plugin-ygocard-10.4.7.tgz",
"integrity": "sha512-M8k93wBWJQNkMtcb4wZGh1vE8m987b2Y/UsYROyACkbIyxDW+dQ+EGMFZluM+6zS8SYfGB36B50YrwgfSYsziQ==",
"version": "10.4.8",
"resolved": "https://registry.npmjs.org/koishi-plugin-ygocard/-/koishi-plugin-ygocard-10.4.8.tgz",
"integrity": "sha512-idUYanIULdLU/61Rf1B0iu6iD9geOYfrjgPb2vKSYD+4CHM8z4Rw3UuP5yCAZ6TJ69wRNqiEsw5aEkwF+oVY9A==",
"dependencies": {
"class-transformer": "^0.5.1",
"koishi-thirdeye": "^11.1.21",
......@@ -15610,9 +15610,9 @@
}
},
"koishi-plugin-ygocard": {
"version": "10.4.7",
"resolved": "https://registry.npmjs.org/koishi-plugin-ygocard/-/koishi-plugin-ygocard-10.4.7.tgz",
"integrity": "sha512-M8k93wBWJQNkMtcb4wZGh1vE8m987b2Y/UsYROyACkbIyxDW+dQ+EGMFZluM+6zS8SYfGB36B50YrwgfSYsziQ==",
"version": "10.4.8",
"resolved": "https://registry.npmjs.org/koishi-plugin-ygocard/-/koishi-plugin-ygocard-10.4.8.tgz",
"integrity": "sha512-idUYanIULdLU/61Rf1B0iu6iD9geOYfrjgPb2vKSYD+4CHM8z4Rw3UuP5yCAZ6TJ69wRNqiEsw5aEkwF+oVY9A==",
"requires": {
"class-transformer": "^0.5.1",
"koishi-thirdeye": "^11.1.21",
......
import { ConsoleLogger, Injectable, OnModuleInit } from '@nestjs/common';
import { InjectContext } from 'koishi-nestjs';
import { ConsoleLogger, Injectable } from '@nestjs/common';
import {
InjectContext,
PluginDef,
UseCommand,
UsePlugin,
WireContextService,
} from 'koishi-nestjs';
import { ConfigService } from '@nestjs/config';
import path from 'path';
import fs from 'fs';
import simpleGit, { ResetMode, SimpleGit } from 'simple-git';
import YGOCardPlugin from 'koishi-plugin-ygocard';
import { Cron } from '@nestjs/schedule';
import { Context, ForkScope } from 'koishi';
import { Context } from 'koishi';
async function exists(path: string) {
try {
......@@ -18,7 +24,7 @@ async function exists(path: string) {
}
@Injectable()
export class CdbLoaderService extends ConsoleLogger implements OnModuleInit {
export class CdbLoaderService extends ConsoleLogger {
private repoUrl = this.config.get<string>('CDB_REPO');
private repoPath = path.join(process.cwd(), 'ygopro-database');
private repoBranch = this.config.get<string>('CDB_BRANCH');
......@@ -43,11 +49,10 @@ export class CdbLoaderService extends ConsoleLogger implements OnModuleInit {
this.git = simpleGit(this.repoPath);
}
private state: ForkScope;
async onModuleInit() {
@UsePlugin()
async load() {
await this.initRepo();
this.state = this.ctx.plugin(YGOCardPlugin, {
return PluginDef(YGOCardPlugin, {
databasePaths: [path.join(this.repoPath, 'locales', 'zh-CN')],
matchCount: 20,
lang: 'cn',
......@@ -55,14 +60,23 @@ export class CdbLoaderService extends ConsoleLogger implements OnModuleInit {
});
}
@WireContextService()
private ygocard: YGOCardPlugin;
@Cron('0 0 3 * * *')
async updateRepo() {
this.log('Updating repo');
await this.git.fetch(['origin', this.repoBranch]);
await this.git.reset(ResetMode.HARD, ['origin/' + this.repoBranch]);
if (this.state) {
this.state.restart();
if (this.ygocard) {
await this.ygocard.reloadDatabase();
}
this.log('Repo updated');
}
/*@UseCommand('reload-cdb')
async reloadRepo() {
await this.updateRepo();
return 'success';
}*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment