2019-01-18 22:46:40 +01:00
|
|
|
import { ChildProcess } from "child_process";
|
|
|
|
import * as fs from "fs";
|
2019-04-01 20:31:34 +02:00
|
|
|
import * as fse from "fs-extra";
|
2019-01-18 22:46:40 +01:00
|
|
|
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-04-01 20:31:34 +02:00
|
|
|
import { logger, 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-04-01 20:31:34 +02: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-20 00:53:14 +01:00
|
|
|
private readonly logger = logger.named("shared");
|
2019-04-01 20:31:34 +02:00
|
|
|
private readonly retry = retry.register("Shared process", () => this.connect());
|
|
|
|
private disposed: boolean = false;
|
2019-01-18 22:46:40 +01:00
|
|
|
|
|
|
|
public constructor(
|
|
|
|
private readonly userDataDir: string,
|
2019-04-04 00:07:47 +02:00
|
|
|
private readonly extensionsDir: string,
|
2019-02-05 18:15:20 +01:00
|
|
|
private readonly builtInExtensionsDir: string,
|
2019-01-18 22:46:40 +01:00
|
|
|
) {
|
2019-04-01 20:31:34 +02:00
|
|
|
this.retry.run();
|
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
|
|
|
}
|
|
|
|
|
2019-04-01 20:31:34 +02:00
|
|
|
/**
|
|
|
|
* Signal the shared process to terminate.
|
|
|
|
*/
|
|
|
|
public dispose(): void {
|
|
|
|
this.disposed = true;
|
|
|
|
if (this.ipcHandler) {
|
|
|
|
this.ipcHandler.send("handshake:goodbye");
|
|
|
|
}
|
|
|
|
this.ipcHandler = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start and connect to the shared process.
|
|
|
|
*/
|
|
|
|
private async connect(): Promise<void> {
|
|
|
|
this.setState({ state: SharedProcessState.Starting });
|
|
|
|
const activeProcess = await this.restart();
|
|
|
|
|
|
|
|
activeProcess.stderr.on("data", (data) => {
|
|
|
|
// Warn instead of error to prevent panic. It's unlikely stderr here is
|
|
|
|
// about anything critical to the functioning of the editor.
|
|
|
|
logger.warn(data.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
activeProcess.on("exit", (exitCode) => {
|
|
|
|
const error = new Error(`Exited with ${exitCode}`);
|
|
|
|
this.setState({
|
|
|
|
error: error.message,
|
|
|
|
state: SharedProcessState.Stopped,
|
|
|
|
});
|
|
|
|
if (!this.disposed) {
|
|
|
|
this.retry.run(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({ state: SharedProcessState.Ready });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restart the shared process. Kill existing process if running. Resolve when
|
|
|
|
* the shared process is ready and reject when it errors or dies before being
|
|
|
|
* ready.
|
|
|
|
*/
|
|
|
|
private async restart(): Promise<ChildProcess> {
|
2019-01-18 22:46:40 +01:00
|
|
|
if (this.activeProcess && !this.activeProcess.killed) {
|
|
|
|
this.activeProcess.kill();
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:36:32 +01:00
|
|
|
const backupsDir = path.join(this.userDataDir, "Backups");
|
2019-04-01 20:31:34 +02:00
|
|
|
await Promise.all([
|
|
|
|
fse.mkdirp(backupsDir),
|
|
|
|
]);
|
|
|
|
|
2019-03-27 15:36:32 +01:00
|
|
|
const workspacesFile = path.join(backupsDir, "workspaces.json");
|
|
|
|
if (!fs.existsSync(workspacesFile)) {
|
2019-04-01 20:31:34 +02:00
|
|
|
fs.appendFileSync(workspacesFile, "");
|
2019-03-27 15:36:32 +01:00
|
|
|
}
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-04-01 20:31:34 +02:00
|
|
|
const activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", [], {
|
2019-02-19 17:17:03 +01:00
|
|
|
env: {
|
|
|
|
VSCODE_ALLOW_IO: "true",
|
|
|
|
VSCODE_LOGS: process.env.VSCODE_LOGS,
|
|
|
|
},
|
2019-02-22 02:32:08 +01:00
|
|
|
}, this.userDataDir);
|
2019-04-01 20:31:34 +02:00
|
|
|
this.activeProcess = activeProcess;
|
|
|
|
|
|
|
|
await new Promise((resolve, reject): void => {
|
2019-04-04 00:32:20 +02:00
|
|
|
const doReject = (error: Error | number | null): void => {
|
|
|
|
if (error === null) {
|
|
|
|
error = new Error("Exited unexpectedly");
|
|
|
|
} else if (typeof error === "number") {
|
2019-04-01 20:31:34 +02:00
|
|
|
error = new Error(`Exited with ${error}`);
|
|
|
|
}
|
|
|
|
activeProcess.removeAllListeners();
|
2019-01-19 00:08:44 +01:00
|
|
|
this.setState({
|
2019-04-01 20:31:34 +02:00
|
|
|
error: error.message,
|
2019-01-19 00:08:44 +01:00
|
|
|
state: SharedProcessState.Stopped,
|
|
|
|
});
|
2019-04-01 20:31:34 +02:00
|
|
|
reject(error);
|
2019-01-18 22:46:40 +01:00
|
|
|
};
|
2019-04-01 20:31:34 +02:00
|
|
|
|
|
|
|
activeProcess.on("error", doReject);
|
|
|
|
activeProcess.on("exit", doReject);
|
|
|
|
|
|
|
|
this.ipcHandler = new StdioIpcHandler(activeProcess);
|
|
|
|
this.ipcHandler.once("handshake:hello", () => {
|
|
|
|
const data: {
|
|
|
|
sharedIPCHandle: string;
|
|
|
|
args: Partial<ParsedArgs>;
|
|
|
|
logLevel: Level;
|
|
|
|
} = {
|
|
|
|
args: {
|
|
|
|
"builtin-extensions-dir": this.builtInExtensionsDir,
|
|
|
|
"user-data-dir": this.userDataDir,
|
2019-04-04 00:07:47 +02:00
|
|
|
"extensions-dir": this.extensionsDir,
|
2019-04-01 20:31:34 +02:00
|
|
|
},
|
|
|
|
logLevel: this.logger.level,
|
|
|
|
sharedIPCHandle: this.socketPath,
|
|
|
|
};
|
|
|
|
this.ipcHandler!.send("handshake:hey there", "", data);
|
|
|
|
});
|
|
|
|
this.ipcHandler.once("handshake:im ready", () => {
|
|
|
|
activeProcess.removeListener("error", doReject);
|
|
|
|
activeProcess.removeListener("exit", doReject);
|
|
|
|
resolve();
|
2019-01-19 00:08:44 +01:00
|
|
|
});
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
|
|
|
|
2019-04-01 20:31:34 +02:00
|
|
|
return activeProcess;
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|
2019-01-19 00:08:44 +01:00
|
|
|
|
2019-04-01 20:31:34 +02:00
|
|
|
/**
|
|
|
|
* Set the internal shared process state and emit the state event.
|
|
|
|
*/
|
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
|
|
|
}
|