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(node-cluster): fix node cluster preset #2893

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions src/presets/node/runtime/node-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ function runMaster() {
}

function runWorker() {
import("./node-server").catch((error) => {
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
import("./node-server")
.then(({ serverWorker }) => serverWorker())
.catch((error) => {
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
}

// Trap unhandled errors
Expand Down
105 changes: 55 additions & 50 deletions src/presets/node/runtime/node-server.ts
Copy link
Member

Choose a reason for hiding this comment

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

This entry is also used for default nitro server-preset that requires the entrypoint itself to immediately start listening and this PR breaks it!

image

I think we should find better fix or at least duplicate this entry to be specific solution for node_cluster.

Copy link
Author

Choose a reason for hiding this comment

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

Eh.. I didn't know about that :/ Sorry, my mistake 😭

Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,65 @@ import {
trapUnhandledNodeErrors,
} from "nitropack/runtime/internal";

const cert = process.env.NITRO_SSL_CERT;
const key = process.env.NITRO_SSL_KEY;
export const serverWorker = async () => {

const nitroApp = useNitroApp();

const server =
cert && key
? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App))
: new HttpServer(toNodeListener(nitroApp.h3App));

const port = (destr(process.env.NITRO_PORT || process.env.PORT) ||
3000) as number;
const host = process.env.NITRO_HOST || process.env.HOST;

const path = process.env.NITRO_UNIX_SOCKET;

// @ts-ignore
const listener = server.listen(path ? { path } : { port, host }, (err) => {
if (err) {
console.error(err);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
const cert = process.env.NITRO_SSL_CERT;
const key = process.env.NITRO_SSL_KEY;

const nitroApp = useNitroApp();

const server =
cert && key
? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App))
: new HttpServer(toNodeListener(nitroApp.h3App));

const port = (destr(process.env.NITRO_PORT || process.env.PORT) ||
3000) as number;
const host = process.env.NITRO_HOST || process.env.HOST;

const path = process.env.NITRO_UNIX_SOCKET;

// @ts-ignore
const listener = server.listen(path ? { path } : { port, host }, (err) => {
if (err) {
console.error(err);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
const protocol = cert && key ? "https" : "http";
const addressInfo = listener.address() as AddressInfo;
if (typeof addressInfo === "string") {
console.log(`Listening on unix socket ${addressInfo}`);
return;
}
const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, "");
const url = `${protocol}://${
addressInfo.family === "IPv6"
? `[${addressInfo.address}]`
: addressInfo.address
}:${addressInfo.port}${baseURL}`;
console.log(`Listening on ${url}`);
});

// Trap unhandled errors
trapUnhandledNodeErrors();

// Graceful shutdown
setupGracefulShutdown(listener, nitroApp);

// Websocket support
// https://crossws.unjs.io/adapters/node
if (import.meta._websocket) {
const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket);
server.on("upgrade", handleUpgrade);
}
const protocol = cert && key ? "https" : "http";
const addressInfo = listener.address() as AddressInfo;
if (typeof addressInfo === "string") {
console.log(`Listening on unix socket ${addressInfo}`);
return;

// Scheduled tasks
if (import.meta._tasks) {
startScheduleRunner();
}
const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, "");
const url = `${protocol}://${
addressInfo.family === "IPv6"
? `[${addressInfo.address}]`
: addressInfo.address
}:${addressInfo.port}${baseURL}`;
console.log(`Listening on ${url}`);
});

// Trap unhandled errors
trapUnhandledNodeErrors();

// Graceful shutdown
setupGracefulShutdown(listener, nitroApp);

// Websocket support
// https://crossws.unjs.io/adapters/node
if (import.meta._websocket) {
const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket);
server.on("upgrade", handleUpgrade);

}

// Scheduled tasks
if (import.meta._tasks) {
startScheduleRunner();
}

export default {};
Loading