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