Commit ed95cf01 authored by nanahira's avatar nanahira

use service to reload database

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