ee-core 4.1.3 → 4.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +65 -65
- package/app/application.d.ts +5 -5
- package/app/application.js +30 -30
- package/app/boot.d.ts +5 -5
- package/app/boot.js +80 -80
- package/app/dir.d.ts +1 -1
- package/app/dir.js +26 -26
- package/app/events.d.ts +14 -14
- package/app/events.js +55 -55
- package/app/index.d.ts +2 -2
- package/app/index.js +6 -6
- package/config/config_loader.d.ts +13 -13
- package/config/config_loader.js +65 -65
- package/config/default_config.d.ts +95 -95
- package/config/default_config.js +111 -111
- package/config/index.d.ts +28 -28
- package/config/index.js +24 -24
- package/const/channel.d.ts +16 -16
- package/const/channel.js +27 -27
- package/controller/controller_loader.d.ts +9 -9
- package/controller/controller_loader.js +77 -77
- package/controller/index.d.ts +2 -2
- package/controller/index.js +24 -24
- package/core/index.d.ts +2 -2
- package/core/index.js +11 -11
- package/core/loader/file_loader.d.ts +66 -66
- package/core/loader/file_loader.js +211 -211
- package/core/utils/index.d.ts +12 -12
- package/core/utils/index.js +83 -83
- package/core/utils/timing.d.ts +22 -22
- package/core/utils/timing.js +78 -78
- package/cross/cross.d.ts +19 -19
- package/cross/cross.js +151 -151
- package/cross/crossProcess.d.ts +29 -29
- package/cross/crossProcess.js +172 -172
- package/cross/index.d.ts +2 -2
- package/cross/index.js +8 -8
- package/electron/app/index.d.ts +5 -5
- package/electron/app/index.js +48 -48
- package/electron/index.d.ts +3 -3
- package/electron/index.js +15 -15
- package/electron/window/index.d.ts +6 -6
- package/electron/window/index.js +268 -268
- package/exception/index.d.ts +9 -9
- package/exception/index.js +100 -100
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.d.ts +1 -1
- package/html/index.js +10 -10
- package/index.d.ts +2 -2
- package/index.js +6 -6
- package/jobs/child/app.d.ts +1 -1
- package/jobs/child/app.js +69 -69
- package/jobs/child/index.d.ts +13 -13
- package/jobs/child/index.js +85 -85
- package/jobs/child/jobProcess.d.ts +16 -16
- package/jobs/child/jobProcess.js +133 -133
- package/jobs/child-pool/index.d.ts +26 -26
- package/jobs/child-pool/index.js +190 -190
- package/jobs/index.d.ts +3 -3
- package/jobs/index.js +6 -6
- package/jobs/load-balancer/algorithm/index.d.ts +4 -4
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.d.ts +2 -2
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.d.ts +2 -2
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.d.ts +8 -8
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.d.ts +96 -96
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.d.ts +16 -16
- package/jobs/load-balancer/scheduler.js +31 -31
- package/loader/index.d.ts +9 -9
- package/loader/index.js +99 -98
- package/log/index.d.ts +5 -5
- package/log/index.js +85 -85
- package/log/logger.d.ts +1 -1
- package/log/logger.js +94 -94
- package/message/childMessage.d.ts +10 -10
- package/message/childMessage.js +43 -43
- package/message/index.d.ts +3 -3
- package/message/index.js +9 -9
- package/package.json +42 -42
- package/ps/index.d.ts +39 -39
- package/ps/index.js +294 -294
- package/socket/httpServer.d.ts +8 -8
- package/socket/httpServer.js +199 -199
- package/socket/index.d.ts +10 -10
- package/socket/index.js +64 -64
- package/socket/ipcServer.d.ts +9 -9
- package/socket/ipcServer.js +105 -105
- package/socket/socketServer.d.ts +10 -10
- package/socket/socketServer.js +81 -81
- package/storage/index.d.ts +4 -4
- package/storage/index.js +6 -6
- package/storage/sqliteStorage.d.ts +16 -16
- package/storage/sqliteStorage.js +123 -123
- package/tsconfig.json +13 -13
- package/utils/extend.d.ts +2 -2
- package/utils/extend.js +77 -77
- package/utils/helper.d.ts +14 -14
- package/utils/helper.js +206 -206
- package/utils/index.d.ts +10 -10
- package/utils/index.js +167 -167
- package/utils/ip.d.ts +2 -2
- package/utils/ip.js +221 -221
- package/utils/is.d.ts +14 -14
- package/utils/is.js +111 -111
- package/utils/json.d.ts +5 -5
- package/utils/json.js +79 -79
- package/utils/pargv.d.ts +3 -3
- package/utils/pargv.js +267 -267
- package/utils/port/index.d.ts +1 -1
- package/utils/port/index.js +125 -125
- package/utils/wrap.d.ts +4 -4
- package/utils/wrap.js +40 -40
package/jobs/child/app.js
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
const is = require('is-type-of');
|
|
3
|
-
const { loadException } = require('ee-core/exception');
|
|
4
|
-
const { requireFile } = require('ee-core/loader');
|
|
5
|
-
const { coreLogger } = require('ee-core/log');
|
|
6
|
-
const { isBytecodeClass } = require('ee-core/core/utils');
|
|
7
|
-
|
|
8
|
-
loadException();
|
|
9
|
-
const commands = ['run'];
|
|
10
|
-
|
|
11
|
-
class ChildApp {
|
|
12
|
-
constructor() {
|
|
13
|
-
this._initEvents();
|
|
14
|
-
this.jobMap = new Map();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 初始化事件监听
|
|
19
|
-
*/
|
|
20
|
-
_initEvents() {
|
|
21
|
-
process.on('message', this._handleMessage.bind(this));
|
|
22
|
-
process.once('exit', (code) => {
|
|
23
|
-
coreLogger.info(`[ee-core] [jobs/child] received a exit from main-process, code:${code}, pid:${process.pid}`);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 监听消息
|
|
29
|
-
*/
|
|
30
|
-
_handleMessage(m) {
|
|
31
|
-
if (commands.indexOf(m.cmd) == -1) {
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
switch (m.cmd) {
|
|
35
|
-
case 'run':
|
|
36
|
-
this.run(m);
|
|
37
|
-
break;
|
|
38
|
-
default:
|
|
39
|
-
}
|
|
40
|
-
coreLogger.info(`[ee-core] [jobs/child] received a message from main-process, message: ${JSON.stringify(m)}`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 运行脚本
|
|
45
|
-
*/
|
|
46
|
-
run(msg = {}) {
|
|
47
|
-
const {jobPath, jobParams, jobFunc, jobFuncParams} = msg;
|
|
48
|
-
let mod = requireFile(jobPath);
|
|
49
|
-
if (is.class(mod) || isBytecodeClass(mod)) {
|
|
50
|
-
let instance;
|
|
51
|
-
if (!this.jobMap.has(jobPath)) {
|
|
52
|
-
instance = new mod(...jobParams);
|
|
53
|
-
this.jobMap.set(jobPath, instance);
|
|
54
|
-
} else {
|
|
55
|
-
instance = this.jobMap.get(jobPath);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 如果指定了函数名,则调用指定的函数
|
|
59
|
-
if (jobFunc) {
|
|
60
|
-
instance[jobFunc](...jobFuncParams);
|
|
61
|
-
} else {
|
|
62
|
-
instance.handle(...jobParams);
|
|
63
|
-
}
|
|
64
|
-
} else if (is.function(mod)) {
|
|
65
|
-
mod(jobParams);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
1
|
+
|
|
2
|
+
const is = require('is-type-of');
|
|
3
|
+
const { loadException } = require('ee-core/exception');
|
|
4
|
+
const { requireFile } = require('ee-core/loader');
|
|
5
|
+
const { coreLogger } = require('ee-core/log');
|
|
6
|
+
const { isBytecodeClass } = require('ee-core/core/utils');
|
|
7
|
+
|
|
8
|
+
loadException();
|
|
9
|
+
const commands = ['run'];
|
|
10
|
+
|
|
11
|
+
class ChildApp {
|
|
12
|
+
constructor() {
|
|
13
|
+
this._initEvents();
|
|
14
|
+
this.jobMap = new Map();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 初始化事件监听
|
|
19
|
+
*/
|
|
20
|
+
_initEvents() {
|
|
21
|
+
process.on('message', this._handleMessage.bind(this));
|
|
22
|
+
process.once('exit', (code) => {
|
|
23
|
+
coreLogger.info(`[ee-core] [jobs/child] received a exit from main-process, code:${code}, pid:${process.pid}`);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 监听消息
|
|
29
|
+
*/
|
|
30
|
+
_handleMessage(m) {
|
|
31
|
+
if (commands.indexOf(m.cmd) == -1) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
switch (m.cmd) {
|
|
35
|
+
case 'run':
|
|
36
|
+
this.run(m);
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
}
|
|
40
|
+
coreLogger.info(`[ee-core] [jobs/child] received a message from main-process, message: ${JSON.stringify(m)}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 运行脚本
|
|
45
|
+
*/
|
|
46
|
+
run(msg = {}) {
|
|
47
|
+
const {jobPath, jobParams, jobFunc, jobFuncParams} = msg;
|
|
48
|
+
let mod = requireFile(jobPath);
|
|
49
|
+
if (is.class(mod) || isBytecodeClass(mod)) {
|
|
50
|
+
let instance;
|
|
51
|
+
if (!this.jobMap.has(jobPath)) {
|
|
52
|
+
instance = new mod(...jobParams);
|
|
53
|
+
this.jobMap.set(jobPath, instance);
|
|
54
|
+
} else {
|
|
55
|
+
instance = this.jobMap.get(jobPath);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 如果指定了函数名,则调用指定的函数
|
|
59
|
+
if (jobFunc) {
|
|
60
|
+
instance[jobFunc](...jobFuncParams);
|
|
61
|
+
} else {
|
|
62
|
+
instance.handle(...jobParams);
|
|
63
|
+
}
|
|
64
|
+
} else if (is.function(mod)) {
|
|
65
|
+
mod(jobParams);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
70
|
new ChildApp();
|
package/jobs/child/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import EventEmitter = require("events");
|
|
2
|
-
import { JobProcess } from "./jobProcess";
|
|
3
|
-
export declare class ChildJob extends EventEmitter<[never]> {
|
|
4
|
-
constructor();
|
|
5
|
-
jobs: {};
|
|
6
|
-
config: any;
|
|
7
|
-
_initEvents(): void;
|
|
8
|
-
exec(filepath: string, params?: {}, opt?: {}): JobProcess;
|
|
9
|
-
createProcess(opt?: {}): JobProcess;
|
|
10
|
-
getPids(): string[];
|
|
11
|
-
execPromise(filepath: string, params?: {}, opt?: {}): Promise<JobProcess>;
|
|
12
|
-
}
|
|
13
|
-
|
|
1
|
+
import EventEmitter = require("events");
|
|
2
|
+
import { JobProcess } from "./jobProcess";
|
|
3
|
+
export declare class ChildJob extends EventEmitter<[never]> {
|
|
4
|
+
constructor();
|
|
5
|
+
jobs: {};
|
|
6
|
+
config: any;
|
|
7
|
+
_initEvents(): void;
|
|
8
|
+
exec(filepath: string, params?: {}, opt?: {}): JobProcess;
|
|
9
|
+
createProcess(opt?: {}): JobProcess;
|
|
10
|
+
getPids(): string[];
|
|
11
|
+
execPromise(filepath: string, params?: {}, opt?: {}): Promise<JobProcess>;
|
|
12
|
+
}
|
|
13
|
+
|
package/jobs/child/index.js
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
const EventEmitter = require('events');
|
|
2
|
-
const { JobProcess } = require('./jobProcess');
|
|
3
|
-
const { getFullpath } = require('../../loader');
|
|
4
|
-
const { Events } = require('../../const/channel');
|
|
5
|
-
const { getConfig } = require('../../config');
|
|
6
|
-
const { extend } = require('../../utils/extend');
|
|
7
|
-
|
|
8
|
-
class ChildJob extends EventEmitter {
|
|
9
|
-
|
|
10
|
-
constructor() {
|
|
11
|
-
super();
|
|
12
|
-
this.jobs = {};
|
|
13
|
-
this.config = {};
|
|
14
|
-
|
|
15
|
-
const cfg = getConfig().jobs;
|
|
16
|
-
if (cfg) {
|
|
17
|
-
this.config = cfg;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
this._initEvents();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 初始化监听
|
|
25
|
-
*/
|
|
26
|
-
_initEvents() {
|
|
27
|
-
this.on(Events.childProcessExit, (data) => {
|
|
28
|
-
delete this.jobs[data.pid];
|
|
29
|
-
});
|
|
30
|
-
this.on(Events.childProcessError, (data) => {
|
|
31
|
-
delete this.jobs[data.pid];
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 执行一个job文件
|
|
37
|
-
*/
|
|
38
|
-
exec(filepath, params = {}, opt = {}) {
|
|
39
|
-
const jobPath = getFullpath(filepath);
|
|
40
|
-
const proc = this.createProcess(opt);
|
|
41
|
-
const cmd = 'run';
|
|
42
|
-
proc.dispatch(cmd, jobPath, params);
|
|
43
|
-
|
|
44
|
-
return proc;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 创建子进程
|
|
49
|
-
*/
|
|
50
|
-
createProcess(opt = {}) {
|
|
51
|
-
const options = extend(true, {
|
|
52
|
-
processArgs: {
|
|
53
|
-
type: 'childJob'
|
|
54
|
-
}
|
|
55
|
-
}, opt);
|
|
56
|
-
const proc = new JobProcess(this, options);
|
|
57
|
-
if (!proc) {
|
|
58
|
-
let errorMessage = `[ee-core] [jobs/child] Failed to obtain the child process !`
|
|
59
|
-
throw new Error(errorMessage);
|
|
60
|
-
}
|
|
61
|
-
this.jobs[proc.pid] = proc;
|
|
62
|
-
|
|
63
|
-
return proc;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 获取当前pids
|
|
68
|
-
*/
|
|
69
|
-
getPids() {
|
|
70
|
-
let pids = Object.keys(this.jobs);
|
|
71
|
-
return pids;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 异步执行一个job文件 todo this指向
|
|
76
|
-
*/
|
|
77
|
-
async execPromise(filepath, params = {}, opt = {}) {
|
|
78
|
-
return this.exec(filepath, params, opt);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = {
|
|
84
|
-
ChildJob
|
|
85
|
-
};
|
|
1
|
+
const EventEmitter = require('events');
|
|
2
|
+
const { JobProcess } = require('./jobProcess');
|
|
3
|
+
const { getFullpath } = require('../../loader');
|
|
4
|
+
const { Events } = require('../../const/channel');
|
|
5
|
+
const { getConfig } = require('../../config');
|
|
6
|
+
const { extend } = require('../../utils/extend');
|
|
7
|
+
|
|
8
|
+
class ChildJob extends EventEmitter {
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this.jobs = {};
|
|
13
|
+
this.config = {};
|
|
14
|
+
|
|
15
|
+
const cfg = getConfig().jobs;
|
|
16
|
+
if (cfg) {
|
|
17
|
+
this.config = cfg;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this._initEvents();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 初始化监听
|
|
25
|
+
*/
|
|
26
|
+
_initEvents() {
|
|
27
|
+
this.on(Events.childProcessExit, (data) => {
|
|
28
|
+
delete this.jobs[data.pid];
|
|
29
|
+
});
|
|
30
|
+
this.on(Events.childProcessError, (data) => {
|
|
31
|
+
delete this.jobs[data.pid];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 执行一个job文件
|
|
37
|
+
*/
|
|
38
|
+
exec(filepath, params = {}, opt = {}) {
|
|
39
|
+
const jobPath = getFullpath(filepath);
|
|
40
|
+
const proc = this.createProcess(opt);
|
|
41
|
+
const cmd = 'run';
|
|
42
|
+
proc.dispatch(cmd, jobPath, params);
|
|
43
|
+
|
|
44
|
+
return proc;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 创建子进程
|
|
49
|
+
*/
|
|
50
|
+
createProcess(opt = {}) {
|
|
51
|
+
const options = extend(true, {
|
|
52
|
+
processArgs: {
|
|
53
|
+
type: 'childJob'
|
|
54
|
+
}
|
|
55
|
+
}, opt);
|
|
56
|
+
const proc = new JobProcess(this, options);
|
|
57
|
+
if (!proc) {
|
|
58
|
+
let errorMessage = `[ee-core] [jobs/child] Failed to obtain the child process !`
|
|
59
|
+
throw new Error(errorMessage);
|
|
60
|
+
}
|
|
61
|
+
this.jobs[proc.pid] = proc;
|
|
62
|
+
|
|
63
|
+
return proc;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 获取当前pids
|
|
68
|
+
*/
|
|
69
|
+
getPids() {
|
|
70
|
+
let pids = Object.keys(this.jobs);
|
|
71
|
+
return pids;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 异步执行一个job文件 todo this指向
|
|
76
|
+
*/
|
|
77
|
+
async execPromise(filepath, params = {}, opt = {}) {
|
|
78
|
+
return this.exec(filepath, params, opt);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = {
|
|
84
|
+
ChildJob
|
|
85
|
+
};
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { EventEmitter } from "events";
|
|
2
|
-
import { ChildProcess } from "child_process";
|
|
3
|
-
declare export class JobProcess {
|
|
4
|
-
constructor(host: any, opt?: {});
|
|
5
|
-
emitter: EventEmitter<[never]>;
|
|
6
|
-
host: any;
|
|
7
|
-
args: string[];
|
|
8
|
-
sleeping: boolean;
|
|
9
|
-
child: ChildProcess;
|
|
10
|
-
pid: number;
|
|
11
|
-
_init(): void;
|
|
12
|
-
_eventEmit(m: any): void;
|
|
13
|
-
dispatch(cmd: string, jobPath?: string, ...params: any[]): void;
|
|
14
|
-
callFunc(jobPath?: string, funcName?: string, ...params: any[]): void;
|
|
15
|
-
kill(timeout?: number): void;
|
|
16
|
-
}
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
import { ChildProcess } from "child_process";
|
|
3
|
+
declare export class JobProcess {
|
|
4
|
+
constructor(host: any, opt?: {});
|
|
5
|
+
emitter: EventEmitter<[never]>;
|
|
6
|
+
host: any;
|
|
7
|
+
args: string[];
|
|
8
|
+
sleeping: boolean;
|
|
9
|
+
child: ChildProcess;
|
|
10
|
+
pid: number;
|
|
11
|
+
_init(): void;
|
|
12
|
+
_eventEmit(m: any): void;
|
|
13
|
+
dispatch(cmd: string, jobPath?: string, ...params: any[]): void;
|
|
14
|
+
callFunc(jobPath?: string, funcName?: string, ...params: any[]): void;
|
|
15
|
+
kill(timeout?: number): void;
|
|
16
|
+
}
|
package/jobs/child/jobProcess.js
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const EventEmitter = require('events');
|
|
3
|
-
const serialize = require('serialize-javascript');
|
|
4
|
-
const { fork } = require('child_process');
|
|
5
|
-
const { coreLogger } = require('../../log');
|
|
6
|
-
const { getBaseDir, isPackaged, allEnv } = require('../../ps');
|
|
7
|
-
const { Processes, Events, Receiver } = require('../../const/channel');
|
|
8
|
-
const { getRandomString } = require('../../utils/helper');
|
|
9
|
-
const { getFullpath } = require('../../loader');
|
|
10
|
-
const { extend } = require('../../utils/extend');
|
|
11
|
-
|
|
12
|
-
class JobProcess {
|
|
13
|
-
constructor(host, opt = {}) {
|
|
14
|
-
|
|
15
|
-
let cwd = getBaseDir();
|
|
16
|
-
const appPath = path.join(__dirname, 'app.js');
|
|
17
|
-
if (isPackaged()) {
|
|
18
|
-
// todo fork的cwd目录为什么要在app.asar外 ?
|
|
19
|
-
cwd = path.join(getBaseDir(), '..');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const options = extend(true, {
|
|
23
|
-
processArgs: {},
|
|
24
|
-
processOptions: {
|
|
25
|
-
cwd: cwd,
|
|
26
|
-
env: allEnv(),
|
|
27
|
-
stdio: 'ignore' // pipe
|
|
28
|
-
}
|
|
29
|
-
}, opt);
|
|
30
|
-
|
|
31
|
-
this.emitter = new EventEmitter();
|
|
32
|
-
this.host = host;
|
|
33
|
-
this.args = [];
|
|
34
|
-
this.sleeping = false;
|
|
35
|
-
|
|
36
|
-
// 传递给子进程的参数
|
|
37
|
-
this.args.push(JSON.stringify(options.processArgs));
|
|
38
|
-
|
|
39
|
-
this.child = fork(appPath, this.args, options.processOptions);
|
|
40
|
-
this.pid = this.child.pid;
|
|
41
|
-
this._init();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
_init() {
|
|
45
|
-
const { messageLog } = this.host.config;
|
|
46
|
-
this.child.on('message', (m) => {
|
|
47
|
-
if (messageLog == true) {
|
|
48
|
-
coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (m.channel == Processes.showException) {
|
|
52
|
-
coreLogger.error(`${m.data}`);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 收到子进程消息,转发到 event
|
|
56
|
-
if (m.channel == Processes.sendToMain) {
|
|
57
|
-
this._eventEmit(m);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
this.child.on('exit', (code, signal) => {
|
|
62
|
-
let data = {
|
|
63
|
-
pid: this.pid
|
|
64
|
-
}
|
|
65
|
-
this.host.emit(Events.childProcessExit, data);
|
|
66
|
-
coreLogger.info(`[ee-core] [jobs/child] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
this.child.on('error', (err) => {
|
|
70
|
-
let data = {
|
|
71
|
-
pid: this.pid
|
|
72
|
-
}
|
|
73
|
-
this.host.emit(Events.childProcessError, data);
|
|
74
|
-
coreLogger.error(`[ee-core] [jobs/child] received a error from child-process, error: ${err}, pid:${this.pid}`);
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
_eventEmit(m) {
|
|
79
|
-
switch (m.eventReceiver) {
|
|
80
|
-
case Receiver.forkProcess:
|
|
81
|
-
this.emitter.emit(m.event, m.data);
|
|
82
|
-
break;
|
|
83
|
-
case Receiver.childJob:
|
|
84
|
-
this.host.emit(m.event, m.data);
|
|
85
|
-
break;
|
|
86
|
-
default:
|
|
87
|
-
this.host.emit(m.event, m.data);
|
|
88
|
-
this.emitter.emit(m.event, m.data);
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
dispatch(cmd, jobPath = '', ...params) {
|
|
94
|
-
// 消息对象
|
|
95
|
-
const mid = getRandomString();
|
|
96
|
-
let msg = {
|
|
97
|
-
mid,
|
|
98
|
-
cmd,
|
|
99
|
-
jobPath,
|
|
100
|
-
jobParams: params
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// todo 是否会发生监听未完成时,接收不到消息?
|
|
104
|
-
// 发消息到子进程
|
|
105
|
-
this.child.send(msg);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
callFunc(jobPath = '', funcName = '', ...params) {
|
|
109
|
-
jobPath = getFullpath(jobPath);
|
|
110
|
-
|
|
111
|
-
// 消息对象
|
|
112
|
-
const mid = getRandomString();
|
|
113
|
-
let msg = {
|
|
114
|
-
mid,
|
|
115
|
-
cmd:'run',
|
|
116
|
-
jobPath,
|
|
117
|
-
jobFunc: funcName,
|
|
118
|
-
jobFuncParams: params
|
|
119
|
-
}
|
|
120
|
-
this.child.send(msg);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
kill(timeout = 1000) {
|
|
124
|
-
this.child.kill('SIGINT');
|
|
125
|
-
setTimeout(() => {
|
|
126
|
-
if (this.child.killed) return;
|
|
127
|
-
this.child.kill('SIGKILL');
|
|
128
|
-
}, timeout)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
module.exports = {
|
|
133
|
-
JobProcess
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const EventEmitter = require('events');
|
|
3
|
+
const serialize = require('serialize-javascript');
|
|
4
|
+
const { fork } = require('child_process');
|
|
5
|
+
const { coreLogger } = require('../../log');
|
|
6
|
+
const { getBaseDir, isPackaged, allEnv } = require('../../ps');
|
|
7
|
+
const { Processes, Events, Receiver } = require('../../const/channel');
|
|
8
|
+
const { getRandomString } = require('../../utils/helper');
|
|
9
|
+
const { getFullpath } = require('../../loader');
|
|
10
|
+
const { extend } = require('../../utils/extend');
|
|
11
|
+
|
|
12
|
+
class JobProcess {
|
|
13
|
+
constructor(host, opt = {}) {
|
|
14
|
+
|
|
15
|
+
let cwd = getBaseDir();
|
|
16
|
+
const appPath = path.join(__dirname, 'app.js');
|
|
17
|
+
if (isPackaged()) {
|
|
18
|
+
// todo fork的cwd目录为什么要在app.asar外 ?
|
|
19
|
+
cwd = path.join(getBaseDir(), '..');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const options = extend(true, {
|
|
23
|
+
processArgs: {},
|
|
24
|
+
processOptions: {
|
|
25
|
+
cwd: cwd,
|
|
26
|
+
env: allEnv(),
|
|
27
|
+
stdio: 'ignore' // pipe
|
|
28
|
+
}
|
|
29
|
+
}, opt);
|
|
30
|
+
|
|
31
|
+
this.emitter = new EventEmitter();
|
|
32
|
+
this.host = host;
|
|
33
|
+
this.args = [];
|
|
34
|
+
this.sleeping = false;
|
|
35
|
+
|
|
36
|
+
// 传递给子进程的参数
|
|
37
|
+
this.args.push(JSON.stringify(options.processArgs));
|
|
38
|
+
|
|
39
|
+
this.child = fork(appPath, this.args, options.processOptions);
|
|
40
|
+
this.pid = this.child.pid;
|
|
41
|
+
this._init();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_init() {
|
|
45
|
+
const { messageLog } = this.host.config;
|
|
46
|
+
this.child.on('message', (m) => {
|
|
47
|
+
if (messageLog == true) {
|
|
48
|
+
coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (m.channel == Processes.showException) {
|
|
52
|
+
coreLogger.error(`${m.data}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 收到子进程消息,转发到 event
|
|
56
|
+
if (m.channel == Processes.sendToMain) {
|
|
57
|
+
this._eventEmit(m);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.child.on('exit', (code, signal) => {
|
|
62
|
+
let data = {
|
|
63
|
+
pid: this.pid
|
|
64
|
+
}
|
|
65
|
+
this.host.emit(Events.childProcessExit, data);
|
|
66
|
+
coreLogger.info(`[ee-core] [jobs/child] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.child.on('error', (err) => {
|
|
70
|
+
let data = {
|
|
71
|
+
pid: this.pid
|
|
72
|
+
}
|
|
73
|
+
this.host.emit(Events.childProcessError, data);
|
|
74
|
+
coreLogger.error(`[ee-core] [jobs/child] received a error from child-process, error: ${err}, pid:${this.pid}`);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
_eventEmit(m) {
|
|
79
|
+
switch (m.eventReceiver) {
|
|
80
|
+
case Receiver.forkProcess:
|
|
81
|
+
this.emitter.emit(m.event, m.data);
|
|
82
|
+
break;
|
|
83
|
+
case Receiver.childJob:
|
|
84
|
+
this.host.emit(m.event, m.data);
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
this.host.emit(m.event, m.data);
|
|
88
|
+
this.emitter.emit(m.event, m.data);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
dispatch(cmd, jobPath = '', ...params) {
|
|
94
|
+
// 消息对象
|
|
95
|
+
const mid = getRandomString();
|
|
96
|
+
let msg = {
|
|
97
|
+
mid,
|
|
98
|
+
cmd,
|
|
99
|
+
jobPath,
|
|
100
|
+
jobParams: params
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// todo 是否会发生监听未完成时,接收不到消息?
|
|
104
|
+
// 发消息到子进程
|
|
105
|
+
this.child.send(msg);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
callFunc(jobPath = '', funcName = '', ...params) {
|
|
109
|
+
jobPath = getFullpath(jobPath);
|
|
110
|
+
|
|
111
|
+
// 消息对象
|
|
112
|
+
const mid = getRandomString();
|
|
113
|
+
let msg = {
|
|
114
|
+
mid,
|
|
115
|
+
cmd:'run',
|
|
116
|
+
jobPath,
|
|
117
|
+
jobFunc: funcName,
|
|
118
|
+
jobFuncParams: params
|
|
119
|
+
}
|
|
120
|
+
this.child.send(msg);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
kill(timeout = 1000) {
|
|
124
|
+
this.child.kill('SIGINT');
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
if (this.child.killed) return;
|
|
127
|
+
this.child.kill('SIGKILL');
|
|
128
|
+
}, timeout)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = {
|
|
133
|
+
JobProcess
|
|
134
134
|
};
|