Get forked watcher & searcher working

This commit is contained in:
Asher 2019-01-23 13:43:20 -06:00 committed by Kyle Carberry
parent 4cd6bed8d2
commit 5cb657b415
No known key found for this signature in database
GPG Key ID: A0409BDB6B0B3EDB
6 changed files with 40 additions and 36 deletions

View File

@ -217,7 +217,7 @@ export class Client {
clientMsg.setNewSession(newSess); clientMsg.setNewSession(newSess);
this.connection.send(clientMsg.serializeBinary()); 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", () => { serverProc.stdin.on("close", () => {
const c = new CloseSessionInputMessage(); const c = new CloseSessionInputMessage();
c.setId(id); c.setId(id);

View File

@ -38,14 +38,16 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess {
public readonly stdin = new stream.Writable(); public readonly stdin = new stream.Writable();
public readonly stdout = new stream.Readable({ read: (): boolean => true }); public readonly stdout = new stream.Readable({ read: (): boolean => true });
public readonly stderr = 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 _killed: boolean = false;
private _connected: boolean = false;
public constructor( public constructor(
private readonly connection: ReadWriteConnection, private readonly connection: ReadWriteConnection,
private readonly id: number, private readonly id: number,
private readonly hasTty: boolean = false, private readonly hasTty: boolean = false,
private readonly ipc: boolean = false,
) { ) {
super(); super();
if (!this.hasTty) { 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 { public get killed(): boolean {
return this._killed; return this._killed;
} }
@ -68,9 +83,10 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess {
this.connection.send(client.serializeBinary()); this.connection.send(client.serializeBinary());
this._killed = true; 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(); const send = new WriteToSessionMessage();
send.setId(this.id); send.setId(this.id);
send.setSource(ipc ? WriteToSessionMessage.Source.IPC : WriteToSessionMessage.Source.STDIN); send.setSource(ipc ? WriteToSessionMessage.Source.IPC : WriteToSessionMessage.Source.STDIN);

View File

@ -46,7 +46,7 @@ export class CP {
public fork = (modulePath: string, args?: ReadonlyArray<string> | cp.ForkOptions, options?: cp.ForkOptions): cp.ChildProcess => { public fork = (modulePath: string, args?: ReadonlyArray<string> | cp.ForkOptions, options?: cp.ForkOptions): cp.ChildProcess => {
//@ts-ignore //@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<string> | cp.SpawnOptions, options?: cp.SpawnOptions): cp.ChildProcess => { public spawn = (command: string, args?: ReadonlyArray<string> | cp.SpawnOptions, options?: cp.SpawnOptions): cp.ChildProcess => {

View File

@ -63,6 +63,7 @@ export const handleNewSession = (connection: SendableConnection, newSession: New
stdin: proc.stdin, stdin: proc.stdin,
stderr: proc.stderr, stderr: proc.stderr,
stdout: proc.stdout, stdout: proc.stdout,
stdio: proc.stdio,
on: (...args: any[]) => (<any>proc.on)(...args), on: (...args: any[]) => (<any>proc.on)(...args),
write: (d) => proc.stdin.write(d), write: (d) => proc.stdin.write(d),
kill: (s) => proc.kill(s || "SIGTERM"), kill: (s) => proc.kill(s || "SIGTERM"),

View File

@ -4,7 +4,7 @@
"bin": "./out/cli.js", "bin": "./out/cli.js",
"files": [], "files": [],
"scripts": { "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:webpack": "rm -rf ./out && export CLI=true && ../../node_modules/.bin/webpack --config ./webpack.config.js",
"build:nexe": "node scripts/nexe.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", "build:bootstrap-fork": "cd ../vscode && npm run build:bootstrap-fork; cp ./bin/bootstrap-fork.js ../server/build/bootstrap-fork.js",

View File

@ -2,9 +2,20 @@ import * as cp from "child_process";
import * as fs from "fs"; import * as fs from "fs";
import * as net from "net"; import * as net from "net";
import * as path from "path"; 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 => { export const requireModule = (modulePath: string): void => {
process.env.AMD_ENTRYPOINT = modulePath; process.env.AMD_ENTRYPOINT = modulePath;
@ -16,6 +27,7 @@ export const requireModule = (modulePath: string): void => {
process.emit("message", JSON.parse(data.toString()), undefined); process.emit("message", JSON.parse(data.toString()), undefined);
}); });
// tslint:disable-next-line no-any
process.send = (message: any): void => { process.send = (message: any): void => {
socket.write(JSON.stringify(message)); 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 * Uses the internal bootstrap-fork.js to load a module
* @example * @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.stdout.on("data", (data) => console.log(data.toString("utf8")));
* cp.stderr.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 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 => { export const forkModule = (modulePath: string): cp.ChildProcess => {
const basename = modulePath.split("/").pop()!; const childLogger = getChildLogger(modulePath);
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));
childLogger.debug("Forking...", field("module", modulePath)); childLogger.debug("Forking...", field("module", modulePath));
let proc: cp.ChildProcess | undefined; let proc: cp.ChildProcess | undefined;
@ -53,27 +56,11 @@ export const forkModule = (modulePath: string, stdio?: boolean): cp.ChildProcess
stdio: [null, null, null, "pipe"], stdio: [null, null, null, "pipe"],
}; };
if (process.env.CLI === "true") { 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 { } else {
proc = cp.spawn(process.execArgv[0], ["-r", "tsconfig-paths/register", process.argv[1], ...args], options); 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) => { proc.on("exit", (exitCode) => {
childLogger.debug(`Exited with ${exitCode}`); childLogger.debug(`Exited with ${exitCode}`);
}); });