From 6afd222515a9b966ce28e413aaea04d75ead89a9 Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@3nprob> Date: Thu, 7 Oct 2021 00:38:01 +0900 Subject: [PATCH] Gracefully exit on SIGTERM/SIGKILL/SIGQUIT --- package-lock.json | 25 +++++++++++++++++++++++++ package.json | 1 + src/index.ts | 29 ++++++++++++++++++----------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 48dce46..db81898 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@hapi/inert": "^6.0.4", "@hapi/vision": "^6.1.0", "cheerio": "^1.0.0-rc.10", + "exiting": "^6.0.1", "got": "^11.8.2", "hpagent": "^0.1.2", "pug": "^3.0.2" @@ -1224,6 +1225,21 @@ "node": ">=0.8.0" } }, + "node_modules/exiting": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/exiting/-/exiting-6.0.1.tgz", + "integrity": "sha512-0kUQkyWTMJUZ2wKkxjducVojsL5vtDxw26q9sd07SwyWZswbHOrWN9Bs2jk9uXffatsGp2QP5tmQUYXiPi1Z2A==", + "dependencies": { + "@hapi/bounce": "^2.0.0", + "@hapi/hoek": "^9.0.2" + }, + "engines": { + "node": ">=12.13.0" + }, + "peerDependencies": { + "@hapi/hapi": ">=17.9.0" + } + }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -4359,6 +4375,15 @@ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", "dev": true }, + "exiting": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/exiting/-/exiting-6.0.1.tgz", + "integrity": "sha512-0kUQkyWTMJUZ2wKkxjducVojsL5vtDxw26q9sd07SwyWZswbHOrWN9Bs2jk9uXffatsGp2QP5tmQUYXiPi1Z2A==", + "requires": { + "@hapi/bounce": "^2.0.0", + "@hapi/hoek": "^9.0.2" + } + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", diff --git a/package.json b/package.json index a9198a2..e5f6c4d 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@hapi/inert": "^6.0.4", "@hapi/vision": "^6.1.0", "cheerio": "^1.0.0-rc.10", + "exiting": "^6.0.1", "got": "^11.8.2", "hpagent": "^0.1.2", "pug": "^3.0.2" diff --git a/src/index.ts b/src/index.ts index 872ec86..638a530 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,29 @@ 'use strict'; import Hapi = require('@hapi/hapi'); +const Exiting = require('exiting'); import Path = require('path'); import { handleAlbum, handleGallery, handleMedia, handleTag, handleUser } from './handlers'; import CONFIG from './config'; -const init = async () => { - const server = Hapi.server({ - port: CONFIG.port, - host: CONFIG.host, - address: CONFIG.address, - routes: { - files: { - relativeTo: Path.join(__dirname, 'static') - } +const server = Hapi.server({ + port: CONFIG.port, + host: CONFIG.host, + address: CONFIG.address, + routes: { + files: { + relativeTo: Path.join(__dirname, 'static') } - }); + } +}); +server.events.on('stop', () => { + console.log('Server stopped.'); +}); + +const manager = Exiting.createManager(server); + +const init = async () => { await server.register(require('@hapi/vision')); await server.register(require('@hapi/inert')); @@ -62,7 +69,7 @@ const init = async () => { handler: handleGallery, }); - await server.start(); + await manager.start(); console.log('Server running on %s', server.info.uri); };