mirror of
https://github.com/wallace5303/ee-core.git
synced 2026-04-05 07:29:04 +08:00
[feat] [ee-bin] The problem of residual optimization process
This commit is contained in:
@@ -5,36 +5,7 @@ const { move } = require('./tools/move');
|
||||
const { encrypt, cleanEncrypt } = require('./tools/encrypt');
|
||||
const { serveProcess } = require('./tools/serve');
|
||||
const { incrUpdater } = require('./tools/incrUpdater');
|
||||
const chalk = require("chalk");
|
||||
const process = require("process");
|
||||
const kill = require("tree-kill");
|
||||
|
||||
/**
|
||||
* resource process
|
||||
*/
|
||||
const resource = {
|
||||
processList: [],
|
||||
addProcess(execProcess) {
|
||||
const keys = Object.keys(execProcess);
|
||||
const len = keys.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const key = keys[i];
|
||||
const p = execProcess[key];
|
||||
this.processList.push({
|
||||
name: key,
|
||||
pid: p.pid,
|
||||
});
|
||||
}
|
||||
},
|
||||
close() {
|
||||
this.processList.forEach((p) => {
|
||||
kill(p.pid);
|
||||
console.log(chalk.red("[ee-bin] ") + `Kill [${chalk.blue(p.name)}] server. pid: ${chalk.green(p.pid)}`);
|
||||
});
|
||||
this.processList = [];
|
||||
process.exit(0);
|
||||
},
|
||||
};
|
||||
/**
|
||||
* dev
|
||||
*/
|
||||
@@ -45,7 +16,6 @@ program
|
||||
.option('--serve <mode>', 'serve mode')
|
||||
.action(function() {
|
||||
serveProcess.dev(this.opts());
|
||||
resource.addProcess(serveProcess.execProcess);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -59,7 +29,6 @@ program
|
||||
.option('--env <env>', 'environment')
|
||||
.action(function() {
|
||||
serveProcess.build(this.opts());
|
||||
resource.addProcess(serveProcess.execProcess);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -71,7 +40,6 @@ program
|
||||
.option('--config <folder>', 'config file')
|
||||
.action(function() {
|
||||
serveProcess.start(this.opts());
|
||||
resource.addProcess(serveProcess.execProcess);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -84,7 +52,6 @@ program
|
||||
.option('--cmds <flag>', 'custom commands')
|
||||
.action(function() {
|
||||
serveProcess.exec(this.opts());
|
||||
resource.addProcess(serveProcess.execProcess);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -148,16 +115,4 @@ program
|
||||
incrUpdater.run(this.opts());
|
||||
});
|
||||
|
||||
// 监听 SIGINT 信号(Ctrl + C)
|
||||
process.on('SIGINT', () => {
|
||||
console.log('Received SIGINT. Closing resources...');
|
||||
resource.close();
|
||||
});
|
||||
|
||||
// 监听 SIGTERM 信号
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('Received SIGTERM. Closing resources...');
|
||||
resource.close();
|
||||
});
|
||||
|
||||
program.parse();
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ee-bin",
|
||||
"version": "4.1.4",
|
||||
"version": "4.1.5",
|
||||
"description": "ee bin",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -10,6 +10,7 @@ const crossSpawn = require('cross-spawn');
|
||||
const { buildSync } = require('esbuild');
|
||||
const chokidar = require('chokidar');
|
||||
const kill = require('tree-kill');
|
||||
const process = require("process");
|
||||
|
||||
class ServeProcess {
|
||||
|
||||
@@ -18,6 +19,48 @@ class ServeProcess {
|
||||
this.execProcess = {};
|
||||
this.electronDir = './electron';
|
||||
this.defaultBundleDir = './public/electron';
|
||||
this._init();
|
||||
}
|
||||
|
||||
/**
|
||||
* init
|
||||
*/
|
||||
_init() {
|
||||
// process manager
|
||||
// Monitor SIGINT signal(Ctrl + C)
|
||||
process.on('SIGINT', () => {
|
||||
console.log(chalk.blue('[ee-bin] ') + `Received SIGINT. Closing processes...`);
|
||||
this._closeProcess();
|
||||
});
|
||||
|
||||
// Monitor SIGTERM signal
|
||||
process.on('SIGTERM', () => {
|
||||
console.log(chalk.blue('[ee-bin] ') + `Received SIGTERM. Closing processes...`);
|
||||
this._closeProcess();
|
||||
});
|
||||
}
|
||||
|
||||
// Close process
|
||||
async _closeProcess() {
|
||||
const currentProcess = [];
|
||||
const keys = Object.keys(this.execProcess);
|
||||
const len = keys.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const key = keys[i];
|
||||
const p = this.execProcess[key];
|
||||
currentProcess.push({
|
||||
name: key,
|
||||
pid: p.pid,
|
||||
});
|
||||
}
|
||||
|
||||
// Cleaning work before the end of the process
|
||||
await this.sleep(1000);
|
||||
currentProcess.forEach((p) => {
|
||||
kill(p.pid);
|
||||
console.log(chalk.blue("[ee-bin] ") + `Kill [${chalk.blue(p.name)}] server. pid: ${chalk.green(p.pid)}`);
|
||||
});
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,8 +242,8 @@ class ServeProcess {
|
||||
|
||||
if(!cfg.sync) {
|
||||
this.execProcess[cmd].on('exit', () => {
|
||||
if (cmd == 'electron') {
|
||||
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('Press "CTRL+C" to exit'));
|
||||
if (binCmd == 'dev') {
|
||||
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`the ${cmd}`) + ' process is exiting');
|
||||
return
|
||||
}
|
||||
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ' command has been executed and exited');
|
||||
|
||||
Reference in New Issue
Block a user