Skip to content
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

Prevent project shutdown on watch mode #47

Merged
merged 1 commit into from
Jul 7, 2022
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
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ wds --inspect some-test.test.ts

- Builds and runs TypeScript really fast (using [`swc`](https://github.com/swc-project/swc) or [`esbuild`](https://github.com/evanw/esbuild))
- Incrementally rebuilds only what has changed in the `--watch` mode, and restarts the process when files change
- Supervises the node.js process with `--supervise` to keep incremental context around on process crash, and can restart on demand in the `--commands` mode
- Execute commands on demand with the `--commands` mode
- Plays nice with node.js command line flags like `--inspect` or `--prof`
- Produces sourcemaps which Just Work™️ by default for debugging with many editors (VSCode, IntelliJ, etc)
- Monorepo aware, allowing for different configuration per package and only compiling what is actually required from the monorepo context
Expand Down
1 change: 0 additions & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface RunOptions {
argv: string[];
terminalCommands: boolean;
reloadOnChanges: boolean;
supervise: boolean;
useEsbuild: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Supervisor extends EventEmitter {

this.process.on("message", (value) => this.emit("message", value));
this.process.on("exit", (code, signal) => {
if (signal !== "SIGKILL" && this.options.supervise) {
if (signal !== "SIGKILL") {
log.warn(`process exited with ${code}`);
}
});
Expand Down
7 changes: 3 additions & 4 deletions src/bench/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ function monitorLogs(childProcess: ChildProcess): Promise<ChildProcessResult> {
});
}

function spawnOnce(args: { supervise?: boolean; filename: string; esbuild: boolean }): ChildProcess {
function spawnOnce(args: { watch?: boolean; filename: string; esbuild: boolean }): ChildProcess {
const extraArgs = [];

if (args.supervise) {
extraArgs.push("--supervise");
if (args.watch) {
extraArgs.push("--watch");
}

Expand Down Expand Up @@ -158,7 +157,7 @@ export async function benchReload(args: BenchArgs): Promise<void> {

process.stdout.write(`Starting reload benchmark (pid=${process.pid})\n`);

const childProcess = spawnOnce({ supervise: true, filename: filepath, esbuild: args.esbuild });
const childProcess = spawnOnce({ watch: true, filename: filepath, esbuild: args.esbuild });
const _ignoreInitialBoot = await monitorLogs(childProcess);

for (let i = 0; i < args.runs; i++) {
Expand Down
20 changes: 9 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ export const cli = async () => {
description: "Trigger restarts by watching for changes to required files",
default: true,
})
.option("supervise", {
alias: "s",
type: "boolean",
description: "Supervise and restart the process when it exits indefinitely",
default: false,
})
.option("esbuild", {
type: "boolean",
description: "Use esbuild instead of sec",
Expand All @@ -49,7 +43,6 @@ export const cli = async () => {
argv: args._ as any,
terminalCommands: args.commands,
reloadOnChanges: args.watch,
supervise: args.supervise,
useEsbuild: args.esbuild,
});
};
Expand Down Expand Up @@ -157,7 +150,7 @@ export const wds = async (options: RunOptions) => {
await startIPCServer(serverSocketPath, project);

// kickoff the first child process
options.supervise && log.info(`Supervision starting for command: node ${options.argv.join(" ")}`);
elyse0 marked this conversation as resolved.
Show resolved Hide resolved
options.reloadOnChanges && log.info(`Supervision starting for command: node ${options.argv.join(" ")}`);
await project.invalidateBuildSetAndReload();

process.on("SIGINT", () => {
Expand All @@ -170,9 +163,14 @@ export const wds = async (options: RunOptions) => {
});

project.supervisor.process.on("exit", (code) => {
log.debug(`child process exited with code ${code}, ${options.supervise ? "not exiting because supervise mode" : "exiting..."}`);
if (!options.supervise) {
project.shutdown(code ?? 1);
const logShutdown = (explanation: string) => {
log.debug(`child process exited with code ${code}, ${explanation}`);
};
if (options.reloadOnChanges) {
logShutdown("not exiting because we're on 'watch' mode");
return;
}
logShutdown("shutting down project since it's no longer needed...");
project.shutdown(code ?? 1);
});
};
6 changes: 3 additions & 3 deletions test/reload/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ trap "kill -9 0" INT TERM
trap 'kill $(jobs -p)' EXIT

# run a server in the background
$DIR/../../pkg/wds.bin.js $@ --supervise --commands $DIR/run-scratch.ts &
$DIR/../../pkg/wds.bin.js $@ --commands $DIR/run-scratch.ts &

max_retry=5
counter=0

set +e
set +e
until curl -s localhost:8080 | grep "World"
do
sleep 1
Expand All @@ -39,4 +39,4 @@ done

echo "Made new request to reloaded server"

exit 0
exit 0