jsir 2.4.3 → 2.4.5
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/cmd/oaa.js +10 -3
- package/deps/evalCode.js +1 -5
- package/deps/room.js +34 -10
- package/deps/setting.js +4 -2
- package/deps/util.js +7 -1
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -13,7 +13,7 @@ const {
|
|
|
13
13
|
getFullPath, parseUniqueName, toUniqueName, isJsirFileName, toJsirFileName,
|
|
14
14
|
getAlias, wrapperJsirText, eia, getKeyTips, getValTips, getJsirTypeKey,
|
|
15
15
|
createDetachedProcess, interceptStdStreams,
|
|
16
|
-
draftModify
|
|
16
|
+
draftModify, isRunningInBackground, fileJson, fileLock
|
|
17
17
|
} = $lib;
|
|
18
18
|
const _args = process.argv.slice(2).map(trim);
|
|
19
19
|
const evalCode = require('../deps/evalCode')
|
|
@@ -33,7 +33,6 @@ const console = createConsole();
|
|
|
33
33
|
const room = require('../deps/room');
|
|
34
34
|
const server = require('../deps/server')
|
|
35
35
|
const {reqNode} = require("../deps/room");
|
|
36
|
-
const {isRunningInBackground} = require("../deps/util");
|
|
37
36
|
|
|
38
37
|
let lastFilterArg = '';
|
|
39
38
|
let _cmdMapFile = setting.name + 'CmdMap.json'
|
|
@@ -117,6 +116,12 @@ const $data = {
|
|
|
117
116
|
}
|
|
118
117
|
return _dealOnLazyGet(key, onLazyTempCode)
|
|
119
118
|
},
|
|
119
|
+
gfs: (key, fn, onLazyTempCode) => {
|
|
120
|
+
if (!vl(_data[key])) {
|
|
121
|
+
return _dataSet(key, fn(), onLazyTempCode)
|
|
122
|
+
}
|
|
123
|
+
return _dealOnLazyGet(key, onLazyTempCode)
|
|
124
|
+
},
|
|
120
125
|
set: _dataSet,
|
|
121
126
|
del: (key) => {
|
|
122
127
|
let val = _data[key]
|
|
@@ -124,7 +129,9 @@ const $data = {
|
|
|
124
129
|
return val
|
|
125
130
|
},
|
|
126
131
|
key: () => Object.keys(_data),
|
|
127
|
-
has: (key) => vl(_data[key])
|
|
132
|
+
has: (key) => vl(_data[key]),
|
|
133
|
+
json: fileJson,
|
|
134
|
+
lock: fileLock
|
|
128
135
|
}
|
|
129
136
|
const $homeDir = getLibDataDir()
|
|
130
137
|
|
package/deps/evalCode.js
CHANGED
|
@@ -13,10 +13,6 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
13
13
|
const require = (moduleName) => $lib.requireG(moduleName, $currentSpace);
|
|
14
14
|
|
|
15
15
|
const $config = $lib.createConfig($cmdName);
|
|
16
|
-
const $file = {
|
|
17
|
-
json: $lib.fileJson,
|
|
18
|
-
lock: $lib.fileLock
|
|
19
|
-
};
|
|
20
16
|
|
|
21
17
|
// 兼容
|
|
22
18
|
$text = $lib.wrapperJsirText($text)
|
|
@@ -27,7 +23,7 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
27
23
|
|
|
28
24
|
const $context = {
|
|
29
25
|
$defArgs,
|
|
30
|
-
$data, $config,
|
|
26
|
+
$data, $config,
|
|
31
27
|
$require, $requires, $import,
|
|
32
28
|
$text, $cmdName, $args,
|
|
33
29
|
$nextLine, $nextText,
|
package/deps/room.js
CHANGED
|
@@ -7,7 +7,7 @@ const {setRoute, createSign} = require('../deps/server')
|
|
|
7
7
|
const roomDataFile = "jsirRoom.json"
|
|
8
8
|
const roomsDirLockKey = "RW_" + getRoomsDir();
|
|
9
9
|
const syncRoomsLockKey = "SyncRooms_" + getRoomsDir();
|
|
10
|
-
const
|
|
10
|
+
const updateRoomInfoLockKey = "UPDATE_" + roomDataFile
|
|
11
11
|
const console = createConsole();
|
|
12
12
|
const net = require("net");
|
|
13
13
|
const setting = require('../deps/setting')
|
|
@@ -54,7 +54,7 @@ function onRoom(wrapperInput) {
|
|
|
54
54
|
await wrapperInput(req.input)
|
|
55
55
|
let outputs = setting.enterOutputs || [];
|
|
56
56
|
setting.enterOutputs = null;
|
|
57
|
-
res.output = outputs.join('
|
|
57
|
+
res.output = outputs.join('');
|
|
58
58
|
}
|
|
59
59
|
})
|
|
60
60
|
} catch (e) {
|
|
@@ -104,7 +104,7 @@ async function getTailscaleNodes() {
|
|
|
104
104
|
return [...new Set(nodes)];
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
async function
|
|
107
|
+
async function updateRoomInfo() {
|
|
108
108
|
let nodes = await getTailscaleNodes();
|
|
109
109
|
let ip = getSelfIP(nodes)
|
|
110
110
|
await fileJson(roomDataFile, async room => {
|
|
@@ -121,6 +121,8 @@ async function enrichRoomInfo() {
|
|
|
121
121
|
|
|
122
122
|
// 提前设置一下
|
|
123
123
|
setting.selfRoom = Object.assign(setting.selfRoom || {}, room);
|
|
124
|
+
|
|
125
|
+
await cleanRoom(room)
|
|
124
126
|
})
|
|
125
127
|
}
|
|
126
128
|
|
|
@@ -141,10 +143,14 @@ async function syncRooms() {
|
|
|
141
143
|
respBody = await reqNode(node, "get", "/");
|
|
142
144
|
} catch (e) {
|
|
143
145
|
debug(`sync ${node} failed:`, e);
|
|
144
|
-
|
|
146
|
+
if (!isDisableConnect(node, setting.defaultPort)) {
|
|
147
|
+
respBody = await reqNode(node, "get", "/", setting.defaultPort);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (respBody) {
|
|
151
|
+
syncRooms.push(JSON.parse(respBody))
|
|
152
|
+
debug(`sync ${node} success`);
|
|
145
153
|
}
|
|
146
|
-
syncRooms.push(JSON.parse(respBody))
|
|
147
|
-
debug(`sync ${node} success`);
|
|
148
154
|
} catch (e) {
|
|
149
155
|
debug(`sync ${node} failed:`, respBody, e);
|
|
150
156
|
}
|
|
@@ -203,9 +209,9 @@ async function _syncSetting(room) {
|
|
|
203
209
|
}
|
|
204
210
|
setting.rooms = [setting.selfRoom, ...rooms]
|
|
205
211
|
.map(room => {
|
|
206
|
-
room.active = Date.now() - (room.lastUpdateTime || 0) <=
|
|
212
|
+
room.active = Date.now() - (room.lastUpdateTime || 0) <= setting.roomHeartbeatExpire;
|
|
207
213
|
Object.entries(room.jsirs).forEach(([key, jsir]) =>
|
|
208
|
-
jsir.active = Date.now() - (jsir.lastUpdateTime || 0) <=
|
|
214
|
+
jsir.active = Date.now() - (jsir.lastUpdateTime || 0) <= setting.roomHeartbeatExpire)
|
|
209
215
|
return room;
|
|
210
216
|
});
|
|
211
217
|
|
|
@@ -238,19 +244,35 @@ async function initRoom() {
|
|
|
238
244
|
let roomUpdateTouchTime = false;
|
|
239
245
|
await fileJson(roomDataFile, async room => {
|
|
240
246
|
await initRoomJsir(room)
|
|
241
|
-
if (!vl(room.lastUpdateTime) || (Date.now() - room.lastUpdateTime) >
|
|
247
|
+
if (!vl(room.lastUpdateTime) || (Date.now() - room.lastUpdateTime) > setting.roomTimer) {
|
|
242
248
|
roomUpdateTouchTime = true;
|
|
243
249
|
}
|
|
244
250
|
})
|
|
245
251
|
|
|
246
252
|
if (roomUpdateTouchTime) {
|
|
247
|
-
await fileLock(
|
|
253
|
+
await fileLock(updateRoomInfoLockKey, updateRoomInfo, false)
|
|
248
254
|
fileLock(syncRoomsLockKey, syncRooms, false);
|
|
249
255
|
}
|
|
250
256
|
|
|
251
257
|
await syncSetting();
|
|
252
258
|
}
|
|
253
259
|
|
|
260
|
+
async function cleanRoom(room) {
|
|
261
|
+
// 清理进程
|
|
262
|
+
for (let pid of Object.keys(room.jsirs || {})) {
|
|
263
|
+
let jsir = room.jsirs[pid]
|
|
264
|
+
let active = Date.now() - (jsir.lastUpdateTime || 0) <= setting.roomHeartbeatExpire;
|
|
265
|
+
if (!active && isPidAlive(pid)) {
|
|
266
|
+
try {
|
|
267
|
+
debug(`kill inactive jsir ${pid}`)
|
|
268
|
+
process.kill(parseInt(pid), 'SIGKILL'); // 强制终止进程
|
|
269
|
+
} catch (e) {
|
|
270
|
+
debug(`clearRoom ${pid} failed`, e)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
254
276
|
/*
|
|
255
277
|
最小为0
|
|
256
278
|
100 为1 毫秒
|
|
@@ -270,6 +292,7 @@ async function initRoomJsir(room) {
|
|
|
270
292
|
Object.entries(room.jsirs || {}).filter(([pid]) => isPidAlive(pid))
|
|
271
293
|
);
|
|
272
294
|
|
|
295
|
+
// 构造services
|
|
273
296
|
let services = {}
|
|
274
297
|
for (let key of Object.keys(setting.routes)) {
|
|
275
298
|
let ss = key.split("/").map(trim).filter(i => i);
|
|
@@ -277,6 +300,7 @@ async function initRoomJsir(room) {
|
|
|
277
300
|
getOr(services, ss[1] + '/' + ss[2], []).push(ss[3])
|
|
278
301
|
}
|
|
279
302
|
}
|
|
303
|
+
// 初始化jsir
|
|
280
304
|
let lastJsir = room.jsirs[process.pid]
|
|
281
305
|
room.jsirs[process.pid] = {
|
|
282
306
|
pid: process.pid,
|
package/deps/setting.js
CHANGED
|
@@ -32,10 +32,12 @@ module.exports = {
|
|
|
32
32
|
serviceFns: {},
|
|
33
33
|
enterOutputs: null,
|
|
34
34
|
defaultPort: 52108,
|
|
35
|
-
reqNodeDefaultTimeout:
|
|
35
|
+
reqNodeDefaultTimeout: 33000,
|
|
36
36
|
reqNodeTimeouts: {
|
|
37
37
|
'/': 3000
|
|
38
38
|
},
|
|
39
39
|
roomTimer: 1000,
|
|
40
|
-
serverSignExpire: 6000
|
|
40
|
+
serverSignExpire: 6000,
|
|
41
|
+
roomHeartbeatExpire: 9000,
|
|
42
|
+
consoleMap: {}
|
|
41
43
|
}
|
package/deps/util.js
CHANGED
|
@@ -272,6 +272,9 @@ function createConsole(uniqueName) {
|
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
|
+
if (uniqueName) {
|
|
276
|
+
setting.consoleMap[uniqueName] = result;
|
|
277
|
+
}
|
|
275
278
|
return result;
|
|
276
279
|
}
|
|
277
280
|
|
|
@@ -1014,11 +1017,14 @@ function _getConfig(key, defaultVal, uniqueName) {
|
|
|
1014
1017
|
if (key === undefined) {
|
|
1015
1018
|
return config
|
|
1016
1019
|
}
|
|
1020
|
+
if (uniqueName && setting.consoleMap[uniqueName]) {
|
|
1021
|
+
setting.consoleMap[uniqueName].msg(`require ${uniqueName} config "${key}"`)
|
|
1022
|
+
}
|
|
1017
1023
|
let writeFlag = false
|
|
1018
1024
|
if (!(key in config)) {
|
|
1019
1025
|
writeFlag = true
|
|
1020
1026
|
config[key] = null
|
|
1021
|
-
console.warn(`require config "${key}"
|
|
1027
|
+
console.warn(`require ${uniqueName ? uniqueName:'global'} config "${key}"`)
|
|
1022
1028
|
}
|
|
1023
1029
|
if (writeFlag) {
|
|
1024
1030
|
fs.writeFileSync(configFile, JSON.stringify(config, null, 2))
|