Skip to content

Commit

Permalink
自定义Server支持“StopServerArgs”
Browse files Browse the repository at this point in the history
  • Loading branch information
xianyunleo committed Apr 22, 2024
1 parent 878dc03 commit a12b7df
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions src/main/core/ServerControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,33 @@ import Path from "@/main/utils/Path";
import FileUtil from "@/main/utils/FileUtil";

export default class ServerControl {

static WinPHPCGI_ProcessName = 'php-cgi.exe';
static WinPHPFPM_ProcessName = 'php-cgi-spawner.exe';


/**
* SoftwareItem
* @param softItem {SoftwareItem}
* @param item {SoftwareItem}
* @returns {Promise<void>}
*/
static async start(softItem) {
const item = softItem;
let workPath = Software.getPath(item); //服务目录
let serverProcessPath = Path.Join(workPath, item.ServerProcessPath); //服务的进程目录
let options = {cwd: workPath, detached: true};
static async start(item) {
const workPath = Software.getPath(item) //服务目录
const serverProcessPath = Path.Join(workPath, item.ServerProcessPath) //服务进程的目录
const options = { cwd: workPath, detached: true }

if (isWindows && item.ShellServerProcess) {
options = { cwd: workPath, shell: true } //使用shell,childProcess返回的pid是shell的pid
if (item.ShellServerProcess) {
options.shell = true //使用shell,childProcess返回的pid是shell的pid
}

if (!await FileUtil.Exists(serverProcessPath)) {
throw new Error(`${serverProcessPath} 文件不存在!`);
}

let args = [];

let args = []
if (item.StartServerArgs) {
args = item.StartServerArgs.map(arg => {
let argObj = {
ServerProcessPath: serverProcessPath,
WorkPath: workPath,
ConfPath: item.ConfPath ? Path.Join(workPath, item.ConfPath) : null,
ServerConfPath: item.ServerConfPath ? Path.Join(workPath, item.ServerConfPath) : null,
ExtraProcessPath: item.ExtraProcessPath ? Path.Join(workPath, item.ExtraProcessPath) : null,
};
return parseTemplateStrings(arg, argObj);
})
args = this.parseServerArgs(item, item.StartServerArgs)
}

item.isRunning = true;
item.errMsg = '';
item.isRunning = true
item.errMsg = ''

let childProcess = child_process.spawn(serverProcessPath, args, options);
const childProcess = child_process.spawn(serverProcessPath, args, options);

childProcess.stderr.on('data', (data) => {
console.log('stderr data',data?.toString())
Expand All @@ -68,12 +52,41 @@ export default class ServerControl {
}

/**
* SoftwareItem
* @param softItem{SoftwareItem}
*
* @param item{SoftwareItem}
* @returns {Promise<void>}
*/
static async stop(softItem) {
await ProcessExtend.kill(softItem.pid);
static async stop(item) {
const workPath = Software.getPath(item)
const options = { cwd: workPath, detached: true }
if (item.StopServerArgs) {
const args = this.parseServerArgs(item, item.StopServerArgs)
if (item.ShellServerProcess) {
options.shell = true //使用shell,childProcess返回的pid是shell的pid
}
const serverProcessPath = Path.Join(workPath, item.ServerProcessPath)
child_process.spawn(serverProcessPath, args, options)
} else {
await ProcessExtend.kill(item.pid)
}
}

/**
*
* @param item{SoftwareItem}
* @param args{array}
*/
static parseServerArgs(item, args) {
const workPath = Software.getPath(item)
return args.map((arg) => {
const argObj = {
WorkPath: workPath,
ServerProcessPath: Path.Join(workPath, item.ServerProcessPath),
ConfPath: item.ConfPath ? Path.Join(workPath, item.ConfPath).replaceSlash() : null,
ServerConfPath: item.ServerConfPath ? Path.Join(workPath, item.ServerConfPath).replaceSlash() : null,
ExtraProcessPath: item.ExtraProcessPath ? Path.Join(workPath, item.ExtraProcessPath).replaceSlash() : null
}
return parseTemplateStrings(arg, argObj)
})
}
}

0 comments on commit a12b7df

Please sign in to comment.