File tree 9 files changed +7378
-1019
lines changed
9 files changed +7378
-1019
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,6 @@ variables:
20
20
RABBITMQ_DEFAULT_PASS : " guest"
21
21
ESLINT_CODE_QUALITY_REPORT : gl-codequality.json
22
22
23
- services :
24
- - r.unueng.com/cloud/rabbitmq-plugins:3.9-1
25
23
26
24
setup :
27
25
stage : build
@@ -39,12 +37,18 @@ test:
39
37
script :
40
38
# Wait for RabbitMQ to be running
41
39
- while ! nc -z localhost 15692 ; do sleep 1 ; done
40
+ - npm run test-graceful-shutdown
42
41
- npm run test
43
42
- npm run test-coverage
44
43
cache :
45
44
policy : pull
46
45
paths :
47
46
- 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
48
52
coverage : ' /Statements.*?(\d+(?:\.\d+)?)%/'
49
53
artifacts :
50
54
reports :
Original file line number Diff line number Diff line change @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
The format is based on [ Keep a Changelog] ( https://keepachangelog.com/en/1.0.0/ ) ,
5
5
and this project adheres to [ Semantic Versioning] ( https://semver.org/spec/v2.0.0.html ) .
6
6
7
+ # [ 5.3.0] - 2023-04-26
8
+
9
+ - Add graceful shutdown for Mongoose, Express and AMQP. Mongoose is optional
10
+
7
11
# [ 5.2.0] - 2023-04-18
8
12
9
13
- Update dependency ` got ` to latest version
Original file line number Diff line number Diff line change
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
1
2
import mongoose from 'mongoose'
2
3
3
4
// https://github.com/aravindnc/mongoose-paginate-v2
Original file line number Diff line number Diff line change
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 }
Original file line number Diff line number Diff line change @@ -4,8 +4,11 @@ import express from 'express'
4
4
import http from 'http'
5
5
import { createMiddleware , getSummary , getContentType } from '@promster/express'
6
6
import { errors } from 'celebrate'
7
+ import debugFactory from 'debug'
7
8
import config from './config/index.js'
8
9
10
+ const debug = debugFactory ( 'taube-http' )
11
+
9
12
// Setup express
10
13
const app = express ( )
11
14
app . use ( express . json ( { limit : config . http . limit } ) )
@@ -84,10 +87,20 @@ function getPort() {
84
87
return server . address ( ) . port
85
88
}
86
89
90
+ function shutdown ( ) {
91
+ return new Promise ( ( resolve ) => {
92
+ server . close ( ( ) => {
93
+ debug ( 'Taube: Express closed' )
94
+ resolve ( )
95
+ } )
96
+ } )
97
+ }
98
+
87
99
export default {
88
100
server,
89
101
getPort,
90
102
init,
91
103
app,
92
104
ensureErrorHandlingMiddlewareIsLast,
105
+ shutdown,
93
106
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import QueueWorkerExponentialRetries from './components/queue-worker-exponential
19
19
20
20
import http from './http.js'
21
21
import amqp from './amqp.js'
22
+ import mongoHelper from './helpers/mongo.js'
22
23
23
24
// Export all components as named
24
25
export { default as Joi } from 'joi'
@@ -40,9 +41,14 @@ export { default as http } from './http.js'
40
41
export { default as amqp } from './amqp.js'
41
42
42
43
export const shutdown = async ( ) => {
44
+ await http . shutdown ( )
43
45
await amqp . shutdown ( )
46
+ await mongoHelper . shutdown ( )
44
47
}
45
48
49
+ process . on ( 'SIGTERM' , shutdown )
50
+ process . on ( 'SIGINT' , shutdown )
51
+
46
52
// Build a default export to make migration to ESM easy
47
53
const taube = {
48
54
Requester,
You can’t perform that action at this time.
0 commit comments