mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-03 23:06:49 +01:00
shared process init data
This commit is contained in:
69
src/vs/code/node/sharedProcess.ts
Normal file
69
src/vs/code/node/sharedProcess.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as cp from 'child_process';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
import { ParsedArgs } from 'vs/platform/environment/node/argv';
|
||||
|
||||
export interface ISharedProcessInitData {
|
||||
args: ParsedArgs;
|
||||
}
|
||||
|
||||
export interface ISharedProcessOptions {
|
||||
allowOutput?: boolean;
|
||||
debugPort?: number;
|
||||
}
|
||||
|
||||
const boostrapPath = URI.parse(require.toUrl('bootstrap')).fsPath;
|
||||
|
||||
function _spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions): cp.ChildProcess {
|
||||
const execArgv = [];
|
||||
const env = assign({}, process.env, {
|
||||
AMD_ENTRYPOINT: 'vs/code/node/sharedProcessMain'
|
||||
});
|
||||
|
||||
if (options.allowOutput) {
|
||||
env['VSCODE_ALLOW_IO'] = 'true';
|
||||
}
|
||||
|
||||
if (options.debugPort) {
|
||||
execArgv.push(`--debug=${options.debugPort}`);
|
||||
}
|
||||
|
||||
const result = cp.fork(boostrapPath, ['--type=SharedProcess'], { env, execArgv });
|
||||
|
||||
// handshake
|
||||
result.once('message', () => result.send(initData));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions = {}): IDisposable {
|
||||
let spawnCount = 0;
|
||||
let child: cp.ChildProcess;
|
||||
|
||||
const spawn = () => {
|
||||
if (++spawnCount > 10) {
|
||||
return;
|
||||
}
|
||||
|
||||
child = _spawnSharedProcess(initData, options);
|
||||
child.on('exit', spawn);
|
||||
};
|
||||
|
||||
spawn();
|
||||
|
||||
return {
|
||||
dispose: () => {
|
||||
if (child) {
|
||||
child.removeListener('exit', spawn);
|
||||
child.kill();
|
||||
child = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
|
||||
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
|
||||
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
|
||||
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
|
||||
import { parseArgs } from 'vs/platform/environment/node/argv';
|
||||
import { IEventService } from 'vs/platform/event/common/event';
|
||||
import { EventService } from 'vs/platform/event/common/eventService';
|
||||
import { ExtensionManagementChannel } from 'vs/platform/extensionManagement/common/extensionManagementIpc';
|
||||
@@ -30,6 +29,7 @@ import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProper
|
||||
import { TelemetryAppenderChannel } from 'vs/platform/telemetry/common/telemetryIpc';
|
||||
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
|
||||
import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender';
|
||||
import { ISharedProcessInitData } from './sharedProcess';
|
||||
|
||||
function quit(err?: Error) {
|
||||
if (err) {
|
||||
@@ -54,11 +54,11 @@ function setupPlanB(parentPid: number): void {
|
||||
|
||||
const eventPrefix = 'monacoworkbench';
|
||||
|
||||
function main(server: Server): void {
|
||||
function main(server: Server, initData: ISharedProcessInitData): void {
|
||||
const services = new ServiceCollection();
|
||||
|
||||
services.set(IEventService, new SyncDescriptor(EventService));
|
||||
services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, parseArgs(process.argv), process.execPath));
|
||||
services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, initData.args, process.execPath));
|
||||
services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
|
||||
services.set(IRequestService, new SyncDescriptor(RequestService));
|
||||
|
||||
@@ -147,8 +147,8 @@ function setupIPC(hook: string): TPromise<Server> {
|
||||
return setup(true);
|
||||
}
|
||||
|
||||
function handshake(): TPromise<void> {
|
||||
return new TPromise<void>((c, e) => {
|
||||
function handshake(): TPromise<ISharedProcessInitData> {
|
||||
return new TPromise<ISharedProcessInitData>((c, e) => {
|
||||
process.once('message', c);
|
||||
process.once('error', e);
|
||||
process.send('hello');
|
||||
@@ -156,6 +156,6 @@ function handshake(): TPromise<void> {
|
||||
}
|
||||
|
||||
TPromise.join<any>([setupIPC(process.env['VSCODE_SHARED_IPC_HOOK']), handshake()])
|
||||
.then(r => main(r[0]))
|
||||
.then(r => main(r[0], r[1]))
|
||||
.then(() => setupPlanB(process.env['VSCODE_PID']))
|
||||
.done(null, quit);
|
||||
Reference in New Issue
Block a user