Skip to content

Commit

Permalink
8428-stop-hosted-instance
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Arad <dan.arad@sap.com>
  • Loading branch information
danarad05 authored and paul-marechal committed Nov 15, 2020
1 parent 476c8df commit cd85908
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
14 changes: 6 additions & 8 deletions packages/plugin-dev/src/node/hosted-instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<any>) => {
// 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.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 8 additions & 10 deletions packages/plugin-ext/src/hosted/node/hosted-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IPCConnectionOptions {

@injectable()
export class HostedPluginSupport {
private isPluginProcessRunning = false;
private client: HostedPluginClient;

@inject(ILogger)
Expand All @@ -37,8 +38,6 @@ export class HostedPluginSupport {
@inject(HostedPluginProcess)
protected readonly hostedPluginProcess: HostedPluginProcess;

private isPluginProcessRunning = false;

/**
* Optional runners to delegate some work
*/
Expand Down Expand Up @@ -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;
Expand All @@ -101,19 +96,22 @@ export class HostedPluginSupport {
/**
* Provides additional plugin ids.
*/
public async getExtraDeployedPluginIds(): Promise<string[]> {
async getExtraDeployedPluginIds(): Promise<string[]> {
return [].concat.apply([], await Promise.all(this.pluginRunners.map(runner => runner.getExtraDeployedPluginIds())));
}

/**
* Provides additional deployed plugins.
*/
public async getExtraDeployedPlugins(): Promise<DeployedPlugin[]> {
async getExtraDeployedPlugins(): Promise<DeployedPlugin[]> {
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();
}
}

0 comments on commit cd85908

Please sign in to comment.