From 00c5fa7fd40e36f864685e968ec25a70c1adbf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E7=91=BE?= Date: Sun, 18 Aug 2024 17:24:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=B3=E9=97=AD=E3=80=81=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=20=E8=BF=87=E5=BF=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/process/process.ts | 2 +- src/core/server/server.ts | 13 +++++++++++-- src/db/level/level.ts | 1 - src/db/redis/redis_level.ts | 23 ++++++++++++----------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/core/process/process.ts b/src/core/process/process.ts index 32cf720c..bcc422e8 100644 --- a/src/core/process/process.ts +++ b/src/core/process/process.ts @@ -56,7 +56,7 @@ export default class Process { /** * 使用api来检查后台 */ - const res = await common.axios(host + '/ping', 'get', { timeout: 100 }) + const res = await common.axios(host + '/ping', 'get', { timeout: 2000 }) if (!res) return this logger.mark(logger.red('检测到后台进程 正在关闭')) diff --git a/src/core/server/server.ts b/src/core/server/server.ts index bf803c45..b5edbe66 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -1,8 +1,9 @@ import fs from 'fs' -import Process from '../process/process' +import { level } from 'karin/db' import { WebSocketServer } from 'ws' import { createServer } from 'http' import { karin } from '../karin/karin' +import Process from '../process/process' import express, { Express } from 'express' import { exec, config, logger, common } from 'karin/utils' import { AdapterOneBot11 } from 'karin/adapter/onebot/11/index' @@ -32,7 +33,7 @@ export const server = new (class Server { try { /** 防止多进程端口冲突 启动失败 */ await Process.check() - + level.open() this.WebSocketServer.on('connection', (socket, request) => { const path = request.url const headers = request.headers @@ -80,6 +81,14 @@ export const server = new (class Server { /** 关闭服务器 */ karin.emit('exit.grpc') this.server.close() + try { + await level.close() + const redis = (await import('karin/db')).redis as any + if (redis && redis.id === 'RedisLevel') await redis.level.close() + } catch (error: any) { + logger.error('[服务器][HTTP] 关闭数据库失败') + logger.error(error) + } /** 如果是pm2 获取当前pm2ID 使用 */ if (process.env.pm_id) await exec(`pm2 delete ${process.env.pm_id}`) /** 正常启动的进程 */ diff --git a/src/db/level/level.ts b/src/db/level/level.ts index 9d4cecea..9152d944 100644 --- a/src/db/level/level.ts +++ b/src/db/level/level.ts @@ -38,4 +38,3 @@ export default class LevelDB extends Level { } export const level = new LevelDB() -level.open() diff --git a/src/db/redis/redis_level.ts b/src/db/redis/redis_level.ts index 7b9926bc..81c3989d 100644 --- a/src/db/redis/redis_level.ts +++ b/src/db/redis/redis_level.ts @@ -1,15 +1,14 @@ import { Level } from 'level' export default class RedisLevel { - #level + level /** 过期时间映射表 */ #expireMap /** 唯一标识符 用于区分不同的数据库 */ id: string constructor () { const path = process.cwd() + '/data/db/RedisLevel' - this.#level = new Level(path, { valueEncoding: 'json' }) - this.#level.open() + this.level = new Level(path, { valueEncoding: 'json' }) this.id = 'RedisLevel' this.#expireMap = new Map() this.#expireHandle() @@ -25,7 +24,7 @@ export default class RedisLevel { const entries = Array.from(this.#expireMap.entries()) for (const [key, expire] of entries) { if (expire < now) { - await this.#level.del(key) + await this.level.del(key) this.#expireMap.delete(key) // 通过代理的方式删除键值对 } } @@ -60,6 +59,8 @@ export default class RedisLevel { } this.#expireMap = new Proxy(this.#expireMap, handler) + /** 延迟2秒执行 */ + setTimeout(async () => this.level.open(), 2000) } /** @@ -72,12 +73,12 @@ export default class RedisLevel { /** 先查过期时间 */ const expire = this.#expireMap.get(key) if (expire && expire < Date.now()) { - await this.#level.del(key) + await this.level.del(key) this.#expireMap.delete(key) return null } - return await this.#level.get(key) + return await this.level.get(key) } catch (error: any) { if (error.notFound) return null throw error @@ -95,7 +96,7 @@ export default class RedisLevel { this.#expireMap.set(key, Date.now() + options.EX * 1000) } - return await this.#level.put(key, value) + return await this.level.put(key, value) } /** @@ -104,7 +105,7 @@ export default class RedisLevel { */ async del (key: string) { this.#expireMap.delete(key) - return await this.#level.del(key) + return await this.level.del(key) } /** @@ -114,7 +115,7 @@ export default class RedisLevel { async keys (prefix = ''): Promise { /** 去掉末尾的* */ prefix = prefix.replace(/\*$/, '') - const list = await this.#level.keys({ gte: prefix, lt: `${prefix}\xFF` }).all() + const list = await this.level.keys({ gte: prefix, lt: `${prefix}\xFF` }).all() this.#checkKeys(list) return list } @@ -127,7 +128,7 @@ export default class RedisLevel { for (const key of keys) { const expire = this.#expireMap.get(key) if (expire && expire < Date.now()) { - await this.#level.del(key) + await this.level.del(key) this.#expireMap.delete(key) } } @@ -202,7 +203,7 @@ export default class RedisLevel { */ async setEx (key: string, seconds: number, value: string) { this.#expireMap.set(key, Date.now() + seconds * 1000) - return await this.#level.put(key, value) + return await this.level.put(key, value) } /**