3
3
4
4
// Reference: https://github.com/BretFisher/node-docker-good-defaults/blob/master/bin/www
5
5
6
- import app from " ../app" ;
6
+ import app from ' ../app' ;
7
7
8
8
const port = process . env . PORT || 3000 ;
9
9
10
10
/** Listen on provided port, on all network interfaces. */
11
- const server = app . listen ( port , ( ) =>
12
- console . log ( `Node app is running on port ${ port } ` )
13
- ) ;
11
+ const server = app . listen ( port , ( ) => console . log ( `Node app is running on port ${ port } ` ) ) ;
14
12
15
13
// need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
16
14
// this also won't work on using npm start since:
@@ -22,30 +20,24 @@ const server = app.listen(port, () =>
22
20
//
23
21
24
22
// quit on ctrl-c when running docker in terminal
25
- process . on ( "SIGINT" , ( ) => {
26
- console . info (
27
- "Got SIGINT (aka ctrl-c in docker). Graceful shutdown " ,
28
- new Date ( ) . toISOString ( )
29
- ) ;
23
+ process . on ( 'SIGINT' , ( ) => {
24
+ console . info ( 'Got SIGINT (aka ctrl-c in docker). Graceful shutdown ' , new Date ( ) . toISOString ( ) ) ;
30
25
shutdown ( ) ;
31
26
} ) ;
32
27
33
28
// quit properly on docker stop
34
- process . on ( "SIGTERM" , ( ) => {
35
- console . info (
36
- "Got SIGTERM (docker container stop). Graceful shutdown " ,
37
- new Date ( ) . toISOString ( )
38
- ) ;
29
+ process . on ( 'SIGTERM' , ( ) => {
30
+ console . info ( 'Got SIGTERM (docker container stop). Graceful shutdown ' , new Date ( ) . toISOString ( ) ) ;
39
31
shutdown ( ) ;
40
32
} ) ;
41
33
42
34
const sockets = { } ;
43
35
let nextSocketId = 0 ;
44
- server . on ( " connection" , socket => {
36
+ server . on ( ' connection' , ( socket ) => {
45
37
const socketId = nextSocketId ++ ;
46
38
sockets [ socketId ] = socket ;
47
39
48
- socket . once ( " close" , ( ) => {
40
+ socket . once ( ' close' , ( ) => {
49
41
delete sockets [ socketId ] ;
50
42
} ) ;
51
43
} ) ;
@@ -54,7 +46,7 @@ server.on("connection", socket => {
54
46
function shutdown ( ) {
55
47
waitForSocketsToClose ( 10 ) ;
56
48
57
- server . close ( err => {
49
+ server . close ( ( err ) => {
58
50
if ( err ) {
59
51
console . error ( err ) ;
60
52
process . exitCode = 1 ;
@@ -67,14 +59,14 @@ function waitForSocketsToClose(counter) {
67
59
if ( counter > 0 ) {
68
60
console . log (
69
61
`Waiting ${ counter } more ${
70
- counter === 1 ? " seconds" : " second"
62
+ counter === 1 ? ' seconds' : ' second'
71
63
} for all connections to close...`
72
64
) ;
73
65
return setTimeout ( waitForSocketsToClose , 1000 , counter - 1 ) ;
74
66
}
75
67
76
- console . log ( " Forcing all connections to close now" ) ;
77
- Object . keys ( sockets ) . forEach ( socketId => {
68
+ console . log ( ' Forcing all connections to close now' ) ;
69
+ Object . keys ( sockets ) . forEach ( ( socketId ) => {
78
70
sockets [ socketId ] . destroy ( ) ;
79
71
} ) ;
80
72
}
0 commit comments