Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fastify/restartable
Restart Fastify without losing a request
https://github.com/fastify/restartable
fastify fastify-library
Last synced: 6 days ago
JSON representation
Restart Fastify without losing a request
- Host: GitHub
- URL: https://github.com/fastify/restartable
- Owner: fastify
- License: mit
- Created: 2022-02-19T12:45:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-15T15:26:38.000Z (16 days ago)
- Last Synced: 2025-01-19T08:11:52.360Z (13 days ago)
- Topics: fastify, fastify-library
- Language: JavaScript
- Homepage: https://npmjs.com/package/@fastify/restartable
- Size: 108 KB
- Stars: 106
- Watchers: 9
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @fastify/restartable
[![NPM version](https://img.shields.io/npm/v/@fastify/restartable.svg?style=flat)](https://www.npmjs.com/package/@fastify/restartable)
[![CI](https://github.com/fastify/restartable/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/restartable/actions/workflows/ci.yml)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)Restart Fastify without losing a request.
This module is useful if you want to compose the
fastify routes dynamically or you need some remote
config. In case of a change, you can restart Fastify.## Install
```bash
npm i @fastify/restartable
```## Usage
```js
import { restartable } from '@fastify/restartable'async function createApp (fastify, opts) {
const app = fastify(opts)app.get('/restart', async () => {
await app.restart()
return { status: 'ok' }
})app.addHook('onClose', async () => {
if(!app.closingRestartable) {
console.log('closing the app because of restart')
}
else{
console.log('closing the app because server is stopping')
}
})return app
}const app = await restartable(createApp, { logger: true })
const host = await app.listen({ port: 3000 })console.log('server listening on', host)
// call restart() if you want to restart
process.on('SIGUSR1', () => {
console.log('Restarting the server')
app.restart()
})process.once('SIGINT', () => {
console.log('Stopping the server')
app.close()
})```
## Hooks
- `preRestart` - called before creating a new app instance and closing an
existing one. The hook is called with the current app instance as an argument.
Use it to close any resources that you don't want to be shared between the
app instances.- `onRestart` - called after the new app instance is created and the old one
is closed. The hook is called with the new app instance as an argument.**Example**:
```js
async function createApplication (fastify, opts) {
console.log('creating new app instance')
return fastify(opts)
}
const app = await restartable(createApplication)app.addPreRestartHook(async (app) => {
console.log('preRestart hook called')
})app.addOnRestartHook(async (app) => {
console.log('onRestart hook called')
})await app.restart()
```**Output**:
```bash
preRestart hook called
creating new app instance
onRestart hook called
```## License
Licensed under [MIT](./LICENSE).