ee-core 2.9.2-beta.1 → 2.10.0
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/addon/index.js +34 -34
- package/addon/window/index.js +98 -98
- package/bin/tools.js +8 -8
- package/config/cache.js +41 -38
- package/config/config.default.js +331 -330
- package/config/index.js +75 -73
- package/const/channel.js +17 -17
- package/const/index.js +8 -8
- package/controller/baseContextClass.js +34 -34
- package/controller/index.js +34 -34
- package/core/index.js +10 -10
- package/core/lib/ee.js +216 -216
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +435 -435
- package/core/lib/loader/file_loader.js +326 -326
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +130 -130
- package/core/lib/loader/mixin/controller.js +125 -125
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/function.js +30 -30
- package/core/lib/utils/index.js +133 -133
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/cross/index.js +183 -183
- package/cross/spawnProcess.js +183 -183
- package/ee/appLoader.js +48 -48
- package/ee/application.js +99 -99
- package/ee/baseApp.js +103 -102
- package/ee/eeApp.js +408 -408
- package/ee/index.js +57 -57
- package/electron/app/index.js +64 -58
- package/electron/index.js +19 -19
- package/electron/window/index.js +73 -73
- package/electron/window/winState.js +186 -186
- package/exception/index.js +112 -112
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.js +13 -13
- package/httpclient/index.js +161 -161
- package/index.js +54 -54
- package/jobs/baseJobClass.js +16 -16
- package/jobs/child/app.js +65 -65
- package/jobs/child/forkProcess.js +145 -145
- package/jobs/child/index.js +82 -82
- package/jobs/child-pool/index.js +213 -213
- package/jobs/index.js +8 -8
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.js +31 -31
- package/jobs/renderer/index.js +141 -141
- package/jobs/renderer/loadView.js +40 -40
- package/jobs/unification.js +63 -63
- package/loader/index.js +172 -172
- package/log/index.js +68 -68
- package/log/logger.js +86 -80
- package/main/index.js +56 -56
- package/message/childMessage.js +54 -54
- package/message/index.js +18 -18
- package/old-utils/index.js +91 -91
- package/package.json +38 -38
- package/ps/index.js +371 -371
- package/services/baseContextClass.js +34 -34
- package/services/index.js +40 -40
- package/socket/httpServer.js +147 -147
- package/socket/index.js +81 -81
- package/socket/io.js +27 -27
- package/socket/ipcServer.js +112 -112
- package/socket/socketServer.js +69 -65
- package/storage/index.js +38 -38
- package/storage/jsondb/adapters/Base.js +23 -23
- package/storage/jsondb/adapters/FileSync.js +64 -52
- package/storage/jsondb/main.js +55 -42
- package/storage/jsondbStorage.js +195 -195
- package/storage/sqliteStorage.js +123 -123
- package/utils/co.js +237 -237
- package/utils/copyto.js +160 -160
- package/utils/depd/index.js +538 -538
- package/utils/depd/lib/browser/index.js +77 -77
- package/utils/extend.js +73 -73
- package/utils/get-port/index.d.ts +64 -64
- package/utils/get-port/index.js +148 -148
- package/utils/helper.js +220 -220
- package/utils/index.js +160 -160
- package/utils/ip.js +261 -261
- package/utils/is.js +145 -145
- package/utils/json.js +72 -72
- package/utils/pargv.js +263 -263
- package/utils/time/index.js +19 -19
- package/utils/time/ms.js +162 -162
- package/utils/wrap.js +35 -35
package/index.js
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @namespace EeCore
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @member {ElectronEgg} EeCore#Index
|
|
7
|
-
* @since 1.0.0
|
|
8
|
-
*/
|
|
9
|
-
const ElectronEgg = require('./main');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @member {app} EeCore#app
|
|
13
|
-
* @since 1.0.0
|
|
14
|
-
*/
|
|
15
|
-
const EE = require('./ee');
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @member {Controller} EeCore#Controller
|
|
19
|
-
* @since 1.0.0
|
|
20
|
-
*/
|
|
21
|
-
const Controller = require('./controller/baseContextClass');
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @member {Service} EeCore#Service
|
|
25
|
-
* @since 1.0.0
|
|
26
|
-
*/
|
|
27
|
-
const Service = require('./services/baseContextClass');
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @member {Storage}
|
|
31
|
-
* @since 1.0.0
|
|
32
|
-
*/
|
|
33
|
-
const Storage = require('./storage');
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @member {Utils}
|
|
37
|
-
* @since 1.0.0
|
|
38
|
-
*/
|
|
39
|
-
const Utils = require('./old-utils');
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @member {Socket}
|
|
43
|
-
* @since 1.0.0
|
|
44
|
-
*/
|
|
45
|
-
const Socket = require('./socket');
|
|
46
|
-
|
|
47
|
-
module.exports = {
|
|
48
|
-
ElectronEgg,
|
|
49
|
-
Application: EE.Application,
|
|
50
|
-
Controller,
|
|
51
|
-
Service,
|
|
52
|
-
Storage,
|
|
53
|
-
Socket,
|
|
54
|
-
Utils
|
|
1
|
+
/**
|
|
2
|
+
* @namespace EeCore
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @member {ElectronEgg} EeCore#Index
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
const ElectronEgg = require('./main');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @member {app} EeCore#app
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
const EE = require('./ee');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @member {Controller} EeCore#Controller
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
const Controller = require('./controller/baseContextClass');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @member {Service} EeCore#Service
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
const Service = require('./services/baseContextClass');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @member {Storage}
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
const Storage = require('./storage');
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @member {Utils}
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
*/
|
|
39
|
+
const Utils = require('./old-utils');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @member {Socket}
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
*/
|
|
45
|
+
const Socket = require('./socket');
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
48
|
+
ElectronEgg,
|
|
49
|
+
Application: EE.Application,
|
|
50
|
+
Controller,
|
|
51
|
+
Service,
|
|
52
|
+
Storage,
|
|
53
|
+
Socket,
|
|
54
|
+
Utils
|
|
55
55
|
};
|
package/jobs/baseJobClass.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BaseJobClass
|
|
3
|
-
*/
|
|
4
|
-
class BaseJobClass {
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @class
|
|
8
|
-
* @param {Object} params - job基础类
|
|
9
|
-
* @since 1.0.0
|
|
10
|
-
*/
|
|
11
|
-
constructor(params = {}) {
|
|
12
|
-
// todo
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = BaseJobClass;
|
|
1
|
+
/**
|
|
2
|
+
* BaseJobClass
|
|
3
|
+
*/
|
|
4
|
+
class BaseJobClass {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class
|
|
8
|
+
* @param {Object} params - job基础类
|
|
9
|
+
* @since 1.0.0
|
|
10
|
+
*/
|
|
11
|
+
constructor(params = {}) {
|
|
12
|
+
// todo
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = BaseJobClass;
|
package/jobs/child/app.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
const is = require('is-type-of');
|
|
3
|
-
const Exception = require('ee-core/exception');
|
|
4
|
-
const Loader = require('ee-core/loader');
|
|
5
|
-
const Log = require('ee-core/log');
|
|
6
|
-
const UtilsCore = require('ee-core/core/lib/utils');
|
|
7
|
-
|
|
8
|
-
// 开发环境下,ee-core是soft link
|
|
9
|
-
// /node_modules[\\/]electron[\\/]/.test(process.execPath)
|
|
10
|
-
// const Exception = require('../../exception');
|
|
11
|
-
// const Loader = require('../../loader');
|
|
12
|
-
// const Log = require('../../log');
|
|
13
|
-
// const UtilsCore = require('../../core/lib/utils');
|
|
14
|
-
|
|
15
|
-
Exception.start();
|
|
16
|
-
const commands = ['run'];
|
|
17
|
-
|
|
18
|
-
class ChildApp {
|
|
19
|
-
constructor() {
|
|
20
|
-
this._initEvents();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 初始化事件监听
|
|
25
|
-
*/
|
|
26
|
-
_initEvents() {
|
|
27
|
-
process.on('message', this._handleMessage.bind(this));
|
|
28
|
-
process.once('exit', (code) => {
|
|
29
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from main-process, code:${code}, pid:${process.pid}`);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 监听消息
|
|
35
|
-
*/
|
|
36
|
-
_handleMessage(m) {
|
|
37
|
-
if (commands.indexOf(m.cmd) == -1) {
|
|
38
|
-
return
|
|
39
|
-
}
|
|
40
|
-
switch (m.cmd) {
|
|
41
|
-
case 'run':
|
|
42
|
-
this.run(m);
|
|
43
|
-
break;
|
|
44
|
-
default:
|
|
45
|
-
}
|
|
46
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] received a message from main-process, message: ${JSON.stringify(m)}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 运行脚本
|
|
51
|
-
*/
|
|
52
|
-
run(msg = {}) {
|
|
53
|
-
let filepath = msg.jobPath;
|
|
54
|
-
let params = msg.jobParams;
|
|
55
|
-
|
|
56
|
-
let mod = Loader.loadJsFile(filepath);
|
|
57
|
-
if (is.class(mod) || UtilsCore.isBytecodeClass(mod)) {
|
|
58
|
-
let jobClass = new mod(params);
|
|
59
|
-
jobClass.handle();
|
|
60
|
-
} else if (is.function(mod)) {
|
|
61
|
-
mod(params);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
1
|
+
|
|
2
|
+
const is = require('is-type-of');
|
|
3
|
+
const Exception = require('ee-core/exception');
|
|
4
|
+
const Loader = require('ee-core/loader');
|
|
5
|
+
const Log = require('ee-core/log');
|
|
6
|
+
const UtilsCore = require('ee-core/core/lib/utils');
|
|
7
|
+
|
|
8
|
+
// 开发环境下,ee-core是soft link
|
|
9
|
+
// /node_modules[\\/]electron[\\/]/.test(process.execPath)
|
|
10
|
+
// const Exception = require('../../exception');
|
|
11
|
+
// const Loader = require('../../loader');
|
|
12
|
+
// const Log = require('../../log');
|
|
13
|
+
// const UtilsCore = require('../../core/lib/utils');
|
|
14
|
+
|
|
15
|
+
Exception.start();
|
|
16
|
+
const commands = ['run'];
|
|
17
|
+
|
|
18
|
+
class ChildApp {
|
|
19
|
+
constructor() {
|
|
20
|
+
this._initEvents();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 初始化事件监听
|
|
25
|
+
*/
|
|
26
|
+
_initEvents() {
|
|
27
|
+
process.on('message', this._handleMessage.bind(this));
|
|
28
|
+
process.once('exit', (code) => {
|
|
29
|
+
Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from main-process, code:${code}, pid:${process.pid}`);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 监听消息
|
|
35
|
+
*/
|
|
36
|
+
_handleMessage(m) {
|
|
37
|
+
if (commands.indexOf(m.cmd) == -1) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
switch (m.cmd) {
|
|
41
|
+
case 'run':
|
|
42
|
+
this.run(m);
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
}
|
|
46
|
+
Log.coreLogger.info(`[ee-core] [jobs/child] received a message from main-process, message: ${JSON.stringify(m)}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 运行脚本
|
|
51
|
+
*/
|
|
52
|
+
run(msg = {}) {
|
|
53
|
+
let filepath = msg.jobPath;
|
|
54
|
+
let params = msg.jobParams;
|
|
55
|
+
|
|
56
|
+
let mod = Loader.loadJsFile(filepath);
|
|
57
|
+
if (is.class(mod) || UtilsCore.isBytecodeClass(mod)) {
|
|
58
|
+
let jobClass = new mod(params);
|
|
59
|
+
jobClass.handle();
|
|
60
|
+
} else if (is.function(mod)) {
|
|
61
|
+
mod(params);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
66
|
new ChildApp();
|
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const EventEmitter = require('events');
|
|
3
|
-
const { fork } = require('child_process');
|
|
4
|
-
const serialize = require('serialize-javascript');
|
|
5
|
-
const Log = require('../../log');
|
|
6
|
-
const Ps = require('../../ps');
|
|
7
|
-
const Channel = require('../../const/channel');
|
|
8
|
-
const Helper = require('../../utils/helper');
|
|
9
|
-
|
|
10
|
-
class ForkProcess {
|
|
11
|
-
constructor(host, opt = {}) {
|
|
12
|
-
|
|
13
|
-
let cwd = Ps.getHomeDir();
|
|
14
|
-
let appPath = path.join(__dirname, 'app.js');
|
|
15
|
-
if (Ps.isPackaged()) {
|
|
16
|
-
// todo fork的cwd目录为什么要在app.asar外 ?
|
|
17
|
-
cwd = path.join(Ps.getHomeDir(), '..');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// TODO Object.assign 只能单层对象结构,多层的对象会直接覆盖
|
|
21
|
-
let options = Object.assign({
|
|
22
|
-
processArgs: {},
|
|
23
|
-
processOptions: {
|
|
24
|
-
cwd: cwd,
|
|
25
|
-
env: Ps.allEnv(),
|
|
26
|
-
stdio: 'ignore' // pipe
|
|
27
|
-
}
|
|
28
|
-
}, opt);
|
|
29
|
-
|
|
30
|
-
this.emitter = new EventEmitter();
|
|
31
|
-
this.host = host;
|
|
32
|
-
this.args = [];
|
|
33
|
-
this.sleeping = false;
|
|
34
|
-
|
|
35
|
-
// 传递给子进程的参数
|
|
36
|
-
this.args.push(JSON.stringify(options.processArgs));
|
|
37
|
-
|
|
38
|
-
this.child = fork(appPath, this.args, options.processOptions);
|
|
39
|
-
this.pid = this.child.pid;
|
|
40
|
-
this._init();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 初始化事件监听
|
|
45
|
-
*/
|
|
46
|
-
_init() {
|
|
47
|
-
const { messageLog } = this.host.config;
|
|
48
|
-
this.child.on('message', (m) => {
|
|
49
|
-
if (messageLog == true) {
|
|
50
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (m.channel == Channel.process.showException) {
|
|
54
|
-
Log.coreLogger.error(`${m.data}`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// 收到子进程消息,转发到 event
|
|
58
|
-
if (m.channel == Channel.process.sendToMain) {
|
|
59
|
-
this._eventEmit(m);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
this.child.on('exit', (code, signal) => {
|
|
64
|
-
let data = {
|
|
65
|
-
pid: this.pid
|
|
66
|
-
}
|
|
67
|
-
this.host.emit(Channel.events.childProcessExit, data);
|
|
68
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
this.child.on('error', (err) => {
|
|
72
|
-
let data = {
|
|
73
|
-
pid: this.pid
|
|
74
|
-
}
|
|
75
|
-
this.host.emit(Channel.events.childProcessError, data);
|
|
76
|
-
Log.coreLogger.error(`[ee-core] [jobs/child] received a error from child-process, error: ${err}, pid:${this.pid}`);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* event emit
|
|
82
|
-
*/
|
|
83
|
-
_eventEmit(m) {
|
|
84
|
-
switch (m.eventReceiver) {
|
|
85
|
-
case Channel.receiver.forkProcess:
|
|
86
|
-
this.emitter.emit(m.event, m.data);
|
|
87
|
-
break;
|
|
88
|
-
case Channel.receiver.childJob:
|
|
89
|
-
this.host.emit(m.event, m.data);
|
|
90
|
-
break;
|
|
91
|
-
default:
|
|
92
|
-
this.host.emit(m.event, m.data);
|
|
93
|
-
this.emitter.emit(m.event, m.data);
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 分发任务
|
|
100
|
-
*/
|
|
101
|
-
dispatch(cmd, jobPath = '', params = {}) {
|
|
102
|
-
// 消息对象
|
|
103
|
-
const mid = Helper.getRandomString();
|
|
104
|
-
let msg = {
|
|
105
|
-
mid,
|
|
106
|
-
cmd,
|
|
107
|
-
jobPath,
|
|
108
|
-
jobParams: params
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// todo 是否会发生监听未完成时,接收不到消息?
|
|
112
|
-
// 发消息到子进程
|
|
113
|
-
this.child.send(msg);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* kill
|
|
118
|
-
*/
|
|
119
|
-
kill(timeout = 1000) {
|
|
120
|
-
this.child.kill('SIGINT');
|
|
121
|
-
setTimeout(() => {
|
|
122
|
-
if (this.child.killed) return;
|
|
123
|
-
this.child.kill('SIGKILL');
|
|
124
|
-
}, timeout)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* sleep (仅Unix平台)
|
|
129
|
-
*/
|
|
130
|
-
sleep() {
|
|
131
|
-
if (this.sleeping) return;
|
|
132
|
-
process.kill(this.pid, 'SIGSTOP');
|
|
133
|
-
this.sleeping = true;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* wakeup (仅Unix平台)
|
|
138
|
-
*/
|
|
139
|
-
wakeup() {
|
|
140
|
-
if (!this.sleeping) return;
|
|
141
|
-
process.kill(this.pid, 'SIGCONT');
|
|
142
|
-
this.sleeping = false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const EventEmitter = require('events');
|
|
3
|
+
const { fork } = require('child_process');
|
|
4
|
+
const serialize = require('serialize-javascript');
|
|
5
|
+
const Log = require('../../log');
|
|
6
|
+
const Ps = require('../../ps');
|
|
7
|
+
const Channel = require('../../const/channel');
|
|
8
|
+
const Helper = require('../../utils/helper');
|
|
9
|
+
|
|
10
|
+
class ForkProcess {
|
|
11
|
+
constructor(host, opt = {}) {
|
|
12
|
+
|
|
13
|
+
let cwd = Ps.getHomeDir();
|
|
14
|
+
let appPath = path.join(__dirname, 'app.js');
|
|
15
|
+
if (Ps.isPackaged()) {
|
|
16
|
+
// todo fork的cwd目录为什么要在app.asar外 ?
|
|
17
|
+
cwd = path.join(Ps.getHomeDir(), '..');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// TODO Object.assign 只能单层对象结构,多层的对象会直接覆盖
|
|
21
|
+
let options = Object.assign({
|
|
22
|
+
processArgs: {},
|
|
23
|
+
processOptions: {
|
|
24
|
+
cwd: cwd,
|
|
25
|
+
env: Ps.allEnv(),
|
|
26
|
+
stdio: 'ignore' // pipe
|
|
27
|
+
}
|
|
28
|
+
}, opt);
|
|
29
|
+
|
|
30
|
+
this.emitter = new EventEmitter();
|
|
31
|
+
this.host = host;
|
|
32
|
+
this.args = [];
|
|
33
|
+
this.sleeping = false;
|
|
34
|
+
|
|
35
|
+
// 传递给子进程的参数
|
|
36
|
+
this.args.push(JSON.stringify(options.processArgs));
|
|
37
|
+
|
|
38
|
+
this.child = fork(appPath, this.args, options.processOptions);
|
|
39
|
+
this.pid = this.child.pid;
|
|
40
|
+
this._init();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 初始化事件监听
|
|
45
|
+
*/
|
|
46
|
+
_init() {
|
|
47
|
+
const { messageLog } = this.host.config;
|
|
48
|
+
this.child.on('message', (m) => {
|
|
49
|
+
if (messageLog == true) {
|
|
50
|
+
Log.coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (m.channel == Channel.process.showException) {
|
|
54
|
+
Log.coreLogger.error(`${m.data}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 收到子进程消息,转发到 event
|
|
58
|
+
if (m.channel == Channel.process.sendToMain) {
|
|
59
|
+
this._eventEmit(m);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this.child.on('exit', (code, signal) => {
|
|
64
|
+
let data = {
|
|
65
|
+
pid: this.pid
|
|
66
|
+
}
|
|
67
|
+
this.host.emit(Channel.events.childProcessExit, data);
|
|
68
|
+
Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.child.on('error', (err) => {
|
|
72
|
+
let data = {
|
|
73
|
+
pid: this.pid
|
|
74
|
+
}
|
|
75
|
+
this.host.emit(Channel.events.childProcessError, data);
|
|
76
|
+
Log.coreLogger.error(`[ee-core] [jobs/child] received a error from child-process, error: ${err}, pid:${this.pid}`);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* event emit
|
|
82
|
+
*/
|
|
83
|
+
_eventEmit(m) {
|
|
84
|
+
switch (m.eventReceiver) {
|
|
85
|
+
case Channel.receiver.forkProcess:
|
|
86
|
+
this.emitter.emit(m.event, m.data);
|
|
87
|
+
break;
|
|
88
|
+
case Channel.receiver.childJob:
|
|
89
|
+
this.host.emit(m.event, m.data);
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
this.host.emit(m.event, m.data);
|
|
93
|
+
this.emitter.emit(m.event, m.data);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 分发任务
|
|
100
|
+
*/
|
|
101
|
+
dispatch(cmd, jobPath = '', params = {}) {
|
|
102
|
+
// 消息对象
|
|
103
|
+
const mid = Helper.getRandomString();
|
|
104
|
+
let msg = {
|
|
105
|
+
mid,
|
|
106
|
+
cmd,
|
|
107
|
+
jobPath,
|
|
108
|
+
jobParams: params
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// todo 是否会发生监听未完成时,接收不到消息?
|
|
112
|
+
// 发消息到子进程
|
|
113
|
+
this.child.send(msg);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* kill
|
|
118
|
+
*/
|
|
119
|
+
kill(timeout = 1000) {
|
|
120
|
+
this.child.kill('SIGINT');
|
|
121
|
+
setTimeout(() => {
|
|
122
|
+
if (this.child.killed) return;
|
|
123
|
+
this.child.kill('SIGKILL');
|
|
124
|
+
}, timeout)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* sleep (仅Unix平台)
|
|
129
|
+
*/
|
|
130
|
+
sleep() {
|
|
131
|
+
if (this.sleeping) return;
|
|
132
|
+
process.kill(this.pid, 'SIGSTOP');
|
|
133
|
+
this.sleeping = true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* wakeup (仅Unix平台)
|
|
138
|
+
*/
|
|
139
|
+
wakeup() {
|
|
140
|
+
if (!this.sleeping) return;
|
|
141
|
+
process.kill(this.pid, 'SIGCONT');
|
|
142
|
+
this.sleeping = false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
146
|
module.exports = ForkProcess;
|