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

fix: cleanup after pages dev tests #678

Merged
merged 1 commit into from
Mar 23, 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
10 changes: 10 additions & 0 deletions .changeset/young-boats-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

fix: cleanup after `pages dev` tests

We weren't killing the process started by wrangler whenever its parent was killed. This fix is to listen on SIGINT/SIGTERM and kill that process. I also did some minor configuration cleanups.

Fixes https://github.com/cloudflare/wrangler2/issues/397
Fixes https://github.com/cloudflare/wrangler2/issues/618
6 changes: 3 additions & 3 deletions packages/example-pages-functions-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"name": "example-pages-functions-app",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "npx wrangler pages dev public --port 8789",
"test": "npx jest --forceExit"
Expand Down Expand Up @@ -29,6 +30,5 @@
}
]
}
},
"version": null
}
}
17 changes: 13 additions & 4 deletions packages/example-pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "child_process";
import { resolve } from "path";
import * as path from "path";
import { fetch } from "undici";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";
Expand All @@ -26,7 +26,7 @@ describe("Pages Functions", () => {
beforeAll(async () => {
wranglerProcess = spawn("npm", ["run", "dev"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
env: { BROWSER: "none", ...process.env },
});
wranglerProcess.stdout.on("data", (chunk) => {
Expand All @@ -37,8 +37,17 @@ describe("Pages Functions", () => {
});
});

afterAll(() => {
wranglerProcess.kill();
afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill();
});
});

it("renders static pages", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/example-pages-functions-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["@cloudflare/workers-types"]
"types": ["@cloudflare/workers-types"],
"moduleResolution": "node"
}
}
3 changes: 2 additions & 1 deletion packages/example-remix-pages-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"name": "example-remix-pages-app",
"version": "0.0.0",
"private": true,
"description": "",
"license": "",
"scripts": {
Expand Down
23 changes: 16 additions & 7 deletions packages/example-remix-pages-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn, spawnSync } from "child_process";
import { resolve } from "path";
import * as path from "path";
import { fetch } from "undici";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";
Expand All @@ -26,23 +26,32 @@ describe("Remix", () => {
beforeAll(async () => {
spawnSync("npm", ["run", "build"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
});
wranglerProcess = spawn("npm", ["run", "dev:wrangler"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
env: { BROWSER: "none", ...process.env },
});
wranglerProcess.stdout.on("data", (chunk) => {
wranglerProcess.stdout?.on("data", (chunk) => {
console.log(chunk.toString());
});
wranglerProcess.stderr.on("data", (chunk) => {
wranglerProcess.stderr?.on("data", (chunk) => {
console.log(chunk.toString());
});
});

afterAll(() => {
wranglerProcess.kill();
afterAll(async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably clean this up by using execa here, but I'll leave that for some other time/person

await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill();
});
});

it("renders", async () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/wrangler/bin/wrangler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const semiver = require("semiver");

const MIN_NODE_VERSION = "16.7.0";

let wranglerProcess;

async function main() {
if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
// Note Volta and nvm are also recommended in the official docs:
Expand All @@ -18,7 +20,7 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
return;
}

spawn(
wranglerProcess = spawn(
process.execPath,
[
"--no-warnings",
Expand All @@ -33,4 +35,11 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
);
}

process.on("SIGINT", () => {
wranglerProcess.kill();
});
process.on("SIGTERM", () => {
wranglerProcess.kill();
});

void main();
1 change: 1 addition & 0 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const EXIT = (message?: string, code?: number) => {
if (message) console.log(message);
if (code) process.exitCode = code;
EXIT_CALLBACKS.forEach((callback) => callback());
RUNNING_BUILDERS.forEach((builder) => builder.stop?.());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleans up hanging esbuild processes

process.exit(code);
};

Expand Down