2023-09-06 03:00:17 +08:00
|
|
|
|
import cfg from "./config.js"
|
|
|
|
|
import common from "../common/common.js"
|
|
|
|
|
import { createClient } from "redis"
|
|
|
|
|
import { exec } from "node:child_process"
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化全局redis客户端
|
|
|
|
|
*/
|
2023-09-06 03:00:17 +08:00
|
|
|
|
export default async function redisInit() {
|
2023-03-11 09:35:53 +08:00
|
|
|
|
const rc = cfg.redis
|
2023-09-06 03:00:17 +08:00
|
|
|
|
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}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
let client = createClient({ url: redisUrl })
|
|
|
|
|
|
|
|
|
|
try {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
logger.info(`正在连接 ${logger.blue(redisUrl)}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
await client.connect()
|
2023-09-06 03:00:17 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Redis 错误:${logger.red(err)}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const cmd = "redis-server --save 900 1 --save 300 10 --daemonize yes" + await aarch64()
|
|
|
|
|
logger.info("正在启动 Redis...")
|
|
|
|
|
await execSync(cmd)
|
|
|
|
|
await common.sleep(1000)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
try {
|
|
|
|
|
client = createClient({ url: redisUrl })
|
|
|
|
|
await client.connect()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Redis 错误:${logger.red(err)}`)
|
|
|
|
|
logger.error(`请先启动 Redis:${logger.blue(cmd)}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
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}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
process.exit()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/** 全局变量 redis */
|
|
|
|
|
global.redis = client
|
2023-09-06 03:00:17 +08:00
|
|
|
|
logger.info("Redis 连接成功")
|
2023-03-04 14:30:13 +08:00
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
async function aarch64() {
|
|
|
|
|
if (process.platform == "win32")
|
|
|
|
|
return ""
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 判断arch */
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const arch = await execSync("uname -m")
|
|
|
|
|
if (arch.stdout && arch.stdout.includes("aarch64")) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 判断redis版本 */
|
2023-09-06 03:00:17 +08:00
|
|
|
|
let v = await execSync("redis-server -v")
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (v.stdout) {
|
|
|
|
|
v = v.stdout.match(/v=(\d)./)
|
|
|
|
|
/** 忽略arm警告 */
|
2023-09-06 03:00:17 +08:00
|
|
|
|
if (v && v[1] >= 6)
|
|
|
|
|
return " --ignore-warnings ARM64-COW-BUG"
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 03:00:17 +08:00
|
|
|
|
return ""
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
function execSync (cmd) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
exec(cmd, (error, stdout, stderr) => {
|
|
|
|
|
resolve({ error, stdout, stderr })
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-09-06 03:00:17 +08:00
|
|
|
|
}
|