Skip to content

Commit deff1e0

Browse files
committed
Graceful shutdown
1 parent 952a311 commit deff1e0

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ FROM installer AS sources
1818
COPY src /build/src
1919
COPY examples /build/examples
2020
COPY tsconfig.json /build/tsconfig.json
21+
COPY server.ts /build
2122

2223
# Builder Image
2324
FROM sources AS builder

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "bun --hot src/index.ts",
88
"start": "bun src/index.ts",
9-
"compile": "bun build --compile --minify --sourcemap src/index.ts --outfile dist/openworkers-api",
9+
"compile": "bun build --compile --minify --sourcemap server.ts --outfile dist/openworkers-api",
1010
"test": "bun test"
1111
},
1212
"dependencies": {

server.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Production server entrypoint with graceful shutdown support
3+
*
4+
* This file is used for:
5+
* - Production runtime (bun server.ts)
6+
* - Compiled binary (bun run compile)
7+
*
8+
* It explicitly creates a Bun server to handle SIGTERM/SIGINT signals
9+
* for graceful shutdown (Docker, Ctrl+C, process managers).
10+
*
11+
* For development, use `bun --hot src/index.ts` which relies on
12+
* the default export and lets Bun handle hot reload lifecycle.
13+
*/
14+
import app from "./src";
15+
16+
const server = Bun.serve(app);
17+
18+
process.on("SIGTERM", () => {
19+
console.log("SIGTERM received, shutting down gracefully...");
20+
server.stop(true);
21+
});
22+
23+
process.on("SIGINT", () => {
24+
console.log("SIGINT received, shutting down gracefully...");
25+
server.stop(true);
26+
});

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ v1.route("/", users);
4040

4141
app.route("/api/v1", v1);
4242

43-
import { port } from "./config";
43+
import { nodeEnv, port } from "./config";
4444

4545
// Start server
4646
console.log(`OpenWorkers API starting on port ${port}...`);
4747

4848
export default {
4949
port,
5050
fetch: app.fetch,
51+
development: nodeEnv === "development",
5152
};

0 commit comments

Comments
 (0)