2019-01-18 22:46:40 +01:00
|
|
|
import { ChildProcess } from "child_process";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as os from "os";
|
|
|
|
import * as path from "path";
|
|
|
|
import { forkModule } from "./bootstrapFork";
|
|
|
|
import { StdioIpcHandler } from "../ipc";
|
|
|
|
import { ParsedArgs } from "vs/platform/environment/common/environment";
|
2019-02-07 01:11:31 +01:00
|
|
|
import { Emitter } from "@coder/events/src";
|
|
|
|
import { retry } from "@coder/ide/src/retry";
|
2019-02-20 00:53:14 +01:00
|
|
|
import { logger, field, Level } from "@coder/logger";
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-01-19 00:08:44 +01:00
|
|
|
export enum SharedProcessState {
|
|
|
|
Stopped,
|
|
|
|
Starting,
|
|
|
|
Ready,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SharedProcessEvent = {
|
|
|
|
readonly state: SharedProcessState.Ready | SharedProcessState.Starting;
|
|
|
|
} | {
|
|
|
|
readonly state: SharedProcessState.Stopped;
|
|
|
|
readonly error: string;
|
2019-01-22 20:11:54 +01:00
|
|
|
};
|
2019-01-19 00:08:44 +01:00
|
|
|
|
2019-01-18 22:46:40 +01:00
|
|
|
export class SharedProcess {
|
2019-02-28 21:04:19 +01:00
|
|
|
public readonly socketPath: string = os.platform() === "win32" ? path.join("\\\\?\\pipe", os.tmpdir(), `.code-server${Math.random().toString()}`) : path.join(os.tmpdir(), `.code-server${Math.random().toString()}`);
|
2019-01-19 00:08:44 +01:00
|
|
|
private _state: SharedProcessState = SharedProcessState.Stopped;
|
2019-01-18 22:46:40 +01:00
|
|
|
private activeProcess: ChildProcess | undefined;
|
|
|
|
private ipcHandler: StdioIpcHandler | undefined;
|
2019-02-06 18:53:23 +01:00
|
|
|
private readonly onStateEmitter = new Emitter<SharedProcessEvent>();
|
|
|
|
public readonly onState = this.onStateEmitter.event;
|
2019-02-07 01:11:31 +01:00
|
|
|
private readonly retryName = "Shared process";
|
2019-02-20 00:53:14 +01:00
|
|
|
private readonly logger = logger.named("shared");
|
2019-01-18 22:46:40 +01:00
|
|
|
|
|
|
|
public constructor(
|
|
|
|
private readonly userDataDir: string,
|
2019-02-05 18:15:20 +01:00
|
|
|
private readonly builtInExtensionsDir: string,
|
2019-01-18 22:46:40 +01:00
|
|
|
) {
|
2019-02-07 01:11:31 +01:00
|
|
|
retry.register(this.retryName, () => this.restart());
|
|
|
|
retry.run(this.retryName);
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 00:08:44 +01:00
|
|
|
public get state(): SharedProcessState {
|
|
|
|
return this._state;
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public restart(): void {
|
|
|
|
if (this.activeProcess && !this.activeProcess.killed) {
|
|
|
|
this.activeProcess.kill();
|
|
|
|
}
|
|
|
|
|
|
|
|
const extensionsDir = path.join(this.userDataDir, "extensions");
|
|
|
|
const mkdir = (dir: string): void => {
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
} catch (ex) {
|
2019-02-27 01:23:33 +01:00
|
|
|
if (ex.code !== "EEXIST" && ex.code !== "EISDIR") {
|
2019-01-18 22:46:40 +01:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mkdir(this.userDataDir);
|
|
|
|
mkdir(extensionsDir);
|
2019-03-27 15:36:32 +01:00
|
|
|
const backupsDir = path.join(this.userDataDir, "Backups");
|
|
|
|
mkdir(backupsDir);
|
|
|
|
const workspacesFile = path.join(backupsDir, "workspaces.json");
|
|
|
|
if (!fs.existsSync(workspacesFile)) {
|
|
|
|
fs.closeSync(fs.openSync(workspacesFile, "w"));
|
|
|
|
}
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-01-19 00:08:44 +01:00
|
|
|
this.setState({
|
|
|
|
state: SharedProcessState.Starting,
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
|
|
|
let resolved: boolean = false;
|
2019-02-20 00:53:14 +01:00
|
|
|
const maybeStop = (error: string): void => {
|
|
|
|
if (resolved) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
error,
|
|
|
|
state: SharedProcessState.Stopped,
|
|
|
|
});
|
|
|
|
if (!this.activeProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.activeProcess.kill();
|
|
|
|
};
|
2019-02-19 17:17:03 +01:00
|
|
|
this.activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", [], {
|
|
|
|
env: {
|
|
|
|
VSCODE_ALLOW_IO: "true",
|
|
|
|
VSCODE_LOGS: process.env.VSCODE_LOGS,
|
|
|
|
},
|
2019-02-22 02:32:08 +01:00
|
|
|
}, this.userDataDir);
|
2019-02-20 00:53:14 +01:00
|
|
|
if (this.logger.level <= Level.Trace) {
|
|
|
|
this.activeProcess.stdout.on("data", (data) => {
|
|
|
|
this.logger.trace(() => ["stdout", field("data", data.toString())]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.activeProcess.on("error", (error) => {
|
|
|
|
this.logger.error("error", field("error", error));
|
|
|
|
maybeStop(error.message);
|
|
|
|
});
|
2019-01-19 00:08:44 +01:00
|
|
|
this.activeProcess.on("exit", (err) => {
|
|
|
|
if (this._state !== SharedProcessState.Stopped) {
|
|
|
|
this.setState({
|
|
|
|
error: `Exited with ${err}`,
|
|
|
|
state: SharedProcessState.Stopped,
|
|
|
|
});
|
|
|
|
}
|
2019-02-07 01:11:31 +01:00
|
|
|
retry.run(this.retryName, new Error(`Exited with ${err}`));
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
|
|
|
this.ipcHandler = new StdioIpcHandler(this.activeProcess);
|
|
|
|
this.ipcHandler.once("handshake:hello", () => {
|
|
|
|
const data: {
|
|
|
|
sharedIPCHandle: string;
|
2019-02-07 20:50:17 +01:00
|
|
|
args: Partial<ParsedArgs>;
|
2019-02-07 21:17:14 +01:00
|
|
|
logLevel: Level;
|
2019-01-18 22:46:40 +01:00
|
|
|
} = {
|
|
|
|
args: {
|
2019-02-05 18:15:20 +01:00
|
|
|
"builtin-extensions-dir": this.builtInExtensionsDir,
|
2019-01-18 22:46:40 +01:00
|
|
|
"user-data-dir": this.userDataDir,
|
|
|
|
"extensions-dir": extensionsDir,
|
2019-02-07 20:50:17 +01:00
|
|
|
},
|
2019-02-20 00:53:14 +01:00
|
|
|
logLevel: this.logger.level,
|
2019-01-18 22:46:40 +01:00
|
|
|
sharedIPCHandle: this.socketPath,
|
|
|
|
};
|
|
|
|
this.ipcHandler!.send("handshake:hey there", "", data);
|
|
|
|
});
|
|
|
|
this.ipcHandler.once("handshake:im ready", () => {
|
|
|
|
resolved = true;
|
2019-02-07 01:11:31 +01:00
|
|
|
retry.recover(this.retryName);
|
2019-01-19 00:08:44 +01:00
|
|
|
this.setState({
|
|
|
|
state: SharedProcessState.Ready,
|
|
|
|
});
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
|
|
|
this.activeProcess.stderr.on("data", (data) => {
|
2019-02-20 00:53:14 +01:00
|
|
|
this.logger.error("stderr", field("data", data.toString()));
|
|
|
|
maybeStop(data.toString());
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public dispose(): void {
|
|
|
|
if (this.ipcHandler) {
|
|
|
|
this.ipcHandler.send("handshake:goodbye");
|
|
|
|
}
|
|
|
|
this.ipcHandler = undefined;
|
|
|
|
}
|
2019-01-19 00:08:44 +01:00
|
|
|
|
|
|
|
private setState(event: SharedProcessEvent): void {
|
|
|
|
this._state = event.state;
|
|
|
|
this.onStateEmitter.emit(event);
|
|
|
|
}
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|