chore: 从配置文件获取默认restart port端口

This commit is contained in:
千奈千祁 2024-04-14 10:00:13 +08:00
parent d2e2aadcc7
commit b84dbbcacc
2 changed files with 25 additions and 5 deletions

10
ksr.js
View File

@ -1,6 +1,8 @@
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import log4js from 'log4js'; import log4js from 'log4js';
import http from 'http' import http from 'http'
import YAML from 'yaml'
import fs from 'fs'
/* keep ssh run */ /* keep ssh run */
@ -47,5 +49,11 @@ const serverHttpexit = http.createServer(async (req, res) => {
res.end('Not Found\n'); res.end('Not Found\n');
} }
}) })
let restart_port
try {
restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`))
restart_port = restart_port.restart_port || 27881
} catch {}
serverHttpexit.listen(27881, () => { }); logger.info(`restart_api run on port ${restart_port || 27881}`)
serverHttpexit.listen(restart_port || 27881, () => { });

View File

@ -2,6 +2,8 @@ import plugin from '../../lib/plugins/plugin.js'
import { createRequire } from 'module' import { createRequire } from 'module'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import net from 'net' import net from 'net'
import fs from 'fs'
import YAML from 'yaml'
const require = createRequire(import.meta.url) const require = createRequire(import.meta.url)
const { exec } = require('child_process') const { exec } = require('child_process')
@ -62,6 +64,11 @@ export class Restart extends plugin {
} }
async restart () { async restart () {
let restart_port
try {
restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`))
restart_port = restart_port.restart_port || 27881
} catch { }
await this.e.reply('开始执行重启,请稍等...') await this.e.reply('开始执行重启,请稍等...')
logger.mark(`${this.e.logFnc} 开始执行重启,请稍等...`) logger.mark(`${this.e.logFnc} 开始执行重启,请稍等...`)
@ -74,9 +81,9 @@ export class Restart extends plugin {
let npm = await this.checkPnpm() let npm = await this.checkPnpm()
await redis.set(this.key, data, { EX: 120 }) await redis.set(this.key, data, { EX: 120 })
if(await isPortTaken(27881)) { if(await isPortTaken(restart_port || 27881)) {
try { try {
let result = await fetch(`http://localhost:27881/restart`) let result = await fetch(`http://localhost:${restart_port || 27881}/restart`)
result = await result.text() result = await result.text()
if(result !== `OK`) { if(result !== `OK`) {
redis.del(this.key) redis.del(this.key)
@ -132,11 +139,16 @@ export class Restart extends plugin {
} }
async stop () { async stop () {
if(await isPortTaken(27881)) { let restart_port
try {
restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`))
restart_port = restart_port.restart_port || 27881
} catch { }
if(await isPortTaken(restart_port || 27881)) {
try { try {
logger.mark('关机成功,已停止运行') logger.mark('关机成功,已停止运行')
await this.e.reply(`关机成功,已停止运行`) await this.e.reply(`关机成功,已停止运行`)
await fetch(`http://localhost:27881/exit`) await fetch(`http://localhost:${restart_port || 27881}/exit`)
return return
} catch(error) { } catch(error) {
this.e.reply(`操作失败!\n${error}`) this.e.reply(`操作失败!\n${error}`)