修复 Redis 连接失败
This commit is contained in:
parent
2a6b5cb21f
commit
c47e5a6519
|
@ -1,121 +1,76 @@
|
|||
import cfg from './config.js'
|
||||
import common from '../common/common.js'
|
||||
import { createClient } from 'redis'
|
||||
import { exec } from 'node:child_process'
|
||||
import cfg from "./config.js"
|
||||
import common from "../common/common.js"
|
||||
import { createClient } from "redis"
|
||||
import { exec } from "node:child_process"
|
||||
|
||||
/**
|
||||
* 初始化全局redis客户端
|
||||
*/
|
||||
export default async function redisInit () {
|
||||
|
||||
export default async function redisInit() {
|
||||
const rc = cfg.redis
|
||||
let redisUn = rc.username || ''
|
||||
let redisPw = rc.password ? `:${rc.password}` : ''
|
||||
if (rc.username || rc.password) redisPw += '@'
|
||||
let redisUrl = `redis://${redisUn}${redisPw}${rc.host}:${rc.port}/${rc.db}`
|
||||
|
||||
// 初始化reids
|
||||
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.mark(`正在连接 Redis...`)
|
||||
logger.mark(redisUrl)
|
||||
|
||||
logger.mark(`正在连接 ${logger.blue(redisUrl)}`)
|
||||
await client.connect()
|
||||
} catch (error) {
|
||||
let err = error.toString()
|
||||
} catch (err) {
|
||||
logger.error(`Redis 错误:${logger.red(err)}`)
|
||||
|
||||
if (err != 'Error: connect ECONNREFUSED 127.0.0.1:6379') {
|
||||
logger.error('连接 Redis 失败!')
|
||||
process.exit()
|
||||
}
|
||||
const cmd = "redis-server --save 900 1 --save 300 10 --daemonize yes" + await aarch64()
|
||||
logger.mark("正在启动 Redis...")
|
||||
await execSync(cmd)
|
||||
await common.sleep(1000)
|
||||
|
||||
/** windows */
|
||||
if (process.platform == 'win32') {
|
||||
logger.error('请先启动 Redis')
|
||||
logger.error('Window 系统:双击 redis-server.exe 启动')
|
||||
try {
|
||||
client = createClient({ url: redisUrl })
|
||||
await client.connect()
|
||||
} catch (err) {
|
||||
logger.error(`Redis 错误:${logger.red(err)}`)
|
||||
logger.error(`请先启动 Redis:${logger.blue(cmd)}`)
|
||||
process.exit()
|
||||
} else {
|
||||
let cmd = 'redis-server --save 900 1 --save 300 10 --daemonize yes'
|
||||
let arm = await aarch64()
|
||||
/** 安卓端自动启动redis */
|
||||
if (arm) {
|
||||
client = await startRedis(`${cmd}${arm}`, client, redisUrl)
|
||||
} else {
|
||||
logger.error('请先启动 Redis')
|
||||
logger.error(`Redis 启动命令:${cmd} ${arm}`)
|
||||
process.exit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.on('error', async (err) => {
|
||||
let log = { error: (log) => console.log(log) }
|
||||
if (typeof logger != 'undefined') log = logger
|
||||
if (err == 'Error: connect ECONNREFUSED 127.0.0.1:6379') {
|
||||
if (process.platform == 'win32') {
|
||||
log.error('请先启动 Redis')
|
||||
log.error('Window 系统:双击 redis-server.exe 启动')
|
||||
} else {
|
||||
let cmd = 'redis-server --save 900 1 --save 300 10 --daemonize yes'
|
||||
let arm = await aarch64()
|
||||
log.error('请先启动 Redis')
|
||||
log.error(`Redis 启动命令:${cmd} ${arm}`)
|
||||
}
|
||||
} else {
|
||||
log.error(`Redis 错误:${err}`)
|
||||
}
|
||||
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 */
|
||||
global.redis = client
|
||||
|
||||
logger.mark('Redis 连接成功')
|
||||
|
||||
logger.mark("Redis 连接成功")
|
||||
return client
|
||||
}
|
||||
|
||||
async function aarch64 () {
|
||||
let tips = ''
|
||||
async function aarch64() {
|
||||
if (process.platform == "win32")
|
||||
return ""
|
||||
/** 判断arch */
|
||||
let arch = await execSync('arch')
|
||||
if (arch.stdout && arch.stdout.includes('aarch64')) {
|
||||
const arch = await execSync("uname -m")
|
||||
if (arch.stdout && arch.stdout.includes("aarch64")) {
|
||||
/** 判断redis版本 */
|
||||
let v = await execSync('redis-server -v')
|
||||
let v = await execSync("redis-server -v")
|
||||
if (v.stdout) {
|
||||
v = v.stdout.match(/v=(\d)./)
|
||||
/** 忽略arm警告 */
|
||||
if (v && v[1] >= 6) tips = ' --ignore-warnings ARM64-COW-BUG'
|
||||
if (v && v[1] >= 6)
|
||||
return " --ignore-warnings ARM64-COW-BUG"
|
||||
}
|
||||
}
|
||||
tips = ' --ignore-warnings ARM64-COW-BUG'
|
||||
return tips
|
||||
return ""
|
||||
}
|
||||
|
||||
/** 尝试自动启动redis */
|
||||
async function startRedis (cmd, client, redisUrl) {
|
||||
logger.mark('正在启动 Redis...')
|
||||
await execSync(cmd)
|
||||
await common.sleep(500)
|
||||
try {
|
||||
/** 重新链接 */
|
||||
client = createClient({ url: redisUrl })
|
||||
await client.connect()
|
||||
} catch (error) {
|
||||
let err = error.toString()
|
||||
logger.mark(err)
|
||||
logger.error('请先启动 Redis')
|
||||
logger.error(`Redis 启动命令:${cmd}`)
|
||||
process.exit()
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
async function execSync (cmd) {
|
||||
function execSync (cmd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(cmd, (error, stdout, stderr) => {
|
||||
resolve({ error, stdout, stderr })
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue