ee-core 2.0.0-beta.3 → 2.0.0-beta.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.
Files changed (60) hide show
  1. package/index.js +12 -13
  2. package/{lib → module/app}/application.js +2 -2
  3. package/{lib → module/app}/baseApp.js +3 -3
  4. package/{lib → module/app}/eeApp.js +2 -2
  5. package/module/app/index.js +5 -0
  6. package/{config → module/config}/config.default.js +2 -3
  7. package/module/const/channel.js +6 -0
  8. package/{core → module/core}/index.js +0 -2
  9. package/{core → module/core}/lib/ee.js +0 -2
  10. package/{core → module/core}/lib/loader/ee_loader.js +1 -1
  11. package/{core → module/core}/lib/loader/mixin/addon.js +1 -1
  12. package/module/core/lib/utils/base_context_class.js +34 -0
  13. package/module/exception/index.js +75 -6
  14. package/module/httpclient/index.js +2 -2
  15. package/module/jobs/baseJobClass.js +16 -0
  16. package/module/jobs/child/app.js +38 -18
  17. package/module/jobs/child/forkProcess.js +17 -38
  18. package/module/jobs/child/index.js +37 -20
  19. package/module/jobs/childPool/app.js +68 -0
  20. package/module/jobs/childPool/forkProcess.js +81 -0
  21. package/module/jobs/childPool/index.js +71 -0
  22. package/module/jobs/{child → childPool}/pool.js +3 -3
  23. package/module/jobs/renderer/loadView.js +1 -1
  24. package/module/jobs/unification.js +1 -1
  25. package/module/loader/index.js +36 -14
  26. package/module/log/logger.js +1 -1
  27. package/module/message/childMessage.js +11 -43
  28. package/module/message/index.js +2 -9
  29. package/{utils → module/oldUtils}/index.js +6 -6
  30. package/module/{utils/ps.js → ps/index.js} +20 -0
  31. package/module/service/index.js +34 -0
  32. package/module/socket/httpServer.js +1 -1
  33. package/module/socket/io.js +2 -2
  34. package/module/socket/ipcServer.js +3 -3
  35. package/module/socket/socketClient.js +2 -2
  36. package/module/socket/socketServer.js +3 -3
  37. package/module/storage/jsondbStorage.js +1 -1
  38. package/module/storage/sqliteStorage.js +1 -1
  39. package/{tools → module/tools}/encrypt.js +1 -1
  40. package/module/utils/helper.js +0 -1
  41. package/module/utils/index.js +1 -1
  42. package/package.json +3 -2
  43. package/module/message/ipcMain.js +0 -160
  44. package/module/message/ipcRender.js +0 -0
  45. package/module/message/manager.js +0 -0
  46. package/module/message/messenger.js +0 -0
  47. /package/{addon → module/addon}/window/index.js +0 -0
  48. /package/{lib → module/app}/appLoader.js +0 -0
  49. /package/{bin → module/bin}/tools.js +0 -0
  50. /package/{core/lib/utils/base_context_class.js → module/controller/index.js} +0 -0
  51. /package/{core → module/core}/lib/loader/context_loader.js +0 -0
  52. /package/{core → module/core}/lib/loader/file_loader.js +0 -0
  53. /package/{core → module/core}/lib/loader/mixin/config.js +0 -0
  54. /package/{core → module/core}/lib/loader/mixin/controller.js +0 -0
  55. /package/{core → module/core}/lib/loader/mixin/service.js +0 -0
  56. /package/{core → module/core}/lib/utils/function.js +0 -0
  57. /package/{core → module/core}/lib/utils/index.js +0 -0
  58. /package/{core → module/core}/lib/utils/sequencify.js +0 -0
  59. /package/{core → module/core}/lib/utils/timing.js +0 -0
  60. /package/{tools → module/tools}/replaceDist.js +0 -0
package/index.js CHANGED
@@ -1,38 +1,31 @@
1
- 'use strict';
2
-
3
1
  /**
4
2
  * @namespace EeCore
5
3
  */
6
-
7
- /**
8
- * @member {Appliaction} EeCore#Appliaction
9
- * @since 1.0.0
10
- */
11
- const Appliaction = require('./lib/application');
4
+ let app;
12
5
 
13
6
  /**
14
7
  * @member {Controller} EeCore#Controller
15
8
  * @since 1.0.0
16
9
  */
17
- const Controller = require('./core/lib/utils/base_context_class');
10
+ const Controller = require('./module/controller');
18
11
 
19
12
  /**
20
13
  * @member {Service} EeCore#Service
21
14
  * @since 1.0.0
22
15
  */
23
- const Service = require('./core/lib/utils/base_context_class');
16
+ const Service = require('./module/service');
24
17
 
25
18
  /**
26
19
  * @member {Storage}
27
20
  * @since 1.0.0
28
21
  */
29
- const Storage = require('./module/storage/index');
22
+ const Storage = require('./module/storage');
30
23
 
31
24
  /**
32
25
  * @member {Utils}
33
26
  * @since 1.0.0
34
27
  */
35
- const Utils = require('./utils/index');
28
+ const Utils = require('./module/oldUtils');
36
29
 
37
30
  /**
38
31
  * @member {Socket}
@@ -41,7 +34,13 @@ const Utils = require('./utils/index');
41
34
  const Socket = require('./module/socket/io');
42
35
 
43
36
  module.exports = {
44
- Appliaction,
37
+ get Appliaction () {
38
+ if (app) {
39
+ return app;
40
+ }
41
+ app = require('./module/app/application');
42
+ return app;
43
+ },
45
44
  Controller,
46
45
  Service,
47
46
  Storage,
@@ -1,3 +1,4 @@
1
+ const Exception = require('../exception');
1
2
  const {app} = require('electron');
2
3
  const path = require('path');
3
4
  const debug = require('debug')('ee-core:Appliaction');
@@ -6,6 +7,7 @@ const EeApp = require('./eeApp');
6
7
 
7
8
  class Appliaction extends EeApp {
8
9
  constructor() {
10
+ Exception.start();
9
11
  const { env } = process;
10
12
  let options = {
11
13
  env: 'prod',
@@ -80,8 +82,6 @@ class Appliaction extends EeApp {
80
82
  await this.ready();
81
83
 
82
84
  await this.createElectronApp();
83
-
84
- await this.catchLog();
85
85
  }
86
86
  }
87
87
 
@@ -3,11 +3,11 @@ const EE_PATH = Symbol.for('ee#eePath');
3
3
  const path = require('path');
4
4
  const EE_LOADER = Symbol.for('ee#loader');
5
5
  const AppLoader = require('./appLoader');
6
- const HttpClient = require('../module/httpclient');
6
+ const HttpClient = require('../httpclient');
7
7
  const HTTPCLIENT = Symbol('EeApplication#httpclient');
8
8
  const LOGGERS = Symbol('EeApplication#loggers');
9
- const Log = require('../module/log');
10
- const Storage = require('../module/storage');
9
+ const Log = require('../log');
10
+ const Storage = require('../storage');
11
11
 
12
12
  class BaseApp extends EeAppCore {
13
13
  constructor (options = {}) {
@@ -8,7 +8,7 @@ const Koa = require('koa');
8
8
  const koaServe = require('koa-static');
9
9
  const https = require('https');
10
10
  const BaseApp = require('./baseApp');
11
- const Log = require('../module/log');
11
+ const Log = require('../log');
12
12
 
13
13
  class EeApp extends BaseApp {
14
14
  constructor(options = {}) {
@@ -51,7 +51,7 @@ class EeApp extends BaseApp {
51
51
  * 启动通信模块
52
52
  */
53
53
  async startSocket () {
54
- const socket = require('../module/socket/start');
54
+ const socket = require('../socket/start');
55
55
  socket(this);
56
56
  }
57
57
 
@@ -0,0 +1,5 @@
1
+ const App = {
2
+
3
+ }
4
+
5
+ module.exports = App;
@@ -1,4 +1,3 @@
1
- 'use strict';
2
1
  const path = require('path');
3
2
 
4
3
  /**
@@ -222,7 +221,7 @@ module.exports = appInfo => {
222
221
  cert: ''
223
222
  },
224
223
  protocol: 'http://',
225
- host: '127.0.0.1',
224
+ host: 'localhost',
226
225
  port: 7071, // 默认端口(如果端口被使用,则随机获取一个)
227
226
  cors: {
228
227
  origin: "*"
@@ -239,7 +238,7 @@ module.exports = appInfo => {
239
238
  /* 主进程加载的地址 */
240
239
  config.mainServer = {
241
240
  protocol: 'http://',
242
- host: '127.0.0.1',
241
+ host: 'localhost',
243
242
  port: 7072, // 默认端口(如果端口被使用,则随机获取一个)
244
243
  options: {},
245
244
  ssl: {
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ process: {
3
+ showException: 'ee#showException',
4
+ sendToMain: 'ee#sendToMain'
5
+ }
6
+ };
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  const EeCore = require('./lib/ee');
4
2
  const EeLoader = require('./lib/loader/ee_loader');
5
3
  const BaseContextClass = require('./lib/utils/base_context_class');
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  const assert = require('assert');
4
2
  const fs = require('fs');
5
3
  const KoaApplication = require('koa');
@@ -9,7 +9,7 @@ const FileLoader = require('./file_loader');
9
9
  const ContextLoader = require('./context_loader');
10
10
  const Utils = require('../utils');
11
11
  const Timing = require('../utils/timing');
12
- const Ps = require('../../../module/utils/ps');
12
+ const Ps = require('../../../ps');
13
13
 
14
14
  const REQUIRE_COUNT = Symbol('EeLoader#requireCount');
15
15
 
@@ -13,7 +13,7 @@ module.exports = {
13
13
 
14
14
  // 加载ee-core的插件 和 用户插件
15
15
  const directorys = [
16
- path.join(this.options.framework, 'addon'),
16
+ path.join(this.options.framework, 'module', 'addon'),
17
17
  path.join(this.options.baseDir, 'addon'),
18
18
  ]
19
19
  opt = Object.assign({
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * BaseContextClass is a base class that can be extended,
5
+ * it's instantiated in context level,
6
+ * {@link Helper}, {@link Service} is extending it.
7
+ */
8
+ class BaseContextClass {
9
+
10
+ /**
11
+ * @class
12
+ * @param {Context} ctx - context instance
13
+ * @since 1.0.0
14
+ */
15
+ constructor(ctx) {
16
+ /**
17
+ * @member {Application} BaseContextClass#app
18
+ * @since 1.0.0
19
+ */
20
+ this.app = ctx;
21
+ /**
22
+ * @member {Config} BaseContextClass#config
23
+ * @since 1.0.0
24
+ */
25
+ this.config = ctx.config;
26
+ /**
27
+ * @member {Service} BaseContextClass#service
28
+ * @since 1.0.0
29
+ */
30
+ this.service = ctx.service;
31
+ }
32
+ }
33
+
34
+ module.exports = BaseContextClass;
@@ -1,16 +1,85 @@
1
1
  const Log = require('../log');
2
+ const Ps = require('../ps');
3
+ const Channel = require('../const/channel');
2
4
 
3
5
  /**
4
6
  * 捕获异常
5
7
  */
6
8
  exports.start = function() {
7
- process.on('uncaughtException', this.uncaughtExceptionHandler);
9
+ this.uncaughtExceptionHandler();
10
+ this.unhandledRejectionHandler();
8
11
  }
9
12
 
10
- exports.uncaughtExceptionHandler = function(err) {
11
- if (!(err instanceof Error)) {
12
- err = new Error(String(err));
13
+ /**
14
+ * 当进程上抛出异常而没有被捕获时触发该事件,并且使异常静默。
15
+ */
16
+ exports.uncaughtExceptionHandler = function() {
17
+ process.on('uncaughtException', function(err) {
18
+ if (!(err instanceof Error)) {
19
+ err = new Error(String(err));
20
+ }
21
+
22
+ if (err.name === 'Error') {
23
+ err.name = 'unhandledExceptionError';
24
+ }
25
+
26
+ Log.coreLogger.error(err);
27
+
28
+ devError(err);
29
+ });
30
+ }
31
+
32
+ /**
33
+ * 当进程上抛出异常而没有被捕获时触发该事件。
34
+ */
35
+ exports.uncaughtExceptionMonitorHandler = function() {
36
+ // process.on('uncaughtExceptionMonitor', function(err, origin) {
37
+ // if (!(err instanceof Error)) {
38
+ // err = new Error(String(err));
39
+ // }
40
+
41
+ // Log.coreLogger.error('uncaughtExceptionMonitor:',err);
42
+ // });
43
+ }
44
+
45
+ /**
46
+ * 当promise中reject的异常在同步任务中没有使用catch捕获就会触发该事件,
47
+ * 即便是在异步情况下使用了catch也会触发该事件
48
+ */
49
+ exports.unhandledRejectionHandler = function() {
50
+ process.on('unhandledRejection', function(err) {
51
+ if (!(err instanceof Error)) {
52
+ const newError = new Error(String(err));
53
+ // err maybe an object, try to copy the name, message and stack to the new error instance
54
+ if (err) {
55
+ if (err.name) newError.name = err.name;
56
+ if (err.message) newError.message = err.message;
57
+ if (err.stack) newError.stack = err.stack;
58
+ }
59
+ err = newError;
60
+ }
61
+ if (err.name === 'Error') {
62
+ err.name = 'unhandledRejectionError';
63
+ }
64
+
65
+ Log.coreLogger.error(err);
66
+
67
+ devError(err);
68
+ });
69
+ }
70
+
71
+ /**
72
+ * 如果是子进程,发送错误到主进程控制台
73
+ */
74
+ function devError (err) {
75
+ if (Ps.isForkedChild() && Ps.isDev()) {
76
+ let msgChannel = Channel.process.showException;
77
+ let errTips = (err && typeof err == 'object') ? err.toString() : '';
78
+ errTips += ' Error !!! Please See file ee-core.log or ee-error-xxx.log for details !'
79
+ let message = {
80
+ channel: msgChannel,
81
+ data: errTips
82
+ }
83
+ process.send(message);
13
84
  }
14
-
15
- Log.coreLogger.error(err);
16
85
  }
@@ -152,12 +152,12 @@ function normalizeConfig(httpConfig) {
152
152
  }
153
153
 
154
154
  if (config.httpAgent.timeout < 30000) {
155
- Log.coreLogger.warn('[ee-core] [module/httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
155
+ Log.coreLogger.warn('[ee-core] [httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
156
156
  config.httpAgent.timeout);
157
157
  config.httpAgent.timeout = 30000;
158
158
  }
159
159
  if (config.httpsAgent.timeout < 30000) {
160
- Log.coreLogger.warn('[ee-core] [module/httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
160
+ Log.coreLogger.warn('[ee-core] [httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
161
161
  config.httpsAgent.timeout);
162
162
  config.httpsAgent.timeout = 30000;
163
163
  }
@@ -0,0 +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,8 +1,24 @@
1
1
 
2
- const Exception = require('ee-core/module/exception');
2
+ const is = require('is-type-of');
3
+ let Exception;
4
+ let Loader;
5
+ let Log;
6
+ let UtilsCore;
7
+
8
+ // 开发环境下,ee-core是soft link
9
+ if (__dirname.indexOf("node_modules") == -1) {
10
+ Exception = require('../../exception');
11
+ Loader = require('../../loader');
12
+ Log = require('../../log');
13
+ UtilsCore = require('../../core/lib/utils');
14
+ } else {
15
+ Exception = require('ee-core/module/exception');
16
+ Loader = require('ee-core/module/loader');
17
+ Log = require('ee-core/module/log');
18
+ UtilsCore = require('../../core/lib/utils');
19
+ }
20
+
3
21
  Exception.start();
4
- const Loader = require('ee-core/module/loader');
5
- const Log = require('ee-core/module/log');
6
22
 
7
23
  class ChildApp {
8
24
  constructor() {
@@ -13,30 +29,34 @@ class ChildApp {
13
29
  * 初始化事件监听
14
30
  */
15
31
  _initEvents() {
16
- Log.info('[ee-core] [child-process] init Events');
17
-
18
- process.on('disconnect', () => {
19
- Log.coreLogger.info(`[ee-core] [module/message/childMessage] child process disconnected:${process.pid} !`);
20
- });
21
- process.on('exit', () => {
22
- Log.coreLogger.info(`[ee-core] [module/message/childMessage] child process exited:${process.pid} !`);
23
- });
24
32
  process.on('message', this._handleMessage.bind(this));
33
+ process.on('exit', (code) => {
34
+ Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from main-process, code:${code}, pid:${process.pid}`);
35
+ });
25
36
  }
26
37
 
27
38
  /**
28
39
  * 监听消息
29
40
  */
30
- _handleMessage(message) {
31
- Log.coreLogger.info(`[ee-core] [module/message/childMessage] Received a message ${message} from the mainProcess`);
32
-
33
- this.run(message);
41
+ _handleMessage(m) {
42
+ this.run(m);
43
+ Log.coreLogger.info(`[ee-core] [jobs/child] received a message from main-process, message: ${JSON.stringify(m)}`);
34
44
  }
35
45
 
46
+ /**
47
+ * 运行脚本
48
+ */
36
49
  run(msg = {}) {
37
- Log.coreLogger.info('[ee-core] [child-process] run');
38
-
39
- Loader.loadJobFile(msg.jobPath, msg.params);
50
+ let filepath = msg.jobPath;
51
+ let params = msg.jobParams;
52
+
53
+ let mod = Loader.loadJsFile(filepath);
54
+ if (is.class(mod) || UtilsCore.isBytecodeClass(mod)) {
55
+ let jobClass = new mod(params);
56
+ jobClass.handle();
57
+ } else if (is.function(mod)) {
58
+ mod(params);
59
+ }
40
60
  }
41
61
  }
42
62
 
@@ -1,20 +1,16 @@
1
1
  const path = require('path');
2
2
  const { fork } = require('child_process');
3
+ const serialize = require('serialize-javascript');
3
4
  const Log = require('../../log');
4
- const Ps = require('../../utils/ps');
5
+ const Ps = require('../../ps');
6
+ const Channel = require('../../const/channel');
5
7
 
6
8
  class ForkProcess {
7
9
  constructor(host, opt = {}) {
8
-
9
- let processCWD = Ps.getHomeDir();
10
- // if (Ps.isDev()) {
11
- // cwd = path.join(Ps.getHomeDir());
12
- // }
13
-
10
+
14
11
  let options = Object.assign({
15
- params: {},
16
12
  processOptions: {
17
- cwd: processCWD,
13
+ cwd: Ps.getHomeDir(),
18
14
  env: Ps.allEnv(),
19
15
  stdio: 'pipe'
20
16
  }
@@ -25,7 +21,7 @@ class ForkProcess {
25
21
  this.sleeping = false;
26
22
 
27
23
  // 传递给子进程的参数
28
- this.args.push(JSON.stringify(options.params));
24
+ //this.args.push(JSON.stringify(options.params));
29
25
 
30
26
  const appPath = path.join(__dirname, 'app.js');
31
27
  this.child = fork(appPath, this.args, options.processOptions);
@@ -38,45 +34,28 @@ class ForkProcess {
38
34
  * 进程初始化
39
35
  */
40
36
  _init() {
41
- this.child.on('message', (data) => {
42
- Log.coreLogger.info(`[ee-core] [module/jobs/child/forkProcess] from childProcess event-message ${data}`);
43
- });
37
+ this.child.on('message', (m) => {
38
+ Log.coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
39
+ if (m.channel == Channel.process.showException) {
40
+ Log.coreLogger.error(`${m.data}`);
41
+ }
44
42
 
45
- this.child.on('disconnect', () => {
46
- Log.coreLogger.info(`[ee-core] [module/jobs/child/forkProcess] from childProcess event-disconnect !`);
47
- });
43
+ // 收到子进程消息,转发到 event
44
+ if (m.channel == Channel.process.sendToMain) {
45
+ this.host.emit(m.event, m.data);
46
+ }
48
47
 
49
- this.child.on('close', (code, signal) => {
50
- Log.coreLogger.info(`[ee-core] [module/jobs/child/forkProcess] from childProcess event-close code:${code}, signal:${signal}`);
51
48
  });
52
49
 
53
50
  this.child.on('exit', (code, signal) => {
54
- Log.coreLogger.info(`[ee-core] [module/jobs/child/forkProcess] from childProcess event-exit code:${code}, signal:${signal}`);
51
+ Log.coreLogger.info(`[ee-core] [jobs/child] received a exit from child-process, code:${code}, signal:${signal}`);
55
52
  });
56
53
 
57
54
  this.child.on('error', (err) => {
58
- Log.coreLogger.error(`[ee-core] [module/jobs/child/forkProcess] from childProcess event-error :${err} !`);
55
+ Log.coreLogger.error(`[ee-core] [jobs/child] received a error from child-process, error: ${err} !`);
59
56
  });
60
57
  }
61
58
 
62
- /**
63
- * 进程挂起
64
- */
65
- sleep() {
66
- if (this.sleeping) return;
67
- process.kill(this.pid, 'SIGSTOP');
68
- this.sleeping = true;
69
- }
70
-
71
- /**
72
- * 进程唤醒
73
- */
74
- wakeup() {
75
- if (!this.sleeping) return;
76
- process.kill(this.pid, 'SIGCONT');
77
- this.sleeping = false;
78
- }
79
-
80
59
  }
81
60
 
82
61
  module.exports = ForkProcess;
@@ -2,46 +2,63 @@ const EventEmitter = require('events');
2
2
  const path = require('path');
3
3
  const fs = require('fs');
4
4
  const ForkProcess = require('./forkProcess');
5
- const Ps = require('../../utils/ps');
5
+ const Ps = require('../../ps');
6
6
  const Loader = require('../../loader');
7
+ const Helper = require('../../utils/helper');
7
8
 
8
9
  class ChildJob extends EventEmitter {
9
10
 
10
11
  constructor() {
11
12
  super();
12
- this.jobList = new Map();
13
+ this.initEvents();
13
14
  }
14
15
 
15
16
  /**
16
- * 运行任务
17
+ * 初始化监听
17
18
  */
18
- run(name, filepath, opt = {}) {
19
+ initEvents = () => {
20
+
21
+ // this.on('forked_message', ({data, id}) => {
22
+ // this.onMessage({data, id});
23
+ // });
24
+ }
19
25
 
26
+ /**
27
+ * 执行一个job文件
28
+ */
29
+ exec(filepath, params = {}, opt = {}) {
20
30
  const jobPath = this._getFullpath(filepath);
21
- let options = Object.assign({
22
- times: 1,
23
- params: {
24
- jobPath
25
- }
26
- }, opt);
27
31
 
28
32
  // 消息对象
33
+ const mid = Helper.getRandomString();
29
34
  let msg = {
30
- jobPath: jobPath,
31
- params: options.params
35
+ mid,
36
+ jobPath,
37
+ jobParams: params
32
38
  }
33
- let subProcess;
34
- for (let i = 1; i <= options.times; i++) {
35
- subProcess = new ForkProcess(this, options);
36
- this.jobList.set(name, i);
37
39
 
38
- // 发消息到子进程
39
- subProcess.child.send(msg);
40
- }
40
+ let subProcess = new ForkProcess(this, opt);
41
+
42
+ // todo 是否会发生监听未完成时,接收不到消息?
43
+ // 发消息到子进程
44
+ subProcess.child.send(msg);
41
45
 
42
46
  return subProcess;
43
47
  }
44
48
 
49
+ /**
50
+ * todo 运行job
51
+ */
52
+ run(name, filepath, opt = {}) {
53
+ let times = opt.times || 1;
54
+
55
+ for (let i = 1; i <= times; i++) {
56
+ this.exec(filepath, opt);
57
+ }
58
+
59
+ return;
60
+ }
61
+
45
62
  _getFullpath(filepath) {
46
63
  const isAbsolute = path.isAbsolute(filepath);
47
64
  if (!isAbsolute) {
@@ -50,7 +67,7 @@ class ChildJob extends EventEmitter {
50
67
 
51
68
  const fullpath = Loader.resolveModule(filepath);
52
69
  if (!fs.existsSync(fullpath)) {
53
- throw new Error(`[ee-core] [module/jobs/child] file ${fullpath} not exists`);
70
+ throw new Error(`[ee-core] [jobs/child] file ${fullpath} not exists`);
54
71
  }
55
72
 
56
73
  return fullpath;
@@ -0,0 +1,68 @@
1
+
2
+ const is = require('is-type-of');
3
+ let Exception;
4
+ let Loader;
5
+ let Log;
6
+ let UtilsCore;
7
+
8
+ // 开发环境下,ee-core是soft link
9
+ if (__dirname.indexOf("node_modules") == -1) {
10
+ Exception = require('../../exception');
11
+ Loader = require('../../loader');
12
+ Log = require('../../log');
13
+ UtilsCore = require('../../core/lib/utils');
14
+ } else {
15
+ Exception = require('ee-core/module/exception');
16
+ Loader = require('ee-core/module/loader');
17
+ Log = require('ee-core/module/log');
18
+ UtilsCore = require('../../core/lib/utils');
19
+ }
20
+
21
+ Exception.start();
22
+
23
+ class ChildApp {
24
+ constructor() {
25
+ this._initEvents();
26
+ }
27
+
28
+ /**
29
+ * 初始化事件监听
30
+ */
31
+ _initEvents() {
32
+ process.on('message', this._handleMessage.bind(this));
33
+ process.on('disconnect', () => {
34
+ Log.coreLogger.info(`[ee-core] [jobs/child] child process disconnected, pid:${process.pid}`);
35
+ });
36
+ process.on('exit', (code) => {
37
+ Log.coreLogger.info(`[ee-core] [jobs/child] child process exit code:${code}, pid:${process.pid}`);
38
+ });
39
+ }
40
+
41
+ /**
42
+ * 监听消息
43
+ */
44
+ _handleMessage(m) {
45
+ this.run(m);
46
+ Log.coreLogger.info(`[ee-core] [jobs/child] Received a message ${JSON.stringify(m)} from the mainProcess`);
47
+ }
48
+
49
+ /**
50
+ * 运行脚本
51
+ */
52
+ run(msg = {}) {
53
+ let filepath = msg.jobPath;
54
+ let params = msg.params;
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
+ Log.coreLogger.info('[ee-core] [child-process] job run');
65
+ }
66
+ }
67
+
68
+ new ChildApp();