ee-core 2.5.0-beta.3 → 2.6.0-beta.1

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,5 +1,4 @@
1
1
  const { app } = require('electron');
2
- const Window = require('../window');
3
2
  const EE = require('../../ee');
4
3
  const Log = require('../../log');
5
4
  const Electron = require('../index');
@@ -44,8 +44,12 @@ class ForkProcess {
44
44
  * 初始化事件监听
45
45
  */
46
46
  _init() {
47
+ const { messageLog } = this.host.config;
47
48
  this.child.on('message', (m) => {
48
- Log.coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
49
+ if (messageLog == true) {
50
+ Log.coreLogger.info(`[ee-core] [jobs/child] received a message from child-process, message: ${serialize(m)}`);
51
+ }
52
+
49
53
  if (m.channel == Channel.process.showException) {
50
54
  Log.coreLogger.error(`${m.data}`);
51
55
  }
@@ -2,12 +2,20 @@ const EventEmitter = require('events');
2
2
  const ForkProcess = require('./forkProcess');
3
3
  const Loader = require('../../loader');
4
4
  const Channel = require('../../const/channel');
5
+ const Conf = require('../../config');
5
6
 
6
7
  class ChildJob extends EventEmitter {
7
8
 
8
9
  constructor() {
9
10
  super();
10
11
  this.jobs = {};
12
+ this.config = {};
13
+
14
+ const cfg = Conf.getValue('jobs');
15
+ if (cfg) {
16
+ this.config = cfg;
17
+ }
18
+
11
19
  this._initEvents();
12
20
  }
13
21
 
@@ -4,6 +4,7 @@ const Loader = require('../../loader');
4
4
  const ForkProcess = require('../child/forkProcess');
5
5
  const Channel = require('../../const/channel');
6
6
  const Helper = require('../../utils/helper');
7
+ const Conf = require('../../config');
7
8
 
8
9
  class ChildPoolJob extends EventEmitter {
9
10
 
@@ -13,6 +14,7 @@ class ChildPoolJob extends EventEmitter {
13
14
  weights: [],
14
15
  }, opt);
15
16
 
17
+ this.config = {};
16
18
  this.boundMap = new Map();
17
19
  this.children = {};
18
20
  this.min = 3;
@@ -28,6 +30,12 @@ class ChildPoolJob extends EventEmitter {
28
30
  targets: [],
29
31
  }
30
32
  this.LB = new LoadBalancer(lbOpt);
33
+
34
+ const cfg = Conf.getValue('jobs');
35
+ if (cfg) {
36
+ this.config = cfg;
37
+ }
38
+
31
39
  this._initEvents();
32
40
  }
33
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.5.0-beta.3",
3
+ "version": "2.6.0-beta.1",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -80,19 +80,30 @@ class IpcServer {
80
80
 
81
81
  // send/on 模型
82
82
  ipcMain.on(channel, async (event, params) => {
83
- const fn = findFn(self.app, channel);
84
- const result = await fn.call(self.app, params, event);
85
-
86
- event.returnValue = result;
87
- event.reply(`${channel}`, result);
83
+ try {
84
+ const fn = findFn(self.app, channel);
85
+ const result = await fn.call(self.app, params, event);
86
+
87
+ event.returnValue = result;
88
+ event.reply(`${channel}`, result);
89
+ } catch(e) {
90
+ Log.coreLogger.error('[ee-core] [socket/IpcServer] send/on throw error:', e);
91
+ // event.returnValue = e;
92
+ // event.reply(`${channel}`, e);
93
+ }
88
94
  });
89
95
 
90
96
  // invoke/handle 模型
91
97
  ipcMain.handle(channel, async (event, params) => {
92
- const fn = findFn(self.app, channel);
93
- const result = await fn.call(self.app, params, event);
94
-
95
- return result;
98
+ try {
99
+ const fn = findFn(self.app, channel);
100
+ const result = await fn.call(self.app, params, event);
101
+
102
+ return result;
103
+ } catch(e) {
104
+ Log.coreLogger.error('[ee-core] [socket/IpcServer] invoke/handle throw error:', e);
105
+ return e
106
+ }
96
107
  });
97
108
  }
98
109
  }
package/storage/index.js CHANGED
@@ -2,6 +2,8 @@ const assert = require('assert');
2
2
  const _ = require('lodash');
3
3
  const DB = {};
4
4
 
5
+ // jsondb 要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
6
+
5
7
  DB.connection = function (database, options = {}) {
6
8
  let driver = options.driver || 'jsondb';
7
9
 
@@ -6,6 +6,15 @@ class Base {
6
6
  this.deserialize = JSON.parse
7
7
  }
8
8
 
9
+ _canDeserialized(obj) {
10
+ try {
11
+ this.deserialize(obj)
12
+ return true
13
+ } catch (e) {
14
+ return false
15
+ }
16
+ }
17
+
9
18
  _stringify(obj) {
10
19
  return JSON.stringify(obj, null, 2)
11
20
  }
@@ -1,30 +1,50 @@
1
1
  const Base = require('./Base')
2
2
  const fs = require('fs')
3
+ const Log = require('../../../log')
3
4
 
4
5
  class FileSync extends Base {
6
+
7
+ constructor(options = {}) {
8
+ const { source, isSysDB } = options;
9
+ super(source);
10
+ this.isSysDB = isSysDB;
11
+ }
5
12
 
6
13
  read() {
7
14
  if (fs.existsSync(this.source)) {
8
15
  // Read database
9
- try {
10
- const data = fs.readFileSync(this.source, {encoding: 'utf8'}).trim()
16
+ const data = fs.readFileSync(this.source, {encoding: 'utf8'}).trim();
17
+
18
+ const canDeserialized = this._canDeserialized(data);
19
+ if (!canDeserialized) {
20
+ const errMessage = `Malformed JSON in file: ${this.source}\n${data}`;
21
+ console.error(errMessage)
11
22
 
12
- // Handle blank file
13
- return data ? this.deserialize(data) : this.defaultValue
14
- } catch (e) {
15
- if (e instanceof SyntaxError) {
16
- e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
23
+ // reset system.json
24
+ if (this.isSysDB) {
25
+ this._fsWrite(this.defaultValue);
17
26
  }
18
- throw e
19
27
  }
28
+ const value = canDeserialized ? this.deserialize(data) : this.defaultValue;
29
+ return value;
20
30
  } else {
21
31
  // Initialize
22
- fs.writeFileSync(this.source, this.serialize(this.defaultValue), {flag:'w+'})
32
+ this._fsWrite(this.defaultValue);
23
33
  return this.defaultValue
24
34
  }
25
35
  }
26
36
 
27
37
  write(data) {
38
+ return this._fsWrite(data);
39
+ }
40
+
41
+ _fsWrite(data) {
42
+ const isObject = Object.prototype.toString.call(data) === '[object Object]';
43
+ if (!isObject) {
44
+ Log.coreLogger.error('[ee-core] [storage/jsondb] Variable is not an object :', data);
45
+ return
46
+ }
47
+
28
48
  return fs.writeFileSync(this.source, this.serialize(data), {flag:'w+'})
29
49
  }
30
50
  }
@@ -28,7 +28,12 @@ class JsondbStorage {
28
28
  */
29
29
  table() {
30
30
  const dbFile = this.getFilePath();
31
- const adapter = new FileSync(dbFile);
31
+ const isSysDB = this.isSystemDB();
32
+ const opt = {
33
+ source: dbFile,
34
+ isSysDB: isSysDB
35
+ }
36
+ const adapter = new FileSync(opt);
32
37
  const db = Jsondb(adapter);
33
38
 
34
39
  assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
@@ -36,7 +41,7 @@ class JsondbStorage {
36
41
  return db;
37
42
  }
38
43
 
39
- /**
44
+ /**
40
45
  * 补全扩展名
41
46
  */
42
47
  _addExtname(name) {
@@ -73,6 +78,13 @@ class JsondbStorage {
73
78
  return fileName;
74
79
  }
75
80
 
81
+ /**
82
+ * is system db
83
+ */
84
+ isSystemDB() {
85
+ return (this.name == 'system.json') ? true : false;
86
+ }
87
+
76
88
  /**
77
89
  * 获取文件绝对路径
78
90
  */