Skip to content

Commit

Permalink
feat: 企业版新增运行节点功能
Browse files Browse the repository at this point in the history
  • Loading branch information
song-xiao-lin committed Nov 13, 2023
1 parent 5665f58 commit 2945373
Show file tree
Hide file tree
Showing 8 changed files with 1,026 additions and 199 deletions.
111 changes: 88 additions & 23 deletions app/main/handlers/engineStatus.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const {ipcMain} = require("electron")
const { ipcMain } = require("electron")
const childProcess = require("child_process")
const path = require("path")
const os = require("os")
const _sudoPrompt = require("sudo-prompt")
const {GLOBAL_YAK_SETTING} = require("../state")
const {testRemoteClient} = require("../ipc")
const {getLocalYaklangEngine, engineLog} = require("../filePath")
const { GLOBAL_YAK_SETTING } = require("../state")
const { testRemoteClient } = require("../ipc")
const { getLocalYaklangEngine, engineLog } = require("../filePath")
const psList = require("./libs/ps-yak-process")
const net = require("net");
const fs = require("fs")

Expand All @@ -19,7 +20,7 @@ fs.open(engineLog, "a", (err, fd) => {
}
})

let dbFile = undefined
let dbFile = undefined

/** 本地引擎随机端口启动重试次数(防止无限制的随机重试,最大重试次数: 5) */
let engineCount = 0
Expand Down Expand Up @@ -59,11 +60,11 @@ function generateWindowsSudoCommand(file, args) {
/** @name 以管理员权限执行命令 */
function sudoExec(cmd, opt, callback) {
if (isWindows) {
childProcess.exec(cmd, {maxBuffer: 1000 * 1000 * 1000}, (err, stdout, stderr) => {
childProcess.exec(cmd, { maxBuffer: 1000 * 1000 * 1000 }, (err, stdout, stderr) => {
callback(err)
})
} else {
_sudoPrompt.exec(cmd, {...opt, env: {YAKIT_HOME: path.join(os.homedir(), "yakit-projects/")}}, callback)
_sudoPrompt.exec(cmd, { ...opt, env: { YAKIT_HOME: path.join(os.homedir(), "yakit-projects/") } }, callback)
}
}

Expand Down Expand Up @@ -114,7 +115,7 @@ module.exports = (win, callback, getClient, newClient) => {
ipcMain.handle("engine-status", () => {
try {
const text = "hello yak grpc engine"
getClient().Echo({text}, (err, data) => {
getClient().Echo({ text }, (err, data) => {
if (win) {
if (data?.result === text) {
win.webContents.send("client-engine-status-ok")
Expand Down Expand Up @@ -165,14 +166,14 @@ module.exports = (win, callback, getClient, newClient) => {
const asyncStartLocalYakEngineServer = (win, params) => {
engineCount += 1

const {sudo, port} = params
const { sudo, port } = params
return new Promise((resolve, reject) => {
try {
// 考虑如果管理员权限启动未成功该通过什么方式自启普通权限引擎进程
if (sudo) {
if (isWindows) {
const subprocess = childProcess.exec(
generateWindowsSudoCommand(getLocalYaklangEngine(), `grpc --port ${port}${dbFile?" --profile-db "+dbFile:""}`),
generateWindowsSudoCommand(getLocalYaklangEngine(), `grpc --port ${port}${dbFile ? " --profile-db " + dbFile : ""}`),
{
maxBuffer: 1000 * 1000 * 1000,
stdio: "pipe",
Expand All @@ -189,7 +190,7 @@ module.exports = (win, callback, getClient, newClient) => {
})
resolve()
} else {
const cmd = `${getLocalYaklangEngine()} grpc --port ${port}${dbFile?` --profile-db ${dbFile}`:""}`
const cmd = `${getLocalYaklangEngine()} grpc --port ${port}${dbFile ? ` --profile-db ${dbFile}` : ""}`
sudoExec(
cmd,
{
Expand All @@ -209,14 +210,14 @@ module.exports = (win, callback, getClient, newClient) => {
const log = out ? out : "ignore"

const grpcPort = ["grpc", "--port", `${port}`]
const extraParams = dbFile?[...grpcPort,"--profile-db", dbFile]:grpcPort
const extraParams = dbFile ? [...grpcPort, "--profile-db", dbFile] : grpcPort

const subprocess = childProcess.spawn(getLocalYaklangEngine(), extraParams, {
// stdio: ["ignore", "ignore", "ignore"]
detached: false, windowsHide: true,
stdio: ["ignore", log, log]
})

// subprocess.unref()
process.on("exit", () => {
// 终止子进程
Expand All @@ -228,17 +229,17 @@ module.exports = (win, callback, getClient, newClient) => {
})
subprocess.on("close", async (e) => {
toLog(`本地引擎退出,退出码为:${e}`)
fs.readFile(engineLog,(err,data) => {
if(err){
console.log("读取引擎文件失败",err);
}else{
fs.readFile(engineLog, (err, data) => {
if (err) {
console.log("读取引擎文件失败", err);
} else {
toStdout(data)
setTimeout(() => {
try {
fs.unlinkSync(engineLog)
} catch (e) {
console.info(`unlinkSync 'engine.log' local cache failed: ${e}`, e)
}
try {
fs.unlinkSync(engineLog)
} catch (e) {
console.info(`unlinkSync 'engine.log' local cache failed: ${e}`, e)
}
}, 1000);
}
})
Expand Down Expand Up @@ -314,7 +315,7 @@ module.exports = (win, callback, getClient, newClient) => {
params["Password"] || "",
)
return (await (new Promise((resolve, reject) => {
newClient().Echo({text: ECHO_TEST_MSG}, (err, data) => {
newClient().Echo({ text: ECHO_TEST_MSG }, (err, data) => {
if (err) {
reject(err)
return
Expand All @@ -336,4 +337,68 @@ module.exports = (win, callback, getClient, newClient) => {
ipcMain.handle("output-log-to-welcome-console", (e, msg) => {
toLog(`${msg}`)
})

/** 调用命令生成运行节点 */
ipcMain.handle("call-command-generate-node", (e, params) => {
return new Promise((resolve, reject) => {
// 若命令行运行了节点 node环境再起相同节点需要单独判断
const cmd = `mq --server ${params.ipOrdomain} --server-port ${params.port} --id ${params.nodename}`
psList().then((data) => {
const yakArr = data.filter((i) => (i.name || "").includes("yak"))
yakArr.forEach(async i => {
if (isWindows) {
childProcess.exec(`wmic process where ProcessId=${i.pid} get CommandLine`, (error, stdout, stderr) => {
if (stdout.indexOf(cmd) > -1) {
reject("存在相同节点运行")
}
})
} else {
childProcess.exec(`ps -p ${i.pid} -o cmd`, (error, stdout, stderr) => {
if (stdout.indexOf(cmd) > -1) {
reject("存在相同节点运行")
}
})
}
})
})

// 运行节点
const subprocess = childProcess.spawn(getLocalYaklangEngine(), ['mq', '--server', params.ipOrdomain, '--server-port', params.port, '--id', params.nodename])
subprocess.stdout.on('data', (data) => {
resolve(subprocess.pid)
})
subprocess.on('error', (error) => {
reject(error)
})
subprocess.stderr.on('data', (data) => {
reject(data)
})
})
})
/** 删除运行节点 */
ipcMain.handle("kill-run-node", (e, params) => {
return new Promise((resolve, reject) => {
if (isWindows) {
childProcess.exec(`taskkill /F /PID ${params.pid}`, (error, stdout, stderr) => {
if (error) {
reject(error)
}
if (stderr) {
reject(stderr)
}
resolve("")
})
} else {
childProcess.exec(`kill -9 ${params.pid}`, (error, stdout, stderr) => {
if (error) {
reject(error)
}
if (stderr) {
reject(stderr)
}
resolve("")
})
}
})
})
}
Loading

0 comments on commit 2945373

Please sign in to comment.