Skip to content

Commit

Permalink
Merge pull request #30 from dockersamples/add-signal-handling
Browse files Browse the repository at this point in the history
Add proper signal handling for graceful shutdowns
  • Loading branch information
mikesir87 authored Jan 16, 2025
2 parents 5eaebef + 9812245 commit fa7565c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const os = require("os");
const fs = require("fs");
const express = require("express");
const ProductService = require("./services/ProductService");
const PublisherService = require("./services/PublisherService");
const multer = require("multer");

const app = express();
Expand Down Expand Up @@ -73,3 +74,12 @@ app.post("/api/products/:id/image", upload.single("file"), async (req, res) => {
app.listen(3000, () => {
console.log("Server is running on port 3000");
});

["SIGINT", "SIGTERM"].forEach((signal) => {
process.on(signal, async () => {
console.log(`Received ${signal}, shutting down...`);
await ProductService.teardown();
await PublisherService.teardown();
process.exit(0);
});
});

0 comments on commit fa7565c

Please sign in to comment.