Archived
1
0

Centralize fork logging

This commit is contained in:
Asher 2019-01-22 19:05:56 -06:00 committed by Kyle Carberry
parent 8a789ac957
commit 704a0defc9
No known key found for this signature in database
GPG Key ID: A0409BDB6B0B3EDB
3 changed files with 47 additions and 33 deletions

View File

@ -44,33 +44,13 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi
const server = new Server(connection, options ? { const server = new Server(connection, options ? {
...options, ...options,
forkProvider: (message: NewSessionMessage): ChildProcess => { forkProvider: (message: NewSessionMessage): ChildProcess => {
const command = message.getCommand();
const childLogger = logger.named(command.split("/").pop()!);
childLogger.debug("Forking...", field("module", command));
let proc: ChildProcess; let proc: ChildProcess;
if (message.getIsBootstrapFork()) { if (message.getIsBootstrapFork()) {
proc = forkModule(command); proc = forkModule(message.getCommand());
} else { } else {
throw new Error("No support for non bootstrap-forking yet"); throw new Error("No support for non bootstrap-forking yet");
} }
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("exit", (exitCode) => {
childLogger.debug(`Exited with ${exitCode}`);
});
return proc; return proc;
}, },
} : undefined); } : undefined);

View File

@ -1,6 +1,7 @@
import * as cp from "child_process"; import * as cp from "child_process";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { logger, field } from "@coder/logger/src";
declare var __non_webpack_require__: typeof require; declare var __non_webpack_require__: typeof require;
@ -14,16 +15,55 @@ 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"); * const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", true);
* 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 * @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): cp.ChildProcess => { 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));
childLogger.debug("Forking...", field("module", modulePath));
let proc: cp.ChildProcess | undefined;
const args = ["--bootstrap-fork", modulePath]; const args = ["--bootstrap-fork", modulePath];
if (process.env.CLI === "true") { if (process.env.CLI === "true") {
return cp.spawn(process.execPath, args); proc = stdio ? cp.spawn(process.execPath, args) : cp.fork(process.execPath, args);
} else if (stdio) {
proc = cp.spawn("npm", ["start", "--scripts-prepend-node-path", "--", ...args]);
} else { } else {
return cp.spawn("npm", ["start", "--scripts-prepend-node-path", "--", ...args]); // TODO: need to fork somehow so we get send/onMessage.
proc = cp.spawn("npm", ["start", "--scripts-prepend-node-path", "--", ...args]);
} }
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}`);
});
return proc;
}; };

View File

@ -4,7 +4,6 @@ import * as os from "os";
import * as path from "path"; import * as path from "path";
import { forkModule } from "./bootstrapFork"; import { forkModule } from "./bootstrapFork";
import { StdioIpcHandler } from "../ipc"; import { StdioIpcHandler } from "../ipc";
import { logger, field } from "@coder/logger/src";
import { ParsedArgs } from "vs/platform/environment/common/environment"; import { ParsedArgs } from "vs/platform/environment/common/environment";
import { LogLevel } from "vs/platform/log/common/log"; import { LogLevel } from "vs/platform/log/common/log";
import { Emitter, Event } from '@coder/events/src'; import { Emitter, Event } from '@coder/events/src';
@ -29,7 +28,6 @@ export class SharedProcess {
private activeProcess: ChildProcess | undefined; private activeProcess: ChildProcess | undefined;
private ipcHandler: StdioIpcHandler | undefined; private ipcHandler: StdioIpcHandler | undefined;
private readonly onStateEmitter: Emitter<SharedProcessEvent>; private readonly onStateEmitter: Emitter<SharedProcessEvent>;
private readonly logger = logger.named("SHRD PROC");
public constructor( public constructor(
private readonly userDataDir: string, private readonly userDataDir: string,
@ -68,7 +66,7 @@ export class SharedProcess {
state: SharedProcessState.Starting, state: SharedProcessState.Starting,
}); });
let resolved: boolean = false; let resolved: boolean = false;
this.activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain"); this.activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", true);
this.activeProcess.on("exit", (err) => { this.activeProcess.on("exit", (err) => {
if (this._state !== SharedProcessState.Stopped) { if (this._state !== SharedProcessState.Stopped) {
this.setState({ this.setState({
@ -101,11 +99,7 @@ export class SharedProcess {
state: SharedProcessState.Ready, state: SharedProcessState.Ready,
}); });
}); });
this.activeProcess.stdout.on("data", (data) => {
this.logger.debug("stdout", field("message", data.toString()));
});
this.activeProcess.stderr.on("data", (data) => { this.activeProcess.stderr.on("data", (data) => {
this.logger.debug("stderr", field("message", data.toString()));
if (!resolved) { if (!resolved) {
this.setState({ this.setState({
error: data.toString(), error: data.toString(),