2024-06-09 01:00:07 +08:00
|
|
|
|
import cfg from "./config.js"
|
2024-06-09 20:35:52 +08:00
|
|
|
|
import { execAsync, sleep } from "../utils/common.js"
|
2024-06-09 01:00:07 +08:00
|
|
|
|
import { createClient } from "redis"
|
|
|
|
|
import { exec } from "node:child_process"
|
|
|
|
|
|
2024-06-09 11:40:24 +08:00
|
|
|
|
|
2024-06-09 01:00:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* 初始化全局redis客户端
|
2024-06-09 11:40:24 +08:00
|
|
|
|
* @returns
|
2024-06-09 01:00:07 +08:00
|
|
|
|
*/
|
|
|
|
|
export default async function redisInit() {
|
|
|
|
|
const rc = cfg.redis
|
|
|
|
|
const redisUn = rc.username || ""
|
|
|
|
|
let redisPw = rc.password ? `:${rc.password}` : ""
|
|
|
|
|
if (rc.username || rc.password)
|
|
|
|
|
redisPw += "@"
|
|
|
|
|
const redisUrl = `redis://${redisUn}${redisPw}${rc.host}:${rc.port}/${rc.db}`
|
|
|
|
|
let client = createClient({ url: redisUrl })
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
logger.info(`正在连接 ${logger.blue(redisUrl)}`)
|
|
|
|
|
await client.connect()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Redis 错误:${logger.red(err)}`)
|
|
|
|
|
|
|
|
|
|
const cmd = "redis-server --save 900 1 --save 300 10 --daemonize yes" + await aarch64()
|
|
|
|
|
logger.info("正在启动 Redis...")
|
|
|
|
|
await execSync(cmd)
|
2024-06-09 18:21:33 +08:00
|
|
|
|
await sleep(1000)
|
2024-06-09 01:00:07 +08:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
client = createClient({ url: redisUrl })
|
|
|
|
|
await client.connect()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Redis 错误:${logger.red(err)}`)
|
|
|
|
|
logger.error(`请先启动 Redis:${logger.blue(cmd)}`)
|
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.on("error", async err => {
|
|
|
|
|
logger.error(`Redis 错误:${logger.red(err)}`)
|
|
|
|
|
const cmd = "redis-server --save 900 1 --save 300 10 --daemonize yes" + await aarch64()
|
|
|
|
|
logger.error(`请先启动 Redis:${cmd}`)
|
|
|
|
|
process.exit()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/** 全局变量 redis */
|
2024-06-09 20:35:52 +08:00
|
|
|
|
global.redis = client as any
|
|
|
|
|
|
2024-06-09 01:00:07 +08:00
|
|
|
|
logger.info("Redis 连接成功")
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 11:40:24 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2024-06-09 18:21:33 +08:00
|
|
|
|
export async function aarch64() {
|
2024-06-09 20:35:52 +08:00
|
|
|
|
if (process.platform == "win32"){
|
2024-06-09 01:00:07 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
2024-06-09 20:35:52 +08:00
|
|
|
|
return await execAsync("uname -m").then( async arch=>{
|
|
|
|
|
if (arch.stdout && arch.stdout.includes("aarch64")) {
|
|
|
|
|
/** 判断redis版本 */
|
|
|
|
|
let v = await execAsync("redis-server -v")
|
|
|
|
|
if (v.stdout) {
|
|
|
|
|
const data = v.stdout.match(/v=(\d)./)
|
|
|
|
|
/** 忽略arm警告 */
|
|
|
|
|
if (data && Number(data[1]) >= 6){
|
|
|
|
|
return " --ignore-warnings ARM64-COW-BUG"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ''
|
|
|
|
|
})
|
2024-06-09 01:00:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 11:40:24 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param cmd
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2024-06-09 18:21:33 +08:00
|
|
|
|
export function execSync(cmd) {
|
2024-06-09 01:00:07 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
exec(cmd, (error, stdout, stderr) => {
|
|
|
|
|
resolve({ error, stdout, stderr })
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|