{"id":14957688,"url":"https://github.com/omrilotan/graceful-shutdown","last_synced_at":"2025-10-30T02:46:49.055Z","repository":{"id":46806659,"uuid":"203758298","full_name":"omrilotan/graceful-shutdown","owner":"omrilotan","description":"💀 Shut down server gracefully","archived":false,"fork":false,"pushed_at":"2021-09-24T12:57:52.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T06:04:28.537Z","etag":null,"topics":["express","expressjs","graceful-shutdown","nodejs","server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omrilotan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-22T09:13:51.000Z","updated_at":"2021-06-18T14:40:26.000Z","dependencies_parsed_at":"2022-09-26T18:22:00.627Z","dependency_job_id":null,"html_url":"https://github.com/omrilotan/graceful-shutdown","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omrilotan%2Fgraceful-shutdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omrilotan%2Fgraceful-shutdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omrilotan%2Fgraceful-shutdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omrilotan%2Fgraceful-shutdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omrilotan","download_url":"https://codeload.github.com/omrilotan/graceful-shutdown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237964466,"owners_count":19394404,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","expressjs","graceful-shutdown","nodejs","server"],"created_at":"2024-09-24T13:15:20.898Z","updated_at":"2025-10-18T16:59:54.022Z","avatar_url":"https://github.com/omrilotan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @routes/graceful-shutdown \u003ca href=\"https://www.npmjs.com/package/@routes/graceful-shutdown\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@routes/graceful-shutdown.svg\"\u003e\u003c/a\u003e [![](https://img.shields.io/badge/source--000000.svg?logo=github\u0026style=social)](https://github.com/omrilotan/graceful-shutdown)\n\n## 💀 Shut down server gracefully\n\n[![](https://github.com/omrilotan/graceful-shutdown/workflows/Publish/badge.svg)](https://github.com/omrilotan/graceful-shutdown/actions) [![](https://snyk.io/test/github/omrilotan/graceful-shutdown/badge.svg)](https://snyk.io/test/github/omrilotan/graceful-shutdown) [![](https://api.codeclimate.com/v1/badges/7914da297e8693bba8f6/maintainability)](https://codeclimate.com/github/omrilotan/graceful-shutdown/maintainability)\n\n[net.Server](https://nodejs.org/api/net.html#net_class_net_server) or [Express](https://expressjs.com/en/api.html#app.listen), whatever you're using should be fine\n\n```js\nconst graceful = require('@routes/graceful-shutdown');\n\nconst server = app.listen(1337); // express will return the server instance here\ngraceful(server);\n```\n\n### Arguments\n- First argument is an net.Server instance (including Express server)\n- Second argument is options:\n\n| option | type | meaning | default\n| - | - | - | -\n| `timeout` | Number | Time (in milliseconds) to wait before forcefully shutting down | `10000` (10 sec)\n| `logger` | Object | Object with `info` and `error` functions (supports async loggers). Pass `false` to disable | `console`\n| `events` | Array | Process events to handle | `['SIGTERM', 'SIGINT']`\n| `onsuccess` | Function | Final functionality when shutdown finished correctly | `process.exit(0)`\n| `onfail` | Function | Final functionality when shutdown finished incorrectly | `process.exit(1)`\n\nExample of using options\n```js\ngraceful(server, {\n\ttimeout: 3e4,\n\tlogger: winston.createLogger({level: 'error'}),\n});\n```\n\n## What happens on process termination?\n1. Process termination is interrupted\n2. Open connections are instructed to end (FIN packet) and receive a new timeout to allow them to close in time\n3. The server is firing a close function\n4. After correct closing: `onsuccess` (default: process exists with exit code 0)\n\t- _End correct behaviour_\n5. After `timeout` passes - an error log is printed with the amount of connection that will be forcefully terminated\n6. `onfail`: (default: process exists with an exit code 1)\n\n## Add custom functionality to shutdown\nAdd behaviour to the graceful shut down process using a built in pub/sub mechanism\n```js\nconst { sub, BEFORE, AFTER } = graceful(server, {...});\n\n// Will be triggered first thing before the procedure starts\nsub(BEFORE, async() =\u003e {\n\tawait flushThrottledThings();\n\tawait closeDatabaseConnections();\n});\n\n// Will be triggered once the procedure has ended\nsub(AFTER, () =\u003e logger.info('Okay okay, closing down'));\n```\n\n## `server.shuttingDown`\nAfter shutdown has initiated, the attribute `shuttingDown` is attached to server with value of `true`.\n\nUser can query this value on service to know not to send any more requests to the service\n```js\napp.get(\n\t'/health',\n\t(request, response) =\u003e response.status(server.shuttingDown ? 503 : 200).end()\n);\n```\n\n## What else does graceful expose?\nMonitor size of connections set. Should be similar open server connections\n\n- `{Set} sockets` A reference to the sockets collection\n```js\nconst { sockets } = graceful(server, {...});\n\n// Monitor size of open connections every two minutes\nsetInterval(() =\u003e {\n\tstats.gauge('graceful_stored_sockets', sockets.size);\n\n  server.getConnections((error, connections) =\u003e {\n    if (error) {\n      throw error;\n    } else {\n      stats.gauge('server_open_connections', connections);\n    }\n  });\n}, 12e4);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomrilotan%2Fgraceful-shutdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomrilotan%2Fgraceful-shutdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomrilotan%2Fgraceful-shutdown/lists"}