add check port and change process

This commit is contained in:
2025-03-10 12:14:12 +08:00
parent b966ea68f2
commit 2e1cf531cf
13 changed files with 307 additions and 204 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/assistant-module",
"version": "0.0.3",
"version": "0.0.4-beta.2",
"description": "assistant module",
"main": "dist/assistant-module.mjs",
"types": "dist/assistant-module.d.ts",

View File

@@ -0,0 +1,18 @@
import { createCert } from '@kevisual/router/sign';
import { writeFileSync } from 'fs';
import path from 'path';
const pemDir = path.join(process.cwd(), 'router-app', 'pem');
const { key, cert } = createCert([
{
name: 'commonName',
value: 'localhost',
},
{
name: 'organizationName',
value: 'kevisual',
},
]);
writeFileSync(path.join(pemDir, 'https-key.pem'), key);
writeFileSync(path.join(pemDir, 'https-cert.pem'), cert);

View File

@@ -1,32 +1,40 @@
import { ChildProcess, fork } from 'child_process';
export const runProcess = (appPath: string) => {
const process = fork(appPath);
process.on('exit', (code) => {
console.log(`Process exited with code ${code}`);
});
process.on('message', (message) => {
console.log('Message from child:', message);
});
// Example of sending a message to the child process
// process.send({ hello: 'world' });
};
import { ChildProcess, fork, ForkOptions } from 'child_process';
class BaseProcess {
private process: ChildProcess;
status: 'running' | 'stopped' | 'error' = 'stopped';
appPath: string;
constructor(appPath: string) {
args: any[] = [];
opts: ForkOptions = {};
/*
* 重启次数, 默认0 TODO, 错误检测,当重启次数超过一定次数,则认为进程已崩溃
*/
restartCount: number = 0;
constructor(appPath?: string, args?: any[], opts?: ForkOptions) {
this.appPath = appPath;
this.args = args || [];
this.opts = opts || {};
// this.createProcess(appPath);
}
createProcess(appPath: string = this.appPath) {
createProcess(appPath: string = this.appPath, args: any[] = [], opts: ForkOptions = {}) {
if (this.process) {
this.process.kill();
}
this.appPath = appPath;
this.process = fork(appPath);
this.appPath = appPath || this.appPath;
this.args = args || this.args;
this.opts = {
...this.opts,
...opts,
};
this.process = fork(appPath, args, {
stdio: 'inherit',
...this.opts,
env: {
...process.env,
NODE_ENV_PARENT: 'fork',
...this.opts?.env,
},
});
return this;
}
kill(signal?: NodeJS.Signals | number) {
@@ -62,6 +70,10 @@ class BaseProcess {
public onDisconnect(callback: () => void) {
this.process.on('disconnect', callback);
}
restart() {
this.kill();
this.createProcess();
}
}
export class AssistantProcess extends BaseProcess {
constructor(appPath: string) {

View File

@@ -14,11 +14,11 @@ export const defaultApiProxy = [
];
/**
* 创建api代理
* @param api
* @param api
* @param paths ['/api/router', '/v1' ]
* @returns
* @returns
*/
export const createApiProxy = (api: string, paths: string[] = ['/api/router', '/v1']) => {
export const createApiProxy = (api: string, paths: string[] = ['/api/router', '/v1', '/resources']) => {
const pathList = paths.map((item) => {
return {
path: item,