refactor(js): process SIGINT and SIGTERM in clusters · grepjava/web-frameworks@c510c03 · GitHub
Skip to content

Commit c510c03

Browse files
committed
refactor(js): process SIGINT and SIGTERM in clusters
1 parent 2fe5245 commit c510c03

52 files changed

Lines changed: 602 additions & 132 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎javascript/0http-bun/cluster.ts‎

Lines changed: 19 additions & 6 deletions

‎javascript/0http/cluster.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import { availableParallelism } from 'node:os';
33

44
const numCpus = availableParallelism();
55

6-
if (numCpus > 1 && cluster.isPrimary) {
6+
if (cluster.isPrimary) {
77
for (let i = 0; i < numCpus; i++) {
88
cluster.fork();
99
}
10+
11+
function shutdown() {
12+
cluster.disconnect(() => process.exit(0));
13+
}
14+
15+
process.on('SIGINT', shutdown);
16+
process.on('SIGTERM', shutdown);
1017
} else {
1118
await import(`./${process.env.NODE_APP}`);
19+
console.log(`Worker ${process.pid} started`);
1220
}

‎javascript/adonisjs-http/cluster.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import { availableParallelism } from 'node:os';
33

44
const numCpus = availableParallelism();
55

6-
if (numCpus > 1 && cluster.isPrimary) {
6+
if (cluster.isPrimary) {
77
for (let i = 0; i < numCpus; i++) {
88
cluster.fork();
99
}
10+
11+
function shutdown() {
12+
cluster.disconnect(() => process.exit(0));
13+
}
14+
15+
process.on('SIGINT', shutdown);
16+
process.on('SIGTERM', shutdown);
1017
} else {
1118
await import(`./${process.env.NODE_APP}`);
19+
console.log(`Worker ${process.pid} started`);
1220
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import cluster from "node:cluster";
2-
import { availableParallelism } from "node:os";
1+
import cluster from 'node:cluster';
2+
import { availableParallelism } from 'node:os';
33

44
const numCpus = availableParallelism();
55

6-
if (numCpus > 1 && cluster.isPrimary) {
6+
if (cluster.isPrimary) {
77
for (let i = 0; i < numCpus; i++) {
88
cluster.fork();
99
}
10+
11+
function shutdown() {
12+
cluster.disconnect(() => process.exit(0));
13+
}
14+
15+
process.on('SIGINT', shutdown);
16+
process.on('SIGTERM', shutdown);
1017
} else {
1118
await import(`./${process.env.NODE_APP}`);
12-
console.log(`Worker ${process.pid} running at:`);
19+
console.log(`Worker ${process.pid} started`);
1320
}

‎javascript/blaze-bun/cluster.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import { availableParallelism } from 'node:os';
1+
import { spawn } from "bun";
22

3-
const numCpus = availableParallelism();
3+
const cpus = navigator.hardwareConcurrency;
4+
const buns = new Array(cpus);
45

5-
for (let i = 0; i < numCpus; i++) {
6-
Bun.spawn(['bun', 'app.ts'], {
7-
stdio: ['inherit', 'inherit', 'inherit'],
8-
env: { ...process.env },
6+
for (let i = 0; i < cpus; i++) {
7+
buns[i] = spawn({
8+
cmd: ["bun", "./app.ts"],
9+
stdio: ["inherit", "inherit", "inherit"],
910
});
11+
12+
console.log(`Worker ${buns[i].pid} started`);
13+
}
14+
15+
function kill() {
16+
for (const bun of buns) {
17+
bun.kill();
18+
}
1019
}
20+
21+
process.on("SIGINT", kill);
22+
process.on("SIGTERM", kill);
23+
process.on("exit", kill);

‎javascript/blaze-node/cluster.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import { availableParallelism } from 'node:os';
33

44
const numCpus = availableParallelism();
55

6-
if (numCpus > 1 && cluster.isPrimary) {
6+
if (cluster.isPrimary) {
77
for (let i = 0; i < numCpus; i++) {
88
cluster.fork();
99
}
10+
11+
function shutdown() {
12+
cluster.disconnect(() => process.exit(0));
13+
}
14+
15+
process.on('SIGINT', shutdown);
16+
process.on('SIGTERM', shutdown);
1017
} else {
1118
await import(`./${process.env.NODE_APP}`);
19+
console.log(`Worker ${process.pid} started`);
1220
}
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
// brahma-firelight v1.5.18+ now supports true multi-core ⚡⚡
1+
import { spawn } from "bun";
22

3-
// import { availableParallelism } from 'node:os';
3+
const cpus = navigator.hardwareConcurrency;
4+
const buns = new Array(cpus);
45

5-
// const numCpus = availableParallelism();
6+
for (let i = 0; i < cpus; i++) {
7+
buns[i] = spawn({
8+
cmd: ["bun", "./app.ts"],
9+
stdio: ["inherit", "inherit", "inherit"],
10+
});
611

7-
// for (let i = 0; i < numCpus; i++) {
8-
// Bun.spawn(['bun', 'app.ts'], {
9-
// stdio: ['inherit', 'inherit', 'inherit'],
10-
// env: { ...process.env },
11-
// });
12-
// }
12+
console.log(`Worker ${buns[i].pid} started`);
13+
}
1314

14-
// disabled for now
15-
import './app';
15+
function kill() {
16+
for (const bun of buns) {
17+
bun.kill();
18+
}
19+
}
20+
21+
process.on("SIGINT", kill);
22+
process.on("SIGTERM", kill);
23+
process.on("exit", kill);

‎javascript/bunicorn/cluster.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import { availableParallelism } from 'node:os';
1+
import { spawn } from "bun";
22

3-
const numCpus = availableParallelism();
3+
const cpus = navigator.hardwareConcurrency;
4+
const buns = new Array(cpus);
45

5-
for (let i = 0; i < numCpus; i++) {
6-
Bun.spawn(['bun', 'app.ts'], {
7-
stdio: ['inherit', 'inherit', 'inherit'],
8-
env: { ...process.env },
6+
for (let i = 0; i < cpus; i++) {
7+
buns[i] = spawn({
8+
cmd: ["bun", "./app.ts"],
9+
stdio: ["inherit", "inherit", "inherit"],
910
});
11+
12+
console.log(`Worker ${buns[i].pid} started`);
13+
}
14+
15+
function kill() {
16+
for (const bun of buns) {
17+
bun.kill();
18+
}
1019
}
20+
21+
process.on("SIGINT", kill);
22+
process.on("SIGTERM", kill);
23+
process.on("exit", kill);

‎javascript/chubbyts-uwebsockets/cluster.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import { availableParallelism } from 'node:os';
33

44
const numCpus = availableParallelism();
55

6-
if (numCpus > 1 && cluster.isPrimary) {
6+
if (cluster.isPrimary) {
77
for (let i = 0; i < numCpus; i++) {
88
cluster.fork();
99
}
10+
11+
function shutdown() {
12+
cluster.disconnect(() => process.exit(0));
13+
}
14+
15+
process.on('SIGINT', shutdown);
16+
process.on('SIGTERM', shutdown);
1017
} else {
1118
await import(`./${process.env.NODE_APP}`);
19+
console.log(`Worker ${process.pid} started`);
1220
}

‎javascript/chubbyts/cluster.mjs‎

Lines changed: 9 additions & 1 deletion

0 commit comments

Comments
 (0)