Skip to content

Commit

Permalink
feat: add vite watchdog (#12230)
Browse files Browse the repository at this point in the history
Add watchdog to stop vite when java handler exits or crashes.

Fixes #12060
  • Loading branch information
caalador committed Nov 2, 2021
1 parent 2acef65 commit e68e335
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
28 changes: 26 additions & 2 deletions flow-server/src/main/resources/vite.generated.ts
Expand Up @@ -5,6 +5,7 @@
* This file will be overwritten on every run. Any custom changes should be made to vite.config.ts
*/
import path from 'path';
import * as net from 'net';

import { processThemeResources } from '@vaadin/application-theme-plugin/theme-handle.js';
import settings from '#settingsImport#';
Expand Down Expand Up @@ -53,7 +54,29 @@ function updateTheme(contextPath: string) {
}
}

export const vaadinConfig:UserConfigFn = (env) => ({
function runWatchDog(watchDogPort) {
const client = net.Socket();
client.setEncoding('utf8');
client.on('error', function () {
console.log("Watchdog connection error. Terminating vite process...");
client.destroy();
process.exit(0);
});
client.on('close', function () {
client.destroy();
runWatchDog(watchDogPort);
});

client.connect(watchDogPort, 'localhost');
}

export const vaadinConfig:UserConfigFn = (env) => {
if (env.mode === 'development' && process.env.watchDogPort) {
// Open a connection with the Java dev-mode handler in order to finish
// vite when it exits or crashes.
runWatchDog(process.env.watchDogPort);
}
return ({
root: 'frontend',
base: env.mode === 'production' ? '' : '/VAADIN/',
resolve: {
Expand Down Expand Up @@ -87,7 +110,8 @@ export const vaadinConfig:UserConfigFn = (env) => ({
}
}
]
});
})
};


export const overrideVaadinConfig = (customConfig:UserConfigFn) => {
Expand Down
Expand Up @@ -342,6 +342,8 @@ protected Process doStartDevServer() {
}

processBuilder.command(command);
processBuilder.environment().put("watchDogPort",
Integer.toString(getWatchDog().getWatchDogPort()));

try {
Process process = processBuilder.redirectErrorStream(true).start();
Expand Down
Expand Up @@ -173,8 +173,7 @@ protected List<String> getServerStartupCommand(String nodeExec) {
command.add(String.valueOf(getPort()));
command.add("--watch-options-stdin"); // Tell wds to stop even if
// watchDog fail
command.add("--env");
command.add("watchDogPort=" + getWatchDog().getWatchDogPort());

String customParameters = getApplicationConfiguration()
.getStringProperty(
InitParameters.SERVLET_PARAMETER_DEVMODE_WEBPACK_OPTIONS,
Expand Down
Expand Up @@ -12,7 +12,6 @@
import com.vaadin.base.devserver.AbstractDevServerRunner;
import com.vaadin.base.devserver.DevModeHandlerManagerImpl;
import com.vaadin.base.devserver.MockDeploymentConfiguration;
import com.vaadin.base.devserver.WebpackHandler;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
Expand Down Expand Up @@ -137,7 +136,7 @@ protected DevModeHandler getDevModeHandler() {
}

protected int getDevServerPort() {
return ((WebpackHandler) handler).getPort();
return ((AbstractDevServerRunner) handler).getPort();
}

protected void waitForDevServer() {
Expand All @@ -155,7 +154,7 @@ protected WebpackHandler startWebpack(int port) {

protected static void waitForDevServer(DevModeHandler devModeHandler) {
Assert.assertNotNull(devModeHandler);
((WebpackHandler) (devModeHandler)).waitForDevServer();
((AbstractDevServerRunner) (devModeHandler)).waitForDevServer();
}

protected static boolean hasDevServerProcess(
Expand Down

0 comments on commit e68e335

Please sign in to comment.