ee-core 2.0.2 → 2.1.0-beta.2
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/config/config.default.js +9 -0
- package/const/channel.js +10 -1
- package/core/lib/ee.js +1 -1
- package/core/lib/loader/file_loader.js +2 -2
- package/core/lib/loader/mixin/config.js +1 -1
- package/core/lib/utils/index.js +1 -1
- package/ee/eeApp.js +8 -7
- package/ee/index.js +1 -1
- package/exception/index.js +40 -12
- package/httpclient/index.js +4 -12
- package/index.js +2 -2
- package/jobs/child/app.js +11 -2
- package/jobs/child/forkProcess.js +81 -6
- package/jobs/child/index.js +41 -45
- package/jobs/child-pool/index.js +205 -0
- package/jobs/index.js +3 -1
- package/jobs/load-balancer/algorithm/index.js +12 -0
- package/jobs/load-balancer/algorithm/minimumConnection.js +19 -0
- package/jobs/load-balancer/algorithm/polling.js +12 -0
- package/jobs/load-balancer/algorithm/random.js +10 -0
- package/jobs/load-balancer/algorithm/specify.js +15 -0
- package/jobs/load-balancer/algorithm/weights.js +22 -0
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +30 -0
- package/jobs/load-balancer/algorithm/weightsPolling.js +23 -0
- package/jobs/load-balancer/algorithm/weightsRandom.js +17 -0
- package/jobs/load-balancer/consts.js +10 -0
- package/jobs/load-balancer/index.js +202 -0
- package/jobs/load-balancer/scheduler.js +32 -0
- package/loader/index.js +22 -2
- package/message/childMessage.js +23 -0
- package/package.json +1 -6
- package/ps/index.js +44 -0
- package/utils/co.js +237 -0
- package/utils/depd/index.js +538 -0
- package/utils/depd/lib/browser/index.js +77 -0
- package/utils/extend.js +73 -0
- package/utils/get-port/index.d.ts +64 -0
- package/utils/get-port/index.js +109 -0
- package/utils/helper.js +25 -1
- package/utils/index.js +46 -0
- package/utils/ip.js +261 -0
- package/utils/is.js +2 -1
- package/utils/time/index.js +20 -0
- package/utils/time/ms.js +162 -0
- package/jobs/childPool/app.js +0 -62
- package/jobs/childPool/forkProcess.js +0 -81
- package/jobs/childPool/index.js +0 -71
- package/jobs/childPool/pool.js +0 -67
- /package/{oldUtils → old-utils}/index.js +0 -0
package/jobs/childPool/app.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
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('../../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
|
-
|
|
17
|
-
class ChildApp {
|
|
18
|
-
constructor() {
|
|
19
|
-
this._initEvents();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 初始化事件监听
|
|
24
|
-
*/
|
|
25
|
-
_initEvents() {
|
|
26
|
-
process.on('message', this._handleMessage.bind(this));
|
|
27
|
-
process.on('disconnect', () => {
|
|
28
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] child process disconnected, pid:${process.pid}`);
|
|
29
|
-
});
|
|
30
|
-
process.on('exit', (code) => {
|
|
31
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] child process exit code:${code}, pid:${process.pid}`);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 监听消息
|
|
37
|
-
*/
|
|
38
|
-
_handleMessage(m) {
|
|
39
|
-
this.run(m);
|
|
40
|
-
Log.coreLogger.info(`[ee-core] [jobs/child] Received a message ${JSON.stringify(m)} from the mainProcess`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 运行脚本
|
|
45
|
-
*/
|
|
46
|
-
run(msg = {}) {
|
|
47
|
-
let filepath = msg.jobPath;
|
|
48
|
-
let params = msg.params;
|
|
49
|
-
|
|
50
|
-
let mod = Loader.loadJsFile(filepath);
|
|
51
|
-
if (is.class(mod) || UtilsCore.isBytecodeClass(mod)) {
|
|
52
|
-
let jobClass = new mod(params);
|
|
53
|
-
jobClass.handle();
|
|
54
|
-
} else if (is.function(mod)) {
|
|
55
|
-
mod(params);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
Log.coreLogger.info('[ee-core] [child-process] job run');
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
new ChildApp();
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const { fork } = require('child_process');
|
|
3
|
-
const serialize = require('serialize-javascript');
|
|
4
|
-
const Log = require('../../log');
|
|
5
|
-
const Ps = require('../../ps');
|
|
6
|
-
const Channel = require('../../const/channel');
|
|
7
|
-
|
|
8
|
-
class ForkProcess {
|
|
9
|
-
constructor(host, opt = {}) {
|
|
10
|
-
|
|
11
|
-
let options = Object.assign({
|
|
12
|
-
processOptions: {
|
|
13
|
-
cwd: Ps.getHomeDir(),
|
|
14
|
-
env: Ps.allEnv(),
|
|
15
|
-
stdio: 'pipe'
|
|
16
|
-
}
|
|
17
|
-
}, opt);
|
|
18
|
-
|
|
19
|
-
this.host = host;
|
|
20
|
-
this.args = [];
|
|
21
|
-
this.sleeping = false;
|
|
22
|
-
|
|
23
|
-
// 传递给子进程的参数
|
|
24
|
-
//this.args.push(JSON.stringify(options.params));
|
|
25
|
-
|
|
26
|
-
const appPath = path.join(__dirname, 'app.js');
|
|
27
|
-
this.child = fork(appPath, this.args, options.processOptions);
|
|
28
|
-
|
|
29
|
-
this.pid = this.child.pid;
|
|
30
|
-
this._init();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 进程初始化
|
|
35
|
-
*/
|
|
36
|
-
_init() {
|
|
37
|
-
this.child.on('message', (m) => {
|
|
38
|
-
Log.coreLogger.info(`[ee-core] [jobs/child/forkProcess] from childProcess event-message: ${serialize(m)}`);
|
|
39
|
-
if (m.channel == Channel.process.showException) {
|
|
40
|
-
Log.coreLogger.error(`${m.data}`);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
this.child.on('disconnect', () => {
|
|
45
|
-
Log.coreLogger.info(`[ee-core] [jobs/child/forkProcess] from childProcess event-disconnect !`);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
this.child.on('close', (code, signal) => {
|
|
49
|
-
Log.coreLogger.info(`[ee-core] [jobs/child/forkProcess] from childProcess event-close code:${code}, signal:${signal}`);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
this.child.on('exit', (code, signal) => {
|
|
53
|
-
Log.coreLogger.info(`[ee-core] [jobs/child/forkProcess] from childProcess event-exit code:${code}, signal:${signal}`);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
this.child.on('error', (err) => {
|
|
57
|
-
Log.coreLogger.error(`[ee-core] [jobs/child/forkProcess] from childProcess event-error: ${err} !`);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 进程挂起
|
|
63
|
-
*/
|
|
64
|
-
sleep() {
|
|
65
|
-
if (this.sleeping) return;
|
|
66
|
-
process.kill(this.pid, 'SIGSTOP');
|
|
67
|
-
this.sleeping = true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 进程唤醒
|
|
72
|
-
*/
|
|
73
|
-
wakeup() {
|
|
74
|
-
if (!this.sleeping) return;
|
|
75
|
-
process.kill(this.pid, 'SIGCONT');
|
|
76
|
-
this.sleeping = false;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module.exports = ForkProcess;
|
package/jobs/childPool/index.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
const EventEmitter = require('events');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const ForkProcess = require('./forkProcess');
|
|
5
|
-
const Ps = require('../../ps');
|
|
6
|
-
const Loader = require('../../loader');
|
|
7
|
-
const Helper = require('../../utils/helper');
|
|
8
|
-
|
|
9
|
-
class ChildJob extends EventEmitter {
|
|
10
|
-
|
|
11
|
-
constructor() {
|
|
12
|
-
super();
|
|
13
|
-
this.jobList = new Map();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 执行一个job文件
|
|
18
|
-
*/
|
|
19
|
-
exec(filepath, opt = {}) {
|
|
20
|
-
const jobPath = this._getFullpath(filepath);
|
|
21
|
-
let options = Object.assign({
|
|
22
|
-
params: {},
|
|
23
|
-
}, opt);
|
|
24
|
-
|
|
25
|
-
// 消息对象
|
|
26
|
-
const mid = Helper.getRandomString();
|
|
27
|
-
let msg = {
|
|
28
|
-
mid,
|
|
29
|
-
jobPath,
|
|
30
|
-
params: options.params
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let subProcess = new ForkProcess(this, options);
|
|
34
|
-
|
|
35
|
-
// todo 是否会发生监听未完成时,接收不到消息?
|
|
36
|
-
// 发消息到子进程,
|
|
37
|
-
subProcess.child.send(msg);
|
|
38
|
-
|
|
39
|
-
return subProcess;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* todo 运行job
|
|
44
|
-
*/
|
|
45
|
-
run(name, filepath, opt = {}) {
|
|
46
|
-
let times = opt.times || 1;
|
|
47
|
-
|
|
48
|
-
for (let i = 1; i <= times; i++) {
|
|
49
|
-
this.exec(filepath, opt);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
_getFullpath(filepath) {
|
|
56
|
-
const isAbsolute = path.isAbsolute(filepath);
|
|
57
|
-
if (!isAbsolute) {
|
|
58
|
-
filepath = path.join(Ps.getBaseDir(), filepath);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const fullpath = Loader.resolveModule(filepath);
|
|
62
|
-
if (!fs.existsSync(fullpath)) {
|
|
63
|
-
throw new Error(`[ee-core] [jobs/child] file ${fullpath} not exists`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return fullpath;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
module.exports = ChildJob;
|
package/jobs/childPool/pool.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const EventEmitter = require('events');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const ForkProcess = require('./forkProcess');
|
|
5
|
-
const Ps = require('../../ps');
|
|
6
|
-
const Loader = require('../../loader');
|
|
7
|
-
const Log = require('../../log');
|
|
8
|
-
|
|
9
|
-
class ChildJob {
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* constructor
|
|
13
|
-
*/
|
|
14
|
-
constructor(name, filepath, opt) {
|
|
15
|
-
this.pools = new Map();
|
|
16
|
-
this.create(name, filepath, opt);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
_initEvents() {
|
|
20
|
-
// ddd
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
create(name, filepath, opt = {}) {
|
|
25
|
-
|
|
26
|
-
const isAbsolute = path.isAbsolute(filepath);
|
|
27
|
-
if (!isAbsolute) {
|
|
28
|
-
filepath = path.join(Ps.getBaseDir(), filepath);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const fullpath = Loader.resolveModule(filepath);
|
|
32
|
-
if (!fs.existsSync(fullpath)) {
|
|
33
|
-
throw new Error(`[ee-core] [jobs/child] file ${fullpath} not exists`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let options = Object.assign({
|
|
37
|
-
scriptArgs: {
|
|
38
|
-
name: name,
|
|
39
|
-
jobPath: fullpath
|
|
40
|
-
},
|
|
41
|
-
processArgs: [],
|
|
42
|
-
processOptions: {
|
|
43
|
-
//cwd: path.dirname(filepath),
|
|
44
|
-
env: Ps.allEnv(),
|
|
45
|
-
stdio: 'pipe'
|
|
46
|
-
}
|
|
47
|
-
}, opt);
|
|
48
|
-
|
|
49
|
-
const subProcess = new ForkProcess(this, options);
|
|
50
|
-
this.pools.set(subProcess.pid, subProcess);
|
|
51
|
-
|
|
52
|
-
return subProcess;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
sendToChild(pid, message, ...other) {
|
|
56
|
-
if (!this.pools.has(pid)) {
|
|
57
|
-
Log.coreLogger.warn(`[ee-core] [jobs/child] process dose not exist ${pid}`);
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const subProcess = this.pools.get(pid);
|
|
61
|
-
subProcess.child.send(message, ...other);
|
|
62
|
-
return
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = ChildJob;
|
|
File without changes
|