ee-core 2.0.0-beta.1 → 2.0.0-beta.3

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.
@@ -1,33 +1,61 @@
1
- //require('bytenode');
1
+ const EventEmitter = require('events');
2
2
  const path = require('path');
3
+ const fs = require('fs');
3
4
  const ForkProcess = require('./forkProcess');
4
5
  const Ps = require('../../utils/ps');
5
- const Constants = require('../../const');
6
+ const Loader = require('../../loader');
6
7
 
7
- class ChildJob {
8
+ class ChildJob extends EventEmitter {
9
+
10
+ constructor() {
11
+ super();
12
+ this.jobList = new Map();
13
+ }
8
14
 
9
15
  /**
10
- * constructor
11
- * @param {String} name - job name
12
- * @param {String} filepath - filepath
13
- * @param {Object} opt - child process options
14
- */
15
- constructor(name, filepath, opt = {}) {
16
+ * 运行任务
17
+ */
18
+ run(name, filepath, opt = {}) {
19
+
20
+ const jobPath = this._getFullpath(filepath);
16
21
  let options = Object.assign({
17
- processArgs: Ps.isDev() ? [`--inspect=${Constants.jobs.inspectStartIndex}`] : [],
18
- processOptions: {
19
- //cwd: path.dirname(filepath),
20
- env: Ps.allEnv(),
21
- stdio: 'pipe'
22
+ times: 1,
23
+ params: {
24
+ jobPath
22
25
  }
23
26
  }, opt);
24
27
 
25
- this.childProcess = new ForkProcess(this, filepath, options.processArgs, options.processOptions);
28
+ // 消息对象
29
+ let msg = {
30
+ jobPath: jobPath,
31
+ params: options.params
32
+ }
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
+
38
+ // 发消息到子进程
39
+ subProcess.child.send(msg);
40
+ }
41
+
42
+ return subProcess;
43
+ }
44
+
45
+ _getFullpath(filepath) {
46
+ const isAbsolute = path.isAbsolute(filepath);
47
+ if (!isAbsolute) {
48
+ filepath = path.join(Ps.getBaseDir(), filepath);
49
+ }
26
50
 
27
- this.jobReady = false;
28
- this.exec = filepath;
29
- this.name = name;
51
+ const fullpath = Loader.resolveModule(filepath);
52
+ if (!fs.existsSync(fullpath)) {
53
+ throw new Error(`[ee-core] [module/jobs/child] file ${fullpath} not exists`);
54
+ }
55
+
56
+ return fullpath;
30
57
  }
58
+
31
59
  }
32
60
 
33
61
  module.exports = ChildJob;
@@ -0,0 +1,67 @@
1
+ const EventEmitter = require('events');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const ForkProcess = require('./forkProcess');
5
+ const Ps = require('../../utils/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] [module/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] [module/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;
@@ -1,55 +1,7 @@
1
- const path = require('path');
2
- const fs = require('fs');
3
- const assert = require('assert');
4
- const RendererJob = require('./renderer');
5
1
  const ChildJob = require('./child');
6
- const Utils = require('../utils');
7
- const Loader = require('../loader');
8
-
9
- class Jobs {
10
- constructor() {
11
- this.type = undefined;
12
- this.instance = undefined;
13
- }
14
-
15
- /**
16
- * 创建 job
17
- */
18
- create (name, opt = {}) {
19
- this.type = opt.type || 'child';
20
- this.dev = opt.dev || false;
21
- this.winOptions = opt.winOptions || {};
22
- this.childOptions = opt.childOptions || {};
23
- this.path = opt.path || null;
24
-
25
-
26
- const isAbsolute = path.isAbsolute(this.path);
27
- if (!isAbsolute) {
28
- this.path = path.join(Utils.getBaseDir(), 'jobs', this.path);
29
- }
30
- const filepath = Loader.resolveModule(this.path);
31
-
32
- assert(fs.existsSync(filepath), `file ${filepath} not exists`);
33
-
34
- this.path = filepath;
35
- if (this.type == 'child') {
36
- this.instance = new ChildJob(name, filepath, this.childOptions);
37
- } else if (this.type == 'renderer') {
38
- this.instance = new RendererJob(name, filepath, this.winOptions);
39
- if (this.dev) {
40
- this.openDevTools();
41
- }
42
- }
43
-
44
- return;
45
- }
46
-
47
- /**
48
- * 显示开发者工具栏(仅支持 RendererJob)
49
- */
50
- openDevTools () {
51
- this.instance.openDevTools();
52
- }
53
- }
2
+ const RendererJob = require('./renderer');
54
3
 
55
- module.exports = Jobs;
4
+ module.exports = {
5
+ ChildJob,
6
+ RendererJob
7
+ };
@@ -0,0 +1,64 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const RendererJob = require('./renderer');
4
+ const ChildJob = require('./child/pool');
5
+ const Ps = require('../utils/ps');
6
+ const Loader = require('../loader');
7
+
8
+ class Jobs {
9
+ constructor() {
10
+ this.type;
11
+ this.dev;
12
+ this.path;
13
+ this.instance;
14
+ this.child;
15
+ this.childOptions;
16
+ this.renderer;
17
+ this.winOptions;
18
+ }
19
+
20
+ /**
21
+ * 创建 job
22
+ */
23
+ create (name, opt = {}) {
24
+ this.type = opt.type || 'child';
25
+ this.dev = opt.dev || false;
26
+ this.winOptions = opt.winOptions || {};
27
+ this.childOptions = opt.childOptions || {};
28
+ this.path = opt.path || null;
29
+
30
+ const isAbsolute = path.isAbsolute(this.path);
31
+ if (!isAbsolute) {
32
+ this.path = path.join(Ps.getBaseDir(), this.path);
33
+ }
34
+ const filepath = Loader.resolveModule(this.path);
35
+
36
+ if (!fs.existsSync(filepath)) {
37
+ throw new Error(`[ee-core] [jobs-create] file ${this.path} not exists`);
38
+ }
39
+
40
+ this.path = filepath;
41
+ if (this.type == 'child') {
42
+ this.instance = new ChildJob(name, filepath, this.childOptions);
43
+ this.child = this.instance;
44
+ } else if (this.type == 'renderer') {
45
+ this.instance = new RendererJob(name, filepath, this.winOptions);
46
+ this.renderer = this.instance;
47
+
48
+ if (this.dev) {
49
+ this.openDevTools();
50
+ }
51
+ }
52
+
53
+ return;
54
+ }
55
+
56
+ /**
57
+ * 显示开发者工具栏(仅支持 RendererJob)
58
+ */
59
+ openDevTools () {
60
+ this.instance.openDevTools();
61
+ }
62
+ }
63
+
64
+ module.exports = Jobs;
@@ -3,6 +3,7 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const UtilsCore = require('../../core/lib/utils');
5
5
  const Ps = require('../utils/ps');
6
+ const Log = require('../log');
6
7
 
7
8
  module.exports = {
8
9
 
@@ -14,10 +15,17 @@ module.exports = {
14
15
  * @return {Object} exports
15
16
  * @since 1.0.0
16
17
  */
17
- loadFile (filepath, ...inject) {
18
+ loadOneFile (filepath, ...inject) {
19
+ const isAbsolute = path.isAbsolute(filepath);
20
+ if (!isAbsolute) {
21
+ filepath = path.join(Ps.getBaseDir(), filepath);
22
+ }
23
+
18
24
  filepath = filepath && this.resolveModule(filepath);
19
- if (!filepath) {
20
- return null;
25
+ if (!fs.existsSync(filepath)) {
26
+ let errorMsg = `[ee-core] [module/loader/index] loadOneFile ${filepath} does not exist`;
27
+ Log.coreLogger.error(errorMsg);
28
+ throw new Error(errorMsg);
21
29
  }
22
30
 
23
31
  const ret = UtilsCore.loadFile(filepath);
@@ -28,26 +36,51 @@ module.exports = {
28
36
  },
29
37
 
30
38
  /**
31
- * 模块的绝对路径
39
+ * 加载job文件
40
+ *
41
+ * @param {String} filepath - fullpath
42
+ * @param {Array} inject - pass rest arguments into the function when invoke
43
+ * @return {Object} exports
44
+ * @since 1.0.0
32
45
  */
33
- resolveModule(filepath) {
34
- const isAbsolute = path.isAbsolute(filepath);
35
- if (!isAbsolute) {
36
- filepath = path.join(Ps.getBaseDir(), 'jobs', filepath);
46
+ loadJobFile (filepath, ...inject) {
47
+ if (!fs.existsSync(filepath)) {
48
+ let warnMsg = `[ee-core] [module/loader] loadJobFile ${filepath} does not exist`;
49
+ Log.coreLogger.error(warnMsg);
50
+ }
51
+
52
+ const ret = UtilsCore.loadFile(filepath);
53
+ if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
54
+ ret = ret(...inject);
37
55
  }
56
+ return ret;
57
+ },
38
58
 
39
- let fullPath;
59
+ /**
60
+ * 模块的绝对路径
61
+ * @param {String} filepath - fullpath
62
+ */
63
+ resolveModule(filepath) {
64
+ let fullpath;
40
65
  try {
41
- fullPath = require.resolve(filepath);
66
+ fullpath = require.resolve(filepath);
42
67
  } catch (e) {
43
- let jscFile = filepath + '.jsc';
44
- if (fs.existsSync(jscFile)) {
45
- return jscFile;
68
+
69
+ // 特殊后缀处理
70
+ if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
71
+ fullpath = filepath + '.jsc';
72
+ } else if (filepath && filepath.endsWith('.js')) {
73
+ fullpath = filepath + 'c';
74
+ }
75
+
76
+ if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
77
+ let files = { filepath, fullpath }
78
+ Log.coreLogger.warn(`[ee-core] [module/loader] resolveModule unknow filepath: ${files}`)
79
+ return undefined;
46
80
  }
47
- return undefined;
48
81
  }
49
82
 
50
- return fullPath;
83
+ return fullpath;
51
84
  },
52
85
 
53
86
  /**
@@ -57,16 +90,35 @@ module.exports = {
57
90
  * @return {Object} exports
58
91
  * @since 1.0.0
59
92
  */
60
- requireModule (filepath) {
61
- filepath = filepath && this.resolveModule(filepath);
62
- if (!filepath) {
63
- return null;
93
+ requireModule (filepath, type = '') {
94
+ let fullpath;
95
+ const isAbsolute = path.isAbsolute(filepath);
96
+ if (!isAbsolute) {
97
+ filepath = path.join(Ps.getBaseDir(), type, filepath);
64
98
  }
65
- const ret = UtilsCore.loadFile(filepath);
99
+
100
+ fullpath = this.resolveModule(filepath);
101
+ if (!fs.existsSync(fullpath)) {
102
+ let errorMsg = `[ee-core] [module/loader] requireModule filepath: ${filepath} does not exist`;
103
+ Log.coreLogger.error(errorMsg);
104
+ }
105
+ const ret = UtilsCore.loadFile(fullpath);
66
106
 
67
107
  return ret;
68
108
  },
69
109
 
110
+ /**
111
+ * 加载jobs模块(子进程中使用)
112
+ *
113
+ * @param {String} filepath - fullpath
114
+ * @return {Object} exports
115
+ * @since 1.0.0
116
+ */
117
+ requireJobsModule (filepath) {
118
+ const ret = this.requireModule(filepath, 'jobs');
119
+
120
+ return ret;
121
+ },
70
122
  }
71
123
 
72
124
 
@@ -0,0 +1,63 @@
1
+ const Log = require('../log');
2
+
3
+ /**
4
+ * ChildMessage
5
+ */
6
+ class ChildMessage {
7
+ constructor() {
8
+ // ...
9
+ }
10
+
11
+ /**
12
+ * 初始化事件监听
13
+ */
14
+ initEvents() {
15
+ process.on('disconnect', () => {
16
+ Log.coreLogger.info(`[ee-core] [module/message/childMessage] child process disconnected:${process.pid} !`);
17
+ });
18
+ process.on('exit', () => {
19
+ Log.coreLogger.info(`[ee-core] [module/message/childMessage] child process exited:${process.pid} !`);
20
+ });
21
+ }
22
+
23
+ /**
24
+ * 监听消息
25
+ */
26
+ onMessage(handle) {
27
+ Log.coreLogger.info(`[ee-core] [module/message/childMessage] Received a message ${params} from the mainProcess`);
28
+
29
+ process.on('message', handle.bind(this));
30
+ }
31
+
32
+ /**
33
+ * 消息处理
34
+ */
35
+ // _handleMessage(params = {}) {
36
+ // Log.coreLogger.info(`[ee-core] [module/message/childMessage] Received a message ${params} from the mainProcess`);
37
+ // }
38
+
39
+ /**
40
+ * 向主进程发消息
41
+ */
42
+ sendToMain(message, ...other) {
43
+ return process.send(message, ...other);
44
+ }
45
+
46
+ /**
47
+ * 断开连接
48
+ */
49
+ disconnect() {
50
+ process.disconnect();
51
+ }
52
+
53
+ /**
54
+ * 进程退出
55
+ */
56
+ exit() {
57
+ process.exit();
58
+ }
59
+ }
60
+
61
+
62
+
63
+ module.exports = ChildMessage;
@@ -1,13 +1,28 @@
1
- const { isDev, isRenderer, isMain } = require('../utils/ps');
1
+ const Ps = require('../utils/ps');
2
+ const EEChildMessage = Symbol('EeCore#Module#ChildMessage');
2
3
 
3
- const m = {};
4
+ const message = {
4
5
 
5
- if (isMain) {
6
- m.d = require();
7
- }
6
+ /**
7
+ *
8
+ */
9
+ create () {
8
10
 
9
- if (isRenderer) {
10
- m.d = require();
11
- }
11
+ },
12
12
 
13
- module.exports = m;
13
+ /**
14
+ * childMessage
15
+ */
16
+ get childMessage() {
17
+ if (!this[EEChildMessage]) {
18
+ this[EEChildMessage] = Logger.create();
19
+ }
20
+
21
+ return this[EEChildMessage] || null;
22
+ },
23
+
24
+ };
25
+
26
+
27
+
28
+ module.exports = message;
File without changes
File without changes
@@ -9,6 +9,7 @@ const https = require('https');
9
9
  const fs = require('fs');
10
10
  const path = require('path');
11
11
  const _ = require('lodash');
12
+ const Log = require('../log');
12
13
 
13
14
  /**
14
15
  * http server
@@ -60,16 +61,16 @@ class HttpServer {
60
61
  })
61
62
  .use(this.dispatch);
62
63
 
63
- let msg = '[ee-core:http:server] server is: ' + url;
64
+ let msg = '[ee-core] [module/socket/httpServer] server is: ' + url;
64
65
  if (isHttps) {
65
66
  https.createServer(sslOptions, koaApp.callback()).listen(httpServer.port, (err) => {
66
67
  msg = err ? err : msg;
67
- self.app.coreLogger.info(msg);
68
+ Log.coreLogger.info(msg);
68
69
  });
69
70
  } else {
70
71
  koaApp.listen(httpServer.port, (e) => {
71
72
  msg = e ? e : msg;
72
- self.app.coreLogger.info(msg);
73
+ Log.coreLogger.info(msg);
73
74
  });
74
75
  }
75
76
  }
@@ -1,5 +1,4 @@
1
1
  const debug = require('debug')('ee-core:ipcServer');
2
- const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
3
2
  const is = require('is-type-of');
4
3
  const { ipcMain } = require('electron');
5
4
  const path = require('path');
@@ -7,18 +6,16 @@ const fs = require('fs');
7
6
  const globby = require('globby');
8
7
  const Utils = require('../../core/lib/utils');
9
8
  const Wrap = require('../utils/wrap');
9
+ const Log = require('../log');
10
10
 
11
11
  class IpcServer {
12
12
  constructor (app) {
13
-
14
13
  this.app = app;
15
- this.consoleLogger = new EggConsoleLogger();
16
- this.consoleLogger.info('[ee-core:socket:ipcMain] start ipcMain');
17
14
  this.register();
18
15
  }
19
16
 
20
17
  register () {
21
- this.consoleLogger.info('[ee-core:socket:ipcMain] register channels');
18
+ console.log('[ee-core] [module/socket/IpcServer] register channels');
22
19
 
23
20
  const self = this;
24
21
  // 遍历方法
@@ -78,7 +75,7 @@ class IpcServer {
78
75
 
79
76
  return fn;
80
77
  } catch (err) {
81
- app.logger.error('[ee:socket:ipcMain] throw error:', err);
78
+ Log.coreLogger.error('[ee-core] [module/socket/IpcServer] throw error:', err);
82
79
  }
83
80
  return null;
84
81
  }
@@ -3,19 +3,16 @@
3
3
  const assert = require('assert');
4
4
  const IoClient = require('socket.io-client');
5
5
  const Constants = require('../const');
6
- const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
7
6
  const Storage = require('../storage');
8
7
 
9
8
  class SocketClient {
10
9
  constructor (port) {
11
- this.consoleLogger = new EggConsoleLogger();
12
10
  port = port ? parseInt(port) : this.getSocketcPort();
13
11
 
14
12
  assert(typeof port === 'number', 'port required, and must be a number');
15
- this.consoleLogger.info('[ee-core:socket:client] start client');
16
13
 
17
14
  const url = 'http://127.0.0.1:' + port;
18
- this.consoleLogger.info('[ee-core:socket:client] url:', url);
15
+ console.log('[ee-core] [module/socket/socketClient] url:', url);
19
16
  this.client = IoClient(url);
20
17
  }
21
18
 
@@ -2,10 +2,10 @@
2
2
 
3
3
  const assert = require('assert');
4
4
  const { Server } = require('socket.io');
5
- const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
6
5
  const is = require('is-type-of');
7
6
  const Storage = require('../storage');
8
7
  const Constants = require('../const');
8
+ const Log = require('../log');
9
9
 
10
10
  /**
11
11
  * socket server
@@ -14,7 +14,6 @@ class SocketServer {
14
14
  constructor (app) {
15
15
  this.app = app;
16
16
  const options = this.app.config.socketServer;
17
- this.consoleLogger = new EggConsoleLogger();
18
17
 
19
18
  if (!options.enable) {
20
19
  return;
@@ -22,7 +21,7 @@ class SocketServer {
22
21
 
23
22
  let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketPort());
24
23
  assert(typeof port === 'number', 'socekt port required, and must be a number');
25
- this.consoleLogger.info('[ee-core:socket:server] socket server port is:', port);
24
+ this.consoleLogger.info('[ee-core] [module/socket/socketServer] port is:', port);
26
25
 
27
26
  // let opt = Object.assign({}, options);
28
27
  // delete opt.enable;
@@ -32,11 +31,10 @@ class SocketServer {
32
31
 
33
32
  connec () {
34
33
  const self = this;
35
- this.consoleLogger.info('[ee-core:socket:server] connection .....');
36
34
  this.io.on('connection', (socket) => {
37
35
  const channel = Constants.socketIo.channel.partySoftware;
38
36
  socket.on(channel, async (message, callback) => {
39
- self.consoleLogger.info('[ee-core:socket:server] socket id:' + socket.id + ' message cmd: ' + message.cmd);
37
+ console.log('[ee-core] [module/socket/socketServer] socket id:' + socket.id + ' message cmd: ' + message.cmd);
40
38
 
41
39
  try {
42
40
  // 找函数
@@ -57,7 +55,7 @@ class SocketServer {
57
55
  const result = await fn.call(self.app, args);
58
56
  callback(result);
59
57
  } catch (err) {
60
- self.app.console.error('[ee-core:socket:server] throw error:', err);
58
+ Log.coreLogger.error('[ee-core] [module/socket/socketServer] throw error:', err);
61
59
  }
62
60
  });
63
61
  });
@@ -4,8 +4,12 @@ const DB = {};
4
4
 
5
5
  DB.connection = function (database, options = {}) {
6
6
  let driver = options.driver || 'jsondb';
7
+
8
+ // 兼容之前api
9
+ driver = driver == 'lowdb' ? 'jsondb' : driver;
10
+
7
11
  let opt = options.default || {};
8
- if (!_.includes(['jsondb', 'lowdb', 'sqlite'], driver)) {
12
+ if (!_.includes(['jsondb', 'sqlite'], driver)) {
9
13
  assert(database, `db driver ${driver} is not supported`);
10
14
  }
11
15
  if (_.isEmpty(database)) {
@@ -21,9 +25,7 @@ DB.connection = function (database, options = {}) {
21
25
  const SqliteStorage = require('./sqliteStorage');
22
26
  storage = new SqliteStorage(database, opt);
23
27
  break;
24
- default:
25
- const JsondbStorage = require('./jsondbStorage');
26
- storage = new JsondbStorage(database);
28
+ default:
27
29
  }
28
30
 
29
31
  return storage;