From cd85908d3bb5af06477ead7168110f2306a42145 Mon Sep 17 00:00:00 2001 From: Dan Arad Date: Wed, 28 Oct 2020 09:26:27 +0200 Subject: [PATCH] 8428-stop-hosted-instance Signed-off-by: Dan Arad --- .../src/node/hosted-instance-manager.ts | 14 ++++++-------- .../src/hosted/node/hosted-plugin-process.ts | 2 +- .../src/hosted/node/hosted-plugin.ts | 18 ++++++++---------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/plugin-dev/src/node/hosted-instance-manager.ts b/packages/plugin-dev/src/node/hosted-instance-manager.ts index aaa7ccfaa4576..537548c617ff3 100644 --- a/packages/plugin-dev/src/node/hosted-instance-manager.ts +++ b/packages/plugin-dev/src/node/hosted-instance-manager.ts @@ -30,8 +30,7 @@ import { FileUri } from '@theia/core/lib/node/file-uri'; import { LogType } from '@theia/plugin-ext/lib/common/types'; import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin'; import { MetadataScanner } from '@theia/plugin-ext/lib/hosted/node/metadata-scanner'; - -const processTree = require('ps-tree'); +import { HostedPluginProcess } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin-process'; export const HostedInstanceManager = Symbol('HostedInstanceManager'); @@ -109,6 +108,9 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan @inject(MetadataScanner) protected readonly metadata: MetadataScanner; + @inject(HostedPluginProcess) + protected readonly hostedPluginProcess: HostedPluginProcess; + isRunning(): boolean { return this.isPluginRunning; } @@ -156,13 +158,9 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan terminate(): void { if (this.isPluginRunning) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - processTree(this.hostedInstanceProcess.pid, (err: Error, children: Array) => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const args = ['-SIGTERM', this.hostedInstanceProcess.pid.toString()].concat(children.map((p: any) => p.PID)); - cp.spawn('kill', args); - }); + this.hostedPluginProcess.killProcessTree(this.hostedInstanceProcess.pid); this.hostedPluginSupport.sendLog({ data: 'Hosted instance has been terminated', type: LogType.Info }); + this.isPluginRunning = false; } else { throw new Error('Hosted plugin instance is not running.'); } diff --git a/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts b/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts index 3540bbae71316..643b2bbda2e46 100644 --- a/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts +++ b/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts @@ -122,7 +122,7 @@ export class HostedPluginProcess implements ServerPluginRunner { this.killProcessTree(cp.pid); } - private killProcessTree(parentPid: number): void { + killProcessTree(parentPid: number): void { psTree(parentPid, (_, childProcesses) => { childProcesses.forEach(childProcess => this.killProcess(parseInt(childProcess.PID)) diff --git a/packages/plugin-ext/src/hosted/node/hosted-plugin.ts b/packages/plugin-ext/src/hosted/node/hosted-plugin.ts index 7b7c1101266ea..83fbfe0a138b1 100644 --- a/packages/plugin-ext/src/hosted/node/hosted-plugin.ts +++ b/packages/plugin-ext/src/hosted/node/hosted-plugin.ts @@ -29,6 +29,7 @@ export interface IPCConnectionOptions { @injectable() export class HostedPluginSupport { + private isPluginProcessRunning = false; private client: HostedPluginClient; @inject(ILogger) @@ -37,8 +38,6 @@ export class HostedPluginSupport { @inject(HostedPluginProcess) protected readonly hostedPluginProcess: HostedPluginProcess; - private isPluginProcessRunning = false; - /** * Optional runners to delegate some work */ @@ -87,11 +86,7 @@ export class HostedPluginSupport { } } - private terminatePluginServer(): void { - this.hostedPluginProcess.terminatePluginServer(); - } - - public runPluginServer(): void { + runPluginServer(): void { if (!this.isPluginProcessRunning) { this.hostedPluginProcess.runPluginServer(); this.isPluginProcessRunning = true; @@ -101,19 +96,22 @@ export class HostedPluginSupport { /** * Provides additional plugin ids. */ - public async getExtraDeployedPluginIds(): Promise { + async getExtraDeployedPluginIds(): Promise { return [].concat.apply([], await Promise.all(this.pluginRunners.map(runner => runner.getExtraDeployedPluginIds()))); } /** * Provides additional deployed plugins. */ - public async getExtraDeployedPlugins(): Promise { + async getExtraDeployedPlugins(): Promise { return [].concat.apply([], await Promise.all(this.pluginRunners.map(runner => runner.getExtraDeployedPlugins()))); } - public sendLog(logPart: LogPart): void { + sendLog(logPart: LogPart): void { this.client.log(logPart); } + private terminatePluginServer(): void { + this.hostedPluginProcess.terminatePluginServer(); + } }