@scrypted/server 0.123.21 → 0.123.23

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.
@@ -12,7 +12,6 @@ import { PluginAPI, PluginAPIProxy, PluginRemote, PluginRemoteLoadZipOptions, Pl
12
12
  import { pipeWorkerConsole, prepareConsoles } from './plugin-console';
13
13
  import { getPluginNodePath, installOptionalDependencies } from './plugin-npm-dependencies';
14
14
  import { attachPluginRemote, DeviceManagerImpl, setupPluginRemote } from './plugin-remote';
15
- import { PluginStats, startStatsUpdater } from './plugin-remote-stats';
16
15
  import { createREPLServer } from './plugin-repl';
17
16
  import { getPluginVolume } from './plugin-volume';
18
17
  import { ChildProcessWorker } from './runtime/child-process-worker';
@@ -196,12 +195,6 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
196
195
 
197
196
  await installOptionalDependencies(getPluginConsole(), packageJson);
198
197
 
199
- // process.cpuUsage is for the entire process.
200
- // process.memoryUsage is per thread.
201
- const allMemoryStats = new Map<RuntimeWorker, NodeJS.MemoryUsage>();
202
- // start the stats updater/watchdog after installation has finished, as that may take some time.
203
- startStatsUpdater(allMemoryStats, zipAPI.updateStats);
204
-
205
198
  peer.params.ping = async (time: number, pong: (time: number) => Promise<void>) => {
206
199
  await pong(time);
207
200
  };
@@ -315,13 +308,11 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
315
308
  threadPeer.kill('worker exited');
316
309
  forkApi.removeListeners();
317
310
  forks.delete(remote);
318
- allMemoryStats.delete(runtimeWorker);
319
311
  });
320
312
  runtimeWorker.on('error', e => {
321
313
  threadPeer.kill('worker error ' + e);
322
314
  forkApi.removeListeners();
323
315
  forks.delete(remote);
324
- allMemoryStats.delete(runtimeWorker);
325
316
  });
326
317
 
327
318
  for (const [nativeId, dmd] of deviceManager.nativeIds.entries()) {
@@ -331,9 +322,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
331
322
  const forkOptions = Object.assign({}, zipOptions);
332
323
  forkOptions.fork = true;
333
324
  forkOptions.main = options?.filename;
334
- const forkZipAPI = new PluginZipAPI(() => zipAPI.getZip(), async (stats: PluginStats) => {
335
- allMemoryStats.set(runtimeWorker, stats.memoryUsage);
336
- });
325
+ const forkZipAPI = new PluginZipAPI(() => zipAPI.getZip());
337
326
  return remote.loadZip(packageJson, forkZipAPI, forkOptions)
338
327
  })();
339
328
 
@@ -1,5 +1,6 @@
1
1
  import type { ForkOptions } from '@scrypted/types';
2
2
  import { once } from 'events';
3
+ import net from 'net';
3
4
  import { install as installSourceMapSupport } from 'source-map-support';
4
5
  import type { Readable } from 'stream';
5
6
  import tls from 'tls';
@@ -67,6 +68,7 @@ export interface ClusterWorkerProperties {
67
68
 
68
69
  export interface ClusterWorker extends ClusterWorkerProperties {
69
70
  peer: RpcPeer;
71
+ forks: Set<ClusterForkOptions>;
70
72
  }
71
73
 
72
74
  export class PeerLiveness {
@@ -109,9 +111,22 @@ export function startClusterClient(mainFilename: string) {
109
111
  // to hang the app since no window is created yet.
110
112
  await sleep(1000);
111
113
 
112
- const socket = tls.connect({
114
+ const rawSocket = net.connect({
113
115
  host,
114
116
  port,
117
+ // require ipv4 to normalize cluster address.
118
+ family: 4,
119
+ });
120
+
121
+ try {
122
+ await once(rawSocket, 'connect');
123
+ }
124
+ catch( e) {
125
+ continue;
126
+ }
127
+
128
+ const socket = tls.connect({
129
+ socket: rawSocket,
115
130
  rejectUnauthorized: false,
116
131
  });
117
132
 
@@ -273,6 +288,7 @@ export function createClusterServer(runtime: ScryptedRuntime, certificate: Retur
273
288
  const worker: ClusterWorker = {
274
289
  ...properties,
275
290
  peer,
291
+ forks: new Set(),
276
292
  };
277
293
  runtime.clusterWorkers.add(worker);
278
294
  peer.killed.then(() => {
@@ -18,7 +18,12 @@ export class ClusterFork {
18
18
  throw new Error(`no worker found for cluster labels ${JSON.stringify(options.labels)}`);
19
19
 
20
20
  const fork: ClusterForkParam = await worker.peer.getParam('fork');
21
- return fork(peerLiveness, options.runtime, packageJson, zipHash, getZip);
21
+ const forkResult = await fork(peerLiveness, options.runtime, packageJson, zipHash, getZip);
22
+ worker.forks.add(options);
23
+ forkResult.waitKilled().catch(() => {}).finally(() => {
24
+ worker.forks.delete(options);
25
+ });
26
+ return forkResult;
22
27
  }
23
28
 
24
29
  async getClusterWorkers() {
@@ -26,6 +31,7 @@ export class ClusterFork {
26
31
  for (const worker of this.runtime.clusterWorkers) {
27
32
  ret[worker.peer.peerName] = {
28
33
  labels: worker.labels,
34
+ forks: [...worker.forks],
29
35
  };
30
36
  }
31
37
  return ret;
@@ -115,7 +115,6 @@ export class PluginComponent {
115
115
  return {
116
116
  pid: host?.worker?.pid,
117
117
  clientsCount: host?.io?.clientsCount,
118
- stats: host?.stats,
119
118
  rpcObjects,
120
119
  packageJson,
121
120
  pendingResults,
@@ -1,7 +0,0 @@
1
- import { RuntimeWorker } from "./runtime/runtime-worker";
2
- export interface PluginStats {
3
- type: 'stats';
4
- cpuUsage: NodeJS.CpuUsage;
5
- memoryUsage: NodeJS.MemoryUsage;
6
- }
7
- export declare function startStatsUpdater(allMemoryStats: Map<RuntimeWorker, NodeJS.MemoryUsage>, updateStats: (stats: PluginStats) => Promise<void>): void;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.startStatsUpdater = startStatsUpdater;
4
- function startStatsUpdater(allMemoryStats, updateStats) {
5
- if (!updateStats)
6
- console.warn('wtf');
7
- setInterval(() => {
8
- let cpuUsage;
9
- let memoryUsage;
10
- if (process.cpuUsage)
11
- cpuUsage = process.cpuUsage();
12
- allMemoryStats.set(undefined, process.memoryUsage());
13
- memoryUsage = {
14
- rss: 0,
15
- heapTotal: 0,
16
- heapUsed: 0,
17
- external: 0,
18
- arrayBuffers: 0,
19
- };
20
- for (const mu of allMemoryStats.values()) {
21
- if (!mu)
22
- continue;
23
- memoryUsage.rss += mu.rss;
24
- memoryUsage.heapTotal += mu.heapTotal;
25
- memoryUsage.heapUsed += mu.heapUsed;
26
- memoryUsage.external += mu.external;
27
- memoryUsage.arrayBuffers += mu.arrayBuffers;
28
- }
29
- updateStats({
30
- type: 'stats',
31
- cpuUsage,
32
- memoryUsage,
33
- }).catch(() => { });
34
- }, 10000);
35
- }
36
- //# sourceMappingURL=plugin-remote-stats.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin-remote-stats.js","sourceRoot":"","sources":["../../src/plugin/plugin-remote-stats.ts"],"names":[],"mappings":";;AAQA,8CAmCC;AAnCD,SAAgB,iBAAiB,CAAC,cAAsD,EAAE,WAAkD;IACxI,IAAI,CAAC,WAAW;QACZ,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACvB,WAAW,CAAC,GAAG,EAAE;QACb,IAAI,QAAyB,CAAC;QAC9B,IAAI,WAA+B,CAAC;QACpC,IAAI,OAAO,CAAC,QAAQ;YAChB,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;QAElC,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;QAErD,WAAW,GAAG;YACV,GAAG,EAAE,CAAC;YACN,SAAS,EAAE,CAAC;YACZ,QAAQ,EAAE,CAAC;YACX,QAAQ,EAAE,CAAC;YACX,YAAY,EAAE,CAAC;SAClB,CAAA;QAED,KAAK,MAAM,EAAE,IAAI,cAAc,CAAC,MAAM,EAAE,EAAE,CAAC;YACvC,IAAI,CAAC,EAAE;gBACH,SAAS;YACb,WAAW,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,CAAC;YAC1B,WAAW,CAAC,SAAS,IAAI,EAAE,CAAC,SAAS,CAAC;YACtC,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAC,QAAQ,CAAC;YACpC,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAC,QAAQ,CAAC;YACpC,WAAW,CAAC,YAAY,IAAI,EAAE,CAAC,YAAY,CAAC;QAChD,CAAC;QAED,WAAW,CAAC;YACR,IAAI,EAAE,OAAO;YACb,QAAQ;YACR,WAAW;SACd,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACvB,CAAC,EAAE,KAAK,CAAC,CAAC;AACd,CAAC"}
@@ -1,44 +0,0 @@
1
- import { RuntimeWorker } from "./runtime/runtime-worker";
2
-
3
- export interface PluginStats {
4
- type: 'stats',
5
- cpuUsage: NodeJS.CpuUsage;
6
- memoryUsage: NodeJS.MemoryUsage;
7
- }
8
-
9
- export function startStatsUpdater(allMemoryStats: Map<RuntimeWorker, NodeJS.MemoryUsage>, updateStats: (stats: PluginStats) => Promise<void>) {
10
- if (!updateStats)
11
- console.warn('wtf')
12
- setInterval(() => {
13
- let cpuUsage: NodeJS.CpuUsage;
14
- let memoryUsage: NodeJS.MemoryUsage;
15
- if (process.cpuUsage)
16
- cpuUsage = process.cpuUsage();
17
-
18
- allMemoryStats.set(undefined, process.memoryUsage());
19
-
20
- memoryUsage = {
21
- rss: 0,
22
- heapTotal: 0,
23
- heapUsed: 0,
24
- external: 0,
25
- arrayBuffers: 0,
26
- }
27
-
28
- for (const mu of allMemoryStats.values()) {
29
- if (!mu)
30
- continue;
31
- memoryUsage.rss += mu.rss;
32
- memoryUsage.heapTotal += mu.heapTotal;
33
- memoryUsage.heapUsed += mu.heapUsed;
34
- memoryUsage.external += mu.external;
35
- memoryUsage.arrayBuffers += mu.arrayBuffers;
36
- }
37
-
38
- updateStats({
39
- type: 'stats',
40
- cpuUsage,
41
- memoryUsage,
42
- }).catch(() => {});
43
- }, 10000);
44
- }