@ fastify/ 可重新启动
重新启动快速化而不丢失请求 .
此模块有用, 如果您想要作曲 动态固定路由或需要一些远程 相会. 如果有变化,可以重新启动快取.
安装
npm i @fastify/restartable
使用量
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()
})
钩子
-
preRestart- 在创建新的应用实例和关闭 现有。 钩子以当前应用实例为参数而叫. 用于关闭您不想共享的资源 应用程序实例. -
onRestart- 在新应用实例和旧实例创建后呼叫 关闭。 钩子以新的应用实例作为论据.
示例数字 :
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()
产出数字 :
preRestart hook called
creating new app instance
onRestart hook called
许可证
许可证根据 麻省理工学院。 。 。 .