From 5cb657b4150a1785931f99bfaf425399db6181de Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 23 Jan 2019 13:43:20 -0600 Subject: [PATCH] Get forked watcher & searcher working --- packages/protocol/src/browser/client.ts | 2 +- packages/protocol/src/browser/command.ts | 20 +++++++- .../src/browser/modules/child_process.ts | 2 +- packages/protocol/src/node/command.ts | 1 + packages/server/package.json | 2 +- packages/server/src/vscode/bootstrapFork.ts | 49 +++++++------------ 6 files changed, 40 insertions(+), 36 deletions(-) diff --git a/packages/protocol/src/browser/client.ts b/packages/protocol/src/browser/client.ts index d4e58ad92..0b430904d 100644 --- a/packages/protocol/src/browser/client.ts +++ b/packages/protocol/src/browser/client.ts @@ -217,7 +217,7 @@ export class Client { clientMsg.setNewSession(newSess); this.connection.send(clientMsg.serializeBinary()); - const serverProc = new ServerProcess(this.connection, id, options ? options.tty !== undefined : false); + const serverProc = new ServerProcess(this.connection, id, options ? options.tty !== undefined : false, isBootstrapFork); serverProc.stdin.on("close", () => { const c = new CloseSessionInputMessage(); c.setId(id); diff --git a/packages/protocol/src/browser/command.ts b/packages/protocol/src/browser/command.ts index ba094d9fa..7f68e622f 100644 --- a/packages/protocol/src/browser/command.ts +++ b/packages/protocol/src/browser/command.ts @@ -38,14 +38,16 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess { public readonly stdin = new stream.Writable(); public readonly stdout = new stream.Readable({ read: (): boolean => true }); public readonly stderr = new stream.Readable({ read: (): boolean => true }); - public pid: number | undefined; + private _pid: number | undefined; private _killed: boolean = false; + private _connected: boolean = false; public constructor( private readonly connection: ReadWriteConnection, private readonly id: number, private readonly hasTty: boolean = false, + private readonly ipc: boolean = false, ) { super(); if (!this.hasTty) { @@ -53,6 +55,19 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess { } } + public get pid(): number | undefined { + return this._pid; + } + + public set pid(pid: number | undefined) { + this._pid = pid; + this._connected = true; + } + + public get connected(): boolean { + return this._connected; + } + public get killed(): boolean { return this._killed; } @@ -68,9 +83,10 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess { this.connection.send(client.serializeBinary()); this._killed = true; + this._connected = false; } - public send(message: string | Uint8Array | any, ipc: boolean = false): void { + public send(message: string | Uint8Array | any, ipc: boolean = this.ipc): void { const send = new WriteToSessionMessage(); send.setId(this.id); send.setSource(ipc ? WriteToSessionMessage.Source.IPC : WriteToSessionMessage.Source.STDIN); diff --git a/packages/protocol/src/browser/modules/child_process.ts b/packages/protocol/src/browser/modules/child_process.ts index 8a4d42eac..1bfdb98f5 100644 --- a/packages/protocol/src/browser/modules/child_process.ts +++ b/packages/protocol/src/browser/modules/child_process.ts @@ -46,7 +46,7 @@ export class CP { public fork = (modulePath: string, args?: ReadonlyArray | cp.ForkOptions, options?: cp.ForkOptions): cp.ChildProcess => { //@ts-ignore - return this.client.fork(options && options.env && options.env.AMD_ENTRYPOINT || modulePath, args, options); + return this.client.bootstrapFork(options && options.env && options.env.AMD_ENTRYPOINT || modulePath); } public spawn = (command: string, args?: ReadonlyArray | cp.SpawnOptions, options?: cp.SpawnOptions): cp.ChildProcess => { diff --git a/packages/protocol/src/node/command.ts b/packages/protocol/src/node/command.ts index 9f9cd195a..eb9f3481c 100644 --- a/packages/protocol/src/node/command.ts +++ b/packages/protocol/src/node/command.ts @@ -63,6 +63,7 @@ export const handleNewSession = (connection: SendableConnection, newSession: New stdin: proc.stdin, stderr: proc.stderr, stdout: proc.stdout, + stdio: proc.stdio, on: (...args: any[]) => (proc.on)(...args), write: (d) => proc.stdin.write(d), kill: (s) => proc.kill(s || "SIGTERM"), diff --git a/packages/server/package.json b/packages/server/package.json index 5f3779aef..13364aacb 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -4,7 +4,7 @@ "bin": "./out/cli.js", "files": [], "scripts": { - "start": "NODE_ENV=development ts-node -r tsconfig-paths/register src/cli.ts", + "start": "ts-node -r tsconfig-paths/register src/cli.ts", "build:webpack": "rm -rf ./out && export CLI=true && ../../node_modules/.bin/webpack --config ./webpack.config.js", "build:nexe": "node scripts/nexe.js", "build:bootstrap-fork": "cd ../vscode && npm run build:bootstrap-fork; cp ./bin/bootstrap-fork.js ../server/build/bootstrap-fork.js", diff --git a/packages/server/src/vscode/bootstrapFork.ts b/packages/server/src/vscode/bootstrapFork.ts index 03361fe3f..dd7dfdbc0 100644 --- a/packages/server/src/vscode/bootstrapFork.ts +++ b/packages/server/src/vscode/bootstrapFork.ts @@ -2,9 +2,20 @@ import * as cp from "child_process"; import * as fs from "fs"; import * as net from "net"; import * as path from "path"; -import { logger, field } from "@coder/logger/src"; +import { Logger, logger, field } from "@coder/logger/src"; -declare var __non_webpack_require__: typeof require; +const getChildLogger = (modulePath: string): Logger => { + const basename = modulePath.split("/").pop()!; + let i = 0; + for (; i < basename.length; i++) { + const character = basename.charAt(i); + if (character === character.toUpperCase()) { + break; + } + } + + return logger.named(basename.substring(0, i)); +}; export const requireModule = (modulePath: string): void => { process.env.AMD_ENTRYPOINT = modulePath; @@ -16,6 +27,7 @@ export const requireModule = (modulePath: string): void => { process.emit("message", JSON.parse(data.toString()), undefined); }); + // tslint:disable-next-line no-any process.send = (message: any): void => { socket.write(JSON.stringify(message)); }; @@ -28,22 +40,13 @@ export const requireModule = (modulePath: string): void => { /** * Uses the internal bootstrap-fork.js to load a module * @example - * const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", true); + * const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain"); * cp.stdout.on("data", (data) => console.log(data.toString("utf8"))); * cp.stderr.on("data", (data) => console.log(data.toString("utf8"))); * @param modulePath Path of the VS Code module to load. - * @param stdio Whether to use stdio (spawn) or send/onMessage (fork). */ -export const forkModule = (modulePath: string, stdio?: boolean): cp.ChildProcess => { - const basename = modulePath.split("/").pop()!; - let i = 0; - for (; i < basename.length; i++) { - const character = basename.charAt(i); - if (character === character.toUpperCase()) { - break; - } - } - const childLogger = logger.named(basename.substring(0, i)); +export const forkModule = (modulePath: string): cp.ChildProcess => { + const childLogger = getChildLogger(modulePath); childLogger.debug("Forking...", field("module", modulePath)); let proc: cp.ChildProcess | undefined; @@ -53,27 +56,11 @@ export const forkModule = (modulePath: string, stdio?: boolean): cp.ChildProcess stdio: [null, null, null, "pipe"], }; if (process.env.CLI === "true") { - proc = stdio ? cp.spawn(process.execPath, args, options) : cp.fork(process.execPath, args, options); + proc = cp.spawn(process.execPath, args, options); } else { proc = cp.spawn(process.execArgv[0], ["-r", "tsconfig-paths/register", process.argv[1], ...args], options); } - proc.stdout.on("data", (message) => { - childLogger.debug("stdout", field("message", message.toString().trim())); - }); - - proc.stderr.on("data", (message) => { - childLogger.debug("stderr", field("message", message.toString().trim())); - }); - - proc.stdin.on("data", (message) => { - childLogger.debug("stdin", field("message", message.toString().trim())); - }); - - proc.on("message", (message) => { - childLogger.debug("message", field("message", message.toString().trim())); - }); - proc.on("exit", (exitCode) => { childLogger.debug(`Exited with ${exitCode}`); });