Skip to content

Commit 9ec16cf

Browse files
author
Immanuel Pelzer
committed
Add graceful shutdown
1 parent 1696897 commit 9ec16cf

File tree

9 files changed

+7378
-1019
lines changed

9 files changed

+7378
-1019
lines changed

.gitlab-ci.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ variables:
2020
RABBITMQ_DEFAULT_PASS: "guest"
2121
ESLINT_CODE_QUALITY_REPORT: gl-codequality.json
2222

23-
services:
24-
- r.unueng.com/cloud/rabbitmq-plugins:3.9-1
2523

2624
setup:
2725
stage: build
@@ -39,12 +37,18 @@ test:
3937
script:
4038
# Wait for RabbitMQ to be running
4139
- while ! nc -z localhost 15692 ; do sleep 1 ; done
40+
- npm run test-graceful-shutdown
4241
- npm run test
4342
- npm run test-coverage
4443
cache:
4544
policy: pull
4645
paths:
4746
- node_modules/
47+
services:
48+
- name: r.unueng.com/cloud/rabbitmq-plugins:3.9-1
49+
alias: rabbitmq
50+
- name: mongo:6.0
51+
alias: mongodb
4852
coverage: '/Statements.*?(\d+(?:\.\d+)?)%/'
4953
artifacts:
5054
reports:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [5.3.0] - 2023-04-26
8+
9+
- Add graceful shutdown for Mongoose, Express and AMQP. Mongoose is optional
10+
711
# [5.2.0] - 2023-04-18
812

913
- Update dependency `got` to latest version

examples/pagination/service.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
12
import mongoose from 'mongoose'
23

34
// https://github.com/aravindnc/mongoose-paginate-v2

lib/helpers/mongo.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import debugFactory from 'debug'
2+
3+
const debug = debugFactory('taube-mongo')
4+
5+
/**
6+
* Only shutdown mongoose if it is available.
7+
*/
8+
async function shutdown() {
9+
try {
10+
// eslint-disable-next-line import/no-extraneous-dependencies
11+
const mymodule = await import('mongoose')
12+
const mongoose = mymodule?.default
13+
await mongoose?.disconnect()
14+
debug('Mongoose - disconnected')
15+
} catch (error) {
16+
debug('Error during mongoose shutdown. Might occur if mongoose is not installed and is expected.')
17+
debug(error)
18+
}
19+
}
20+
21+
export default { shutdown }

lib/http.js

+13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import express from 'express'
44
import http from 'http'
55
import { createMiddleware, getSummary, getContentType } from '@promster/express'
66
import { errors } from 'celebrate'
7+
import debugFactory from 'debug'
78
import config from './config/index.js'
89

10+
const debug = debugFactory('taube-http')
11+
912
// Setup express
1013
const app = express()
1114
app.use(express.json({ limit: config.http.limit }))
@@ -84,10 +87,20 @@ function getPort() {
8487
return server.address().port
8588
}
8689

90+
function shutdown() {
91+
return new Promise((resolve) => {
92+
server.close(() => {
93+
debug('Taube: Express closed')
94+
resolve()
95+
})
96+
})
97+
}
98+
8799
export default {
88100
server,
89101
getPort,
90102
init,
91103
app,
92104
ensureErrorHandlingMiddlewareIsLast,
105+
shutdown,
93106
}

lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import QueueWorkerExponentialRetries from './components/queue-worker-exponential
1919

2020
import http from './http.js'
2121
import amqp from './amqp.js'
22+
import mongoHelper from './helpers/mongo.js'
2223

2324
// Export all components as named
2425
export { default as Joi } from 'joi'
@@ -40,9 +41,14 @@ export { default as http } from './http.js'
4041
export { default as amqp } from './amqp.js'
4142

4243
export const shutdown = async() => {
44+
await http.shutdown()
4345
await amqp.shutdown()
46+
await mongoHelper.shutdown()
4447
}
4548

49+
process.on('SIGTERM', shutdown)
50+
process.on('SIGINT', shutdown)
51+
4652
// Build a default export to make migration to ESM easy
4753
const taube = {
4854
Requester,

0 commit comments

Comments
 (0)