@scrypted/server 0.138.4 → 0.138.6

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/src/runtime.ts CHANGED
@@ -16,6 +16,7 @@ import { Parser as TarParser } from 'tar';
16
16
  import { URL } from "url";
17
17
  import WebSocket, { Server as WebSocketServer } from "ws";
18
18
  import { computeClusterObjectHash } from './cluster/cluster-hash';
19
+ import { isClusterAddress } from './cluster/cluster-setup';
19
20
  import { ClusterObject } from './cluster/connect-rpc-object';
20
21
  import { Plugin, PluginDevice, ScryptedAlert, ScryptedUser } from './db-types';
21
22
  import { httpFetch } from './fetch/http-fetch';
@@ -33,20 +34,20 @@ import { isConnectionUpgrade, PluginHttp } from './plugin/plugin-http';
33
34
  import { WebSocketConnection } from './plugin/plugin-remote-websocket';
34
35
  import { getPluginVolume } from './plugin/plugin-volume';
35
36
  import { getBuiltinRuntimeHosts } from './plugin/runtime/runtime-host';
37
+ import { timeoutPromise } from './promise-utils';
38
+ import { RunningClusterWorker } from './scrypted-cluster-main';
36
39
  import { getIpAddress, SCRYPTED_INSECURE_PORT, SCRYPTED_SECURE_PORT } from './server-settings';
37
40
  import { AddressSettings } from './services/addresses';
38
41
  import { Alerts } from './services/alerts';
39
42
  import { Backup } from './services/backup';
40
43
  import { ClusterForkService } from './services/cluster-fork';
41
44
  import { CORSControl } from './services/cors';
45
+ import { EnvControl } from './services/env';
42
46
  import { Info } from './services/info';
43
47
  import { getNpmPackageInfo, PluginComponent } from './services/plugin';
44
48
  import { ServiceControl } from './services/service-control';
45
49
  import { UsersService } from './services/users';
46
50
  import { getState, ScryptedStateManager, setState } from './state';
47
- import { isClusterAddress } from './cluster/cluster-setup';
48
- import { RunningClusterWorker } from './scrypted-cluster-main';
49
- import { EnvControl } from './services/env';
50
51
 
51
52
  interface DeviceProxyPair {
52
53
  handler: PluginDeviceProxyHandler;
@@ -678,7 +679,7 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
678
679
  }
679
680
 
680
681
  try {
681
- this.runPlugin(plugin);
682
+ await this.runPlugin(plugin);
682
683
  }
683
684
  catch (e) {
684
685
  logger.log('e', `error restarting plugin ${pluginId}`);
@@ -691,7 +692,6 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
691
692
  if (pluginHost) {
692
693
  pluginHost.worker.once('error', restart);
693
694
  pluginHost.worker.once('exit', restart);
694
- pluginHost.worker.once('close', restart);
695
695
  }
696
696
  else {
697
697
  restart();
@@ -754,7 +754,16 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
754
754
 
755
755
  }
756
756
 
757
- runPlugin(plugin: Plugin, pluginDebug?: PluginDebug) {
757
+ async runPlugin(plugin: Plugin, pluginDebug?: PluginDebug) {
758
+ const existingPluginHost = this.plugins[plugin._id];
759
+ const killPromise = existingPluginHost?.worker?.killPromise;
760
+ if (killPromise) {
761
+ existingPluginHost?.kill();
762
+ await timeoutPromise(5000, killPromise).catch(() => {
763
+ console.warn('plugin worker did not exit in 5 seconds');
764
+ });
765
+ }
766
+
758
767
  const pluginHost = this.loadPlugin(plugin, pluginDebug);
759
768
  this.probePluginDevices(plugin);
760
769
  return pluginHost;
@@ -75,7 +75,7 @@ export class PluginComponent {
75
75
  }
76
76
  async reload(pluginId: string) {
77
77
  const plugin = await this.scrypted.datastore.tryGet(Plugin, pluginId);
78
- this.scrypted.runPlugin(plugin);
78
+ await this.scrypted.runPlugin(plugin);
79
79
  }
80
80
  async kill(pluginId: string) {
81
81
  return this.scrypted.plugins[pluginId]?.kill();