Skip to content

Commit

Permalink
electron: always fork except if --no-cluster
Browse files Browse the repository at this point in the history
The generated application code is not forking the backend when working
from sources. Running the backend in the Electron main process was only
meant to help with debugging. This commit makes the generated code fork
the backend unless a `--no-cluster` flag is specified.

If you want the generated code to not fork the backend in a sub-process,
you can either pass a `--no-cluster` flag to your application, or set
`THEIA_ELECTRON_NO_BACKEND_FORK=1` environment variable.

Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Mar 25, 2020
1 parent 5b6afb4 commit 492db2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ app.on('ready', () => {
// Check whether we are in bundled application or development mode.
// @ts-ignore
const devMode = process.defaultApp || /node_modules[\/]electron[\/]/.test(process.execPath);
// Check if user wants to run everything as one process.
const noBackendFork = Boolean(process.env['THEIA_ELECTRON_NO_BACKEND_FORK']) || process.argv.includes('--no-cluster');
const mainWindow = createNewWindow();
if (isSingleInstance) {
Expand Down Expand Up @@ -331,9 +333,10 @@ app.on('ready', () => {
process.env.THEIA_ELECTRON_VERSION = process.versions.electron;
const mainPath = join(__dirname, '..', 'backend', 'main');
// We need to distinguish between bundled application and development mode when starting the clusters.
// See: https://github.com/electron/electron/issues/6337#issuecomment-230183287
if (devMode) {
// We spawn a separate process for the backend for Express to not run in the Electron main process.
// See: https://github.com/eclipse-theia/theia/pull/7361#issuecomment-601272212
// But when in debugging we want to run everything in the same process to make things easier.
if (noBackendFork) {
process.env[ElectronSecurityToken] = JSON.stringify(electronSecurityToken);
require(mainPath).then(address => {
loadMainWindow(address.port);
Expand Down
15 changes: 15 additions & 0 deletions dev-packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [**Inspecting Tests**](#inspecting-tests)
- [**Reporting Test Coverage**](#reporting-test-coverage)
- [**Downloading Plugins**](#downloading-plugins)
- [**Autogenerated Application**](#autogenerated-application)

## Description

Expand Down Expand Up @@ -270,6 +271,20 @@ The property `theiaPlugins` describes the list of plugins to download, for examp
}
```

## Autogenerated Application

This package can auto-generate application code for both the backend and frontend, as well as webpack configuration files.

When targeting Electron, the `electron-main.js` script will spawn the backend process in a Node.js sub-process, where Electron's API won't be available. To prevent the generated application from forking the backend, you can pass a `--no-cluster` flag, or set an environment variable like `THEIA_ELECTRON_NO_BACKEND_FORK=1`.

```sh
# when developing a Theia application with @theia/cli:
yarn theia start --no-cluster

# when starting a bundled application made using @theia/cli:
bundled-application.exe --no-cluster
```

## Additional Information

- [Theia - GitHub](https://github.com/eclipse-theia/theia)
Expand Down

0 comments on commit 492db2b

Please sign in to comment.