{
  "slug": "shutdown-handler",
  "runtimes": {
    "node": {
      "frameworks": {
        "express": {
          "dependencies": {
            "runtime": [],
            "dev": []
          },
          "env": [],
          "architectures": {
            "mvc": {
              "files": [
                {
                  "type": "file",
                  "path": "src/utils/shutdown.ts",
                  "content": "import { Server } from \"http\";\r\n\r\n/**\r\n * Gracefully shuts down the server when a termination signal is received.\r\n * @param server The HTTP server instance to close.\r\n */\r\nexport const configureGracefulShutdown = (server: Server) => {\r\n  const signals = [\"SIGTERM\", \"SIGINT\"];\r\n\r\n  signals.forEach(signal => {\r\n    process.on(signal, () => {\r\n      console.log(`\\n${signal} signal received. Shutting down gracefully...`);\r\n\r\n      server.close(err => {\r\n        if (err) {\r\n          console.error(\"Error during server close:\", err);\r\n          process.exit(1);\r\n        }\r\n\r\n        console.log(\"HTTP server closed.\");\r\n        // Add additional cleanup logic here (e.g., closing database connections)\r\n        process.exit(0);\r\n      });\r\n\r\n      // Force shutdown after 10 seconds if graceful shutdown fails\r\n      setTimeout(() => {\r\n        console.error(\r\n          \"Could not close connections in time, forcefully shutting down\"\r\n        );\r\n        process.exit(1);\r\n      }, 10000);\r\n    });\r\n  });\r\n};\r\n"
                }
              ]
            },
            "feature": {
              "files": [
                {
                  "type": "file",
                  "path": "src/shared/utils/shutdown.ts",
                  "content": "import { Server } from \"http\";\r\n\r\n/**\r\n * Gracefully shuts down the server when a termination signal is received.\r\n * @param server The HTTP server instance to close.\r\n */\r\nexport const configureGracefulShutdown = (server: Server) => {\r\n  const signals = [\"SIGTERM\", \"SIGINT\"];\r\n\r\n  signals.forEach(signal => {\r\n    process.on(signal, () => {\r\n      console.log(`\\n${signal} signal received. Shutting down gracefully...`);\r\n\r\n      server.close(err => {\r\n        if (err) {\r\n          console.error(\"Error during server close:\", err);\r\n          process.exit(1);\r\n        }\r\n\r\n        console.log(\"HTTP server closed.\");\r\n        // Add additional cleanup logic here (e.g., closing database connections)\r\n        process.exit(0);\r\n      });\r\n\r\n      // Force shutdown after 10 seconds if graceful shutdown fails\r\n      setTimeout(() => {\r\n        console.error(\r\n          \"Could not close connections in time, forcefully shutting down\"\r\n        );\r\n        process.exit(1);\r\n      }, 10000);\r\n    });\r\n  });\r\n};\r\n"
                }
              ]
            }
          }
        }
      }
    }
  }
}
