@scrypted/server 0.123.47 → 0.123.49

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 (35) hide show
  1. package/dist/cluster/cluster-setup.d.ts +2 -0
  2. package/dist/cluster/cluster-setup.js +11 -3
  3. package/dist/cluster/cluster-setup.js.map +1 -1
  4. package/dist/cluster/connect-rpc-object.d.ts +18 -0
  5. package/dist/cluster/cpu-timer.d.ts +6 -0
  6. package/dist/cluster/cpu-timer.js +44 -0
  7. package/dist/cluster/cpu-timer.js.map +1 -0
  8. package/dist/plugin/plugin-api.d.ts +1 -0
  9. package/dist/plugin/plugin-api.js.map +1 -1
  10. package/dist/plugin/plugin-host.js +2 -0
  11. package/dist/plugin/plugin-host.js.map +1 -1
  12. package/dist/plugin/plugin-remote-worker.js +2 -1
  13. package/dist/plugin/plugin-remote-worker.js.map +1 -1
  14. package/dist/runtime.d.ts +1 -0
  15. package/dist/runtime.js +1 -0
  16. package/dist/runtime.js.map +1 -1
  17. package/dist/scrypted-cluster-main.d.ts +5 -3
  18. package/dist/scrypted-cluster-main.js +33 -13
  19. package/dist/scrypted-cluster-main.js.map +1 -1
  20. package/dist/services/cluster-fork.d.ts +2 -1
  21. package/dist/services/cluster-fork.js +2 -0
  22. package/dist/services/cluster-fork.js.map +1 -1
  23. package/dist/tsconfig.tsbuildinfo +1 -1
  24. package/package.json +2 -2
  25. package/python/cluster_setup.py +2 -2
  26. package/python/plugin_remote.py +5 -5
  27. package/src/cluster/cluster-setup.ts +13 -5
  28. package/src/cluster/connect-rpc-object.ts +18 -0
  29. package/src/cluster/cpu-timer.ts +48 -0
  30. package/src/plugin/plugin-api.ts +1 -0
  31. package/src/plugin/plugin-host.ts +2 -0
  32. package/src/plugin/plugin-remote-worker.ts +2 -1
  33. package/src/runtime.ts +1 -0
  34. package/src/scrypted-cluster-main.ts +37 -16
  35. package/src/services/cluster-fork.ts +6 -3
@@ -1,4 +1,4 @@
1
- import type { ClusterManager, ClusterWorker, ForkOptions } from '@scrypted/types';
1
+ import type { ClusterManager, ForkOptions } from '@scrypted/types';
2
2
  import crypto from 'crypto';
3
3
  import { once } from 'events';
4
4
  import net from 'net';
@@ -11,6 +11,7 @@ import { computeClusterObjectHash } from './cluster/cluster-hash';
11
11
  import { getClusterLabels, getClusterWorkerWeight } from './cluster/cluster-labels';
12
12
  import { getScryptedClusterMode, InitializeCluster, setupCluster } from './cluster/cluster-setup';
13
13
  import type { ClusterObject } from './cluster/connect-rpc-object';
14
+ import { CpuTimer } from './cluster/cpu-timer';
14
15
  import type { PluginAPI } from './plugin/plugin-api';
15
16
  import { getPluginVolume, getScryptedVolume } from './plugin/plugin-volume';
16
17
  import { prepareZip } from './plugin/runtime/node-worker-common';
@@ -20,10 +21,10 @@ import { RpcPeer } from './rpc';
20
21
  import { createRpcDuplexSerializer } from './rpc-serializer';
21
22
  import type { ScryptedRuntime } from './runtime';
22
23
  import type { ClusterForkService } from './services/cluster-fork';
23
- import { sleep } from './sleep';
24
- import type { ServiceControl } from './services/service-control';
25
24
  import { EnvControl } from './services/env';
26
25
  import { Info } from './services/info';
26
+ import type { ServiceControl } from './services/service-control';
27
+ import { sleep } from './sleep';
27
28
 
28
29
  installSourceMapSupport({
29
30
  environment: 'node',
@@ -84,6 +85,7 @@ export interface RunningClusterWorker extends ClusterWorkerProperties {
84
85
  forks: Set<ClusterForkOptions>;
85
86
  address: string;
86
87
  weight: number;
88
+ cpuUsage: number;
87
89
  }
88
90
 
89
91
  export class PeerLiveness {
@@ -120,7 +122,7 @@ export interface ClusterForkResultInterface {
120
122
 
121
123
  export type ClusterForkParam = (runtime: string, options: RuntimeWorkerOptions, peerLiveness: PeerLiveness, getZip: () => Promise<Buffer>) => Promise<ClusterForkResultInterface>;
122
124
 
123
- function createClusterForkParam(mainFilename: string, clusterId: string, clusterSecret: string) {
125
+ function createClusterForkParam(mainFilename: string, clusterId: string, clusterSecret: string, clusterWorkerId: string) {
124
126
  const clusterForkParam: ClusterForkParam = async (runtime, runtimeWorkerOptions, peerLiveness, getZip) => {
125
127
  let runtimeWorker: RuntimeWorker;
126
128
 
@@ -166,7 +168,7 @@ function createClusterForkParam(mainFilename: string, clusterId: string, cluster
166
168
  let ping: any;
167
169
  try {
168
170
  const initializeCluster: InitializeCluster = await threadPeer.getParam('initializeCluster');
169
- await initializeCluster({ clusterId, clusterSecret });
171
+ await initializeCluster({ clusterId, clusterSecret, clusterWorkerId });
170
172
  getRemote = await threadPeer.getParam('getRemote');
171
173
  ping = await threadPeer.getParam('ping');
172
174
  }
@@ -206,6 +208,7 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
206
208
  console.log('Cluster client starting.');
207
209
 
208
210
  const envControl = new EnvControl();
211
+ const cpuTimer = new CpuTimer();
209
212
 
210
213
  const originalClusterAddress = process.env.SCRYPTED_CLUSTER_ADDRESS;
211
214
  const labels = getClusterLabels();
@@ -259,6 +262,7 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
259
262
  peer.params['service-control'] = serviceControl;
260
263
  peer.params['env-control'] = envControl;
261
264
  peer.params['info'] = new Info();
265
+ peer.params['cpu'] = async () => cpuTimer.sample();
262
266
 
263
267
  const { localAddress, localPort } = socket;
264
268
  console.log('Cluster server connected.', localAddress, localPort);
@@ -284,11 +288,10 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
284
288
  };
285
289
 
286
290
  const { clusterId, clusterWorkerId } = await connectForkWorker(auth, properties);
287
- process.env.SCRYPTED_CLUSTER_WORKER_ID = clusterWorkerId;
288
291
  const clusterPeerSetup = setupCluster(peer);
289
- await clusterPeerSetup.initializeCluster({ clusterId, clusterSecret });
292
+ await clusterPeerSetup.initializeCluster({ clusterId, clusterSecret, clusterWorkerId });
290
293
 
291
- peer.params['fork'] = createClusterForkParam(mainFilename, clusterId, clusterSecret);
294
+ peer.params['fork'] = createClusterForkParam(mainFilename, clusterId, clusterSecret, clusterWorkerId);
292
295
 
293
296
  await peer.killed;
294
297
  }
@@ -305,19 +308,26 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
305
308
  }
306
309
 
307
310
  export function createClusterServer(mainFilename: string, scryptedRuntime: ScryptedRuntime, certificate: ReturnType<typeof createSelfSignedCertificate>) {
308
- const serverClusterWorkerId = crypto.randomUUID();
309
- process.env.SCRYPTED_CLUSTER_WORKER_ID = serverClusterWorkerId;
311
+ scryptedRuntime.serverClusterWorkerId = crypto.randomUUID();
310
312
  const serverWorker: RunningClusterWorker = {
311
313
  labels: getClusterLabels(),
312
- id: serverClusterWorkerId,
314
+ id: scryptedRuntime.serverClusterWorkerId,
313
315
  peer: undefined,
314
- fork: Promise.resolve(createClusterForkParam(mainFilename, scryptedRuntime.clusterId, scryptedRuntime.clusterSecret)),
316
+ fork: Promise.resolve(createClusterForkParam(mainFilename, scryptedRuntime.clusterId, scryptedRuntime.clusterSecret, scryptedRuntime.serverClusterWorkerId)),
315
317
  name: process.env.SCRYPTED_CLUSTER_WORKER_NAME || os.hostname(),
316
318
  address: process.env.SCRYPTED_CLUSTER_ADDRESS,
317
319
  weight: getClusterWorkerWeight(),
318
320
  forks: new Set(),
321
+ cpuUsage: 0,
319
322
  };
320
- scryptedRuntime.clusterWorkers.set(serverClusterWorkerId, serverWorker);
323
+ scryptedRuntime.clusterWorkers.set(scryptedRuntime.serverClusterWorkerId, serverWorker);
324
+
325
+ {
326
+ const cpuTimer = new CpuTimer();
327
+ setInterval(() => {
328
+ serverWorker.cpuUsage = cpuTimer.sample();
329
+ }, 1000);
330
+ }
321
331
 
322
332
  const server = tls.createServer({
323
333
  key: certificate.serviceKey,
@@ -353,6 +363,7 @@ export function createClusterServer(mainFilename: string, scryptedRuntime: Scryp
353
363
  name: auth.id,
354
364
  address: socket.remoteAddress,
355
365
  forks: new Set(),
366
+ cpuUsage: 0,
356
367
  };
357
368
  scryptedRuntime.clusterWorkers.set(id, worker);
358
369
  peer.killedSafe.finally(() => {
@@ -362,6 +373,16 @@ export function createClusterServer(mainFilename: string, scryptedRuntime: Scryp
362
373
  scryptedRuntime.clusterWorkers.delete(id);
363
374
  });
364
375
  console.log('Cluster client authenticated.', socket.remoteAddress, socket.remotePort, properties);
376
+
377
+ let cpu: Promise<() => Promise<number>>;
378
+ const cpuTimer = setInterval(async () => {
379
+ cpu ||= peer.getParam('cpu');
380
+ const usage = await (await cpu)();
381
+ worker.cpuUsage = usage;
382
+ }, 1000);
383
+ peer.killedSafe.finally(() => {
384
+ clearInterval(cpuTimer);
385
+ });
365
386
  }
366
387
  catch (e) {
367
388
  peer.kill(e);
@@ -383,18 +404,18 @@ export class ClusterManagerImpl implements ClusterManager {
383
404
  private clusterServicePromise: Promise<ClusterForkService>;
384
405
  private clusterMode = getScryptedClusterMode()?.[0];
385
406
 
386
- constructor(private api: PluginAPI) {
407
+ constructor(private api: PluginAPI, private clusterWorkerId: string) {
387
408
  }
388
409
 
389
410
  getClusterWorkerId(): string {
390
- return process.env.SCRYPTED_CLUSTER_WORKER_ID;
411
+ return this.clusterWorkerId;
391
412
  }
392
413
 
393
414
  getClusterMode(): 'server' | 'client' | undefined {
394
415
  return this.clusterMode;
395
416
  }
396
417
 
397
- async getClusterWorkers(): Promise<Record<string, ClusterWorker>> {
418
+ async getClusterWorkers() {
398
419
  const clusterFork = await this.getClusterService();
399
420
  return clusterFork.getClusterWorkers();
400
421
  }
@@ -1,3 +1,4 @@
1
+ import { ClusterFork, ClusterWorker } from "@scrypted/types";
1
2
  import { matchesClusterLabels } from "../cluster/cluster-labels";
2
3
  import type { RuntimeWorkerOptions } from "../plugin/runtime/runtime-worker";
3
4
  import { RpcPeer } from "../rpc";
@@ -98,13 +99,15 @@ export class ClusterForkService {
98
99
  return ret;
99
100
  };
100
101
 
101
- async getClusterWorkers() {
102
- const ret: any = {};
102
+ async getClusterWorkers(): Promise<Record<string, ClusterWorker>> {
103
+ const ret: Record<string, ClusterWorker> = {};
103
104
  for (const worker of this.runtime.clusterWorkers.values()) {
104
105
  ret[worker.id] = {
106
+ id: worker.id,
105
107
  name: worker.name,
106
108
  labels: worker.labels,
107
- forks: [...worker.forks],
109
+ forks: [...worker.forks] as ClusterFork[],
110
+ cpuUsage: worker.cpuUsage,
108
111
  };
109
112
  }
110
113
  return ret;