File tree Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -16,17 +16,29 @@ spec:
16
16
- name : training
17
17
image : training:v1
18
18
imagePullPolicy : IfNotPresent
19
+ readinessProbe :
20
+ httpGet :
21
+ path : /healthz
22
+ port : 3000
23
+ initialDelaySeconds : 2
24
+ periodSeconds : 2
25
+ failureThreshold : 2
26
+ successThreshold : 1 # default
27
+ timeoutSeconds : 1 # default
19
28
livenessProbe :
20
29
httpGet :
21
30
path : /healthz
22
31
port : 3000
23
- initialDelaySeconds : 30
24
- timeoutSeconds : 1
32
+ initialDelaySeconds : 5
33
+ periodSeconds : 10
34
+ failureThreshold : 3 # default
35
+ successThreshold : 1 # default
36
+ timeoutSeconds : 1 # default
25
37
ports :
26
38
- containerPort : 3000
27
39
env :
28
40
- name : SHOULD_FAIL_WITHIN
29
- value : " 5000 "
41
+ value : " 300000 "
30
42
- name : REDIS_URI
31
43
valueFrom :
32
44
secretKeyRef :
Original file line number Diff line number Diff line change 1
1
const express = require ( 'express' )
2
2
const app = express ( )
3
3
4
- let healthy = true
5
-
4
+ // READINESS_PROBE_DELAY = failureThreshold * periodSeconds * 1000
5
+ const READINESS_PROBE_DELAY = 4000
6
+ const state = {
7
+ isShutdown : false ,
8
+ healthy : true
9
+ }
10
+ console . log ( process . pid )
6
11
if ( process . env . SHOULD_FAIL_WITHIN ) {
7
- setTimeout ( ( ) => { healthy = false } , process . env . SHOULD_FAIL_WITHIN )
12
+ setTimeout ( ( ) => { state . healthy = false } , process . env . SHOULD_FAIL_WITHIN )
8
13
}
9
14
15
+ process . on ( 'SIGTERM' , ( ) => {
16
+ state . isShutdown = true
17
+ setTimeout ( function gracefulShutdown ( ) {
18
+ server . close ( )
19
+ // db close
20
+ process . exit ( )
21
+ } , READINESS_PROBE_DELAY )
22
+ } )
23
+
10
24
app . get ( '/' , ( req , res ) => res . send ( 'Hello World!' ) )
11
25
app . get ( '/healthz' , ( req , res ) => {
12
- if ( healthy ) {
26
+ if ( state . isShutdown ) {
27
+ return res . send ( 503 , 'Service is shutting down' )
28
+ }
29
+
30
+ if ( state . healthy ) {
13
31
return res . sendStatus ( 200 )
14
32
} else {
15
33
return res . sendStatus ( 500 )
16
34
}
17
35
} )
18
36
19
- app . listen ( 3000 , ( ) => console . log ( 'Example app listening on port 3000!' ) )
37
+ const server = app . listen ( 3000 , ( ) => console . log ( 'Example app listening on port 3000!' ) )
You can’t perform that action at this time.
0 commit comments