Skip to content

Commit

Permalink
fix: 关闭、启动 过快
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Aug 18, 2024
1 parent cacc669 commit 00c5fa7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/core/process/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('检测到后台进程 正在关闭'))
Expand Down
13 changes: 11 additions & 2 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
/** 正常启动的进程 */
Expand Down
1 change: 0 additions & 1 deletion src/db/level/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ export default class LevelDB extends Level {
}

export const level = new LevelDB()
level.open()
23 changes: 12 additions & 11 deletions src/db/redis/redis_level.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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) // 通过代理的方式删除键值对
}
}
Expand Down Expand Up @@ -60,6 +59,8 @@ export default class RedisLevel {
}

this.#expireMap = new Proxy(this.#expireMap, handler)
/** 延迟2秒执行 */
setTimeout(async () => this.level.open(), 2000)
}

/**
Expand All @@ -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
Expand All @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand All @@ -114,7 +115,7 @@ export default class RedisLevel {
async keys (prefix = ''): Promise<string[]> {
/** 去掉末尾的* */
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
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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)
}

/**
Expand Down

1 comment on commit 00c5fa7

@sj817
Copy link
Contributor Author

@sj817 sj817 commented on 00c5fa7 Aug 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.