2023-03-11 09:35:53 +08:00
|
|
|
import cfg from './config.js'
|
2023-03-04 14:30:13 +08:00
|
|
|
import common from '../common/common.js'
|
|
|
|
import { createClient } from 'redis'
|
2023-03-11 09:35:53 +08:00
|
|
|
import { exec } from 'node:child_process'
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 初始化全局redis客户端
|
|
|
|
*/
|
|
|
|
export default async function redisInit () {
|
|
|
|
|
2023-03-11 09:35:53 +08:00
|
|
|
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}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
// 初始化reids
|
|
|
|
let client = createClient({ url: redisUrl })
|
|
|
|
|
|
|
|
try {
|
2023-03-11 09:35:53 +08:00
|
|
|
logger.mark(`正在连接 Redis...`)
|
|
|
|
logger.mark(redisUrl)
|
|
|
|
|
2023-03-04 14:30:13 +08:00
|
|
|
await client.connect()
|
|
|
|
} catch (error) {
|
|
|
|
let err = error.toString()
|
|
|
|
|
|
|
|
if (err != 'Error: connect ECONNREFUSED 127.0.0.1:6379') {
|
2023-03-06 10:00:25 +08:00
|
|
|
logger.error('连接 Redis 失败!')
|
2023-03-04 14:30:13 +08:00
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
|
|
|
/** windows */
|
|
|
|
if (process.platform == 'win32') {
|
2023-03-06 10:00:25 +08:00
|
|
|
logger.error('请先启动 Redis')
|
|
|
|
logger.error('Window 系统:双击 redis-server.exe 启动')
|
2023-03-04 14:30:13 +08:00
|
|
|
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 {
|
2023-03-06 10:00:25 +08:00
|
|
|
logger.error('请先启动 Redis')
|
|
|
|
logger.error(`Redis 启动命令:${cmd} ${arm}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
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') {
|
2023-03-06 10:00:25 +08:00
|
|
|
log.error('请先启动 Redis')
|
|
|
|
log.error('Window 系统:双击 redis-server.exe 启动')
|
2023-03-04 14:30:13 +08:00
|
|
|
} else {
|
|
|
|
let cmd = 'redis-server --save 900 1 --save 300 10 --daemonize yes'
|
|
|
|
let arm = await aarch64()
|
2023-03-06 10:00:25 +08:00
|
|
|
log.error('请先启动 Redis')
|
|
|
|
log.error(`Redis 启动命令:${cmd} ${arm}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-06 10:00:25 +08:00
|
|
|
log.error(`Redis 错误:${err}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
|
|
|
/** 全局变量 redis */
|
|
|
|
global.redis = client
|
|
|
|
|
2023-03-06 10:00:25 +08:00
|
|
|
logger.mark('Redis 连接成功')
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
async function aarch64 () {
|
|
|
|
let tips = ''
|
|
|
|
/** 判断arch */
|
|
|
|
let arch = await execSync('arch')
|
|
|
|
if (arch.stdout && arch.stdout.includes('aarch64')) {
|
|
|
|
/** 判断redis版本 */
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tips = ' --ignore-warnings ARM64-COW-BUG'
|
|
|
|
return tips
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 尝试自动启动redis */
|
|
|
|
async function startRedis (cmd, client, redisUrl) {
|
2023-03-11 09:35:53 +08:00
|
|
|
logger.mark('正在启动 Redis...')
|
2023-03-04 14:30:13 +08:00
|
|
|
await execSync(cmd)
|
|
|
|
await common.sleep(500)
|
|
|
|
try {
|
|
|
|
/** 重新链接 */
|
|
|
|
client = createClient({ url: redisUrl })
|
|
|
|
await client.connect()
|
|
|
|
} catch (error) {
|
|
|
|
let err = error.toString()
|
|
|
|
logger.mark(err)
|
2023-03-06 10:00:25 +08:00
|
|
|
logger.error('请先启动 Redis')
|
|
|
|
logger.error(`Redis 启动命令:${cmd}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
async function execSync (cmd) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
exec(cmd, (error, stdout, stderr) => {
|
|
|
|
resolve({ error, stdout, stderr })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|