Skip to content

feat(compass-shell): Integrate worker thread runtime in compass shell COMPASS-4556 #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/compass-shell/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ if ( process.defaultApp || /[\\/]electron-prebuilt[\\/]/.test(process.execPath)
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1024, height: 768, show: false
width: 1024,
height: 768,
show: false,
webPreferences: {
nodeIntegration: true,
},
});

// and load the index.html of the app.
Expand Down
49 changes: 34 additions & 15 deletions packages/compass-shell/electron/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,41 @@ render(CompassShellPlugin);
// mongoclient is. For that reason we can use a mocked one, this avoid
// the dependency to keytar:
//
const localUri = 'mongodb://localhost:27017/test';
MongoClient.connect(localUri, { useNewUrlParser: true, useUnifiedTopology: true }, (error, client) => {
const dataService = { client: { client } };
appRegistry.emit('data-service-initialized', dataService);
appRegistry.emit('data-service-connected', error, dataService);

if (error) {
console.error('Unable to connect to', localUri, error);
return;
}

console.info('Connected to', localUri);
const connectionOptions = {
url: 'mongodb://localhost:27020/test?readPreference=primary&ssl=false',
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
connectWithNoPrimary: true,
sslValidate: false,
},
};

appRegistry.emit('data-service-initialized', dataService);
appRegistry.emit('data-service-connected', error, dataService);
});
MongoClient.connect(
connectionOptions.url,
connectionOptions.options,
(error, client) => {
const dataService = {
client: { client },
getConnectionOptions() {
return connectionOptions;
},
};

appRegistry.emit('data-service-initialized', dataService);
appRegistry.emit('data-service-connected', error, dataService);

if (error) {
console.error('Unable to connect to', connectionOptions.url, error);
return;
}

console.info('Connected to', connectionOptions.url);

appRegistry.emit('data-service-initialized', dataService);
appRegistry.emit('data-service-connected', error, dataService);
}
);

if (module.hot) {
module.hot.accept('plugin', () => render(CompassShellPlugin));
Expand Down
8 changes: 4 additions & 4 deletions packages/compass-shell/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/compass-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@leafygreen-ui/icon-button": "^5.0.2",
"@mongosh/browser-repl": "0.0.0-dev.0",
"@mongosh/browser-runtime-electron": "0.0.0-dev.0",
"@mongosh/node-runtime-worker-thread": "0.0.0-dev.0",
"@mongosh/service-provider-server": "0.0.0-dev.0",
"hadron-react-buttons": "^4.0.4",
"mongodb-redux-common": "0.0.2"
Expand Down Expand Up @@ -93,7 +94,7 @@
"cross-env": "^5.0.1",
"css-loader": "^0.28.1",
"debug": "^3.0.1",
"electron": "^3.1.13",
"electron": "^6.1.7",
"electron-packager": "^8.7.0",
"electron-rebuild": "^1.10.0",
"enzyme": "^3.11.0",
Expand Down
23 changes: 18 additions & 5 deletions packages/compass-shell/src/modules/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { CompassServiceProvider } from '@mongosh/service-provider-server';
import { WorkerRuntime } from '@mongosh/node-runtime-worker-thread';

/**
* The prefix.
Expand Down Expand Up @@ -38,17 +39,29 @@ export default function reducer(state = INITIAL_STATE, action) {

function reduceSetupRuntime(state, action) {
if (action.error || !action.dataService) {
return {error: action.error, dataService: null, runtime: null};
return { error: action.error, dataService: null, runtime: null };
}

if (state.dataService === action.dataService) {
return state;
}

const runtime = new ElectronRuntime(
CompassServiceProvider.fromDataService(action.dataService),
action.appRegistry
);
const { url, options } = action.dataService.getConnectionOptions();

const runtime = !!process.env.COMPASS_SHELL_EXPERIMENTAL_WORKER_RUNTIME
? new WorkerRuntime(
url,
options,
{},
{
env: { ...process.env, ELECTRON_RUN_AS_NODE: 1 },
serialization: 'advanced',
}
)
: new ElectronRuntime(
CompassServiceProvider.fromDataService(action.dataService),
action.appRegistry
);

return {
error: action.error,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ChildProcess } from 'child_process';
import { EvaluationListener } from '@mongosh/shell-evaluator';
import { exposeAll, WithClose } from './rpc';
import type { WorkerRuntime } from './index';

export class ChildProcessEvaluationListener {
exposedListener: WithClose<EvaluationListener>;

constructor(workerRuntime: WorkerRuntime, childProcess: ChildProcess) {
this.exposedListener = exposeAll<EvaluationListener>(
{
onPrompt(question, type) {
return (
workerRuntime.evaluationListener?.onPrompt?.(question, type) ?? ''
);
},
onPrint(values) {
return workerRuntime.evaluationListener?.onPrint?.(values);
},
toggleTelemetry(enabled) {
return workerRuntime.evaluationListener?.toggleTelemetry?.(enabled);
},
onClearCommand() {
return workerRuntime.evaluationListener?.onClearCommand?.();
},
onExit() {
return (
workerRuntime.evaluationListener?.onExit?.() ??
(Promise.resolve() as Promise<never>)
);
}
},
childProcess
);
}
}
57 changes: 15 additions & 42 deletions packages/node-runtime-worker-thread/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
/* istanbul ignore file */
/* ^^^ we test the dist directly, so isntanbul can't calculate the coverage correctly */

import { ChildProcess } from 'child_process';
import { ChildProcess, SpawnOptionsWithoutStdio } from 'child_process';
import { MongoClientOptions } from '@mongosh/service-provider-core';
import { Runtime } from '@mongosh/browser-runtime-core';
import { EvaluationListener } from '@mongosh/shell-evaluator';
import spawnChildFromSource, { kill } from './spawn-child-from-source';
import { Caller, createCaller, exposeAll, WithClose } from './rpc';
import { Caller, createCaller } from './rpc';
import { ChildProcessEvaluationListener } from './child-process-evaluation-listener';
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime';
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy';

type ChildProcessRuntime = Caller<WorkerThreadWorkerRuntime>;

class WorkerEvaluationListener {
exposedListener: WithClose<EvaluationListener>;

constructor(workerRuntime: WorkerRuntime, childProcess: ChildProcess) {
this.exposedListener = exposeAll<EvaluationListener>(
{
onPrompt(question, type) {
return (
workerRuntime.evaluationListener?.onPrompt?.(question, type) ?? ''
);
},
onPrint(values) {
return workerRuntime.evaluationListener?.onPrint?.(values);
},
toggleTelemetry(enabled) {
return workerRuntime.evaluationListener?.toggleTelemetry?.(enabled);
},
onClearCommand() {
return workerRuntime.evaluationListener?.onClearCommand?.();
},
onExit() {
return (
workerRuntime.evaluationListener?.onExit?.() ??
(Promise.resolve() as Promise<never>)
);
}
},
childProcess
);
}
}

class WorkerRuntime implements Runtime {
private initOptions: {
uri: string;
driverOptions: MongoClientOptions;
cliOptions: { nodb?: boolean };
spawnOptions: SpawnOptionsWithoutStdio;
};

evaluationListener: EvaluationListener | null = null;

private childProcessEvaluationListener!: WorkerEvaluationListener;
private childProcessEvaluationListener!: ChildProcessEvaluationListener;

private childProcess!: ChildProcess;

Expand All @@ -64,27 +33,31 @@ class WorkerRuntime implements Runtime {
constructor(
uri: string,
driverOptions: MongoClientOptions = {},
cliOptions: { nodb?: boolean } = {}
cliOptions: { nodb?: boolean } = {},
spawnOptions: SpawnOptionsWithoutStdio = {}
) {
this.initOptions = { uri, driverOptions, cliOptions };
this.initOptions = { uri, driverOptions, cliOptions, spawnOptions };
this.initWorkerPromise = this.initWorker();
}

private async initWorker() {
this.childProcess = await spawnChildFromSource(childProcessProxySrc);
const { uri, driverOptions, cliOptions, spawnOptions } = this.initOptions;

this.childProcess = await spawnChildFromSource(
childProcessProxySrc,
spawnOptions
);

this.childProcessRuntime = createCaller(
['init', 'evaluate', 'getCompletions', 'setEvaluationListener'],
this.childProcess
);

this.childProcessEvaluationListener = new WorkerEvaluationListener(
this.childProcessEvaluationListener = new ChildProcessEvaluationListener(
this,
this.childProcess
);

const { uri, driverOptions, cliOptions } = this.initOptions;

await this.childProcessRuntime.init(uri, driverOptions, cliOptions);
}

Expand Down