@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "crypto/tls"
2222 "crypto/x509"
23+ "errors"
2324 "fmt"
2425 "net"
2526 "net/http"
@@ -54,6 +55,8 @@ const (
5455 // DefaultMaxHeaderBytes defines the default max size of request headers in bytes
5556 // 1 MB
5657 DefaultMaxHeaderBytes = 1 << 20
58+ // DefaultGracefulShutdownTimeout defines the default timeout for graceful shutdown http server
59+ DefaultGracefulShutdownTimeout = 30 * time .Second
5760)
5861
5962// JobKind creates job GroupVersionKind.
@@ -72,6 +75,8 @@ var JobFlowKind = flow.SchemeGroupVersion.WithKind("JobFlow")
7275// JobTemplateKind creates jobtemplate GroupVersionKind.
7376var JobTemplateKind = flow .SchemeGroupVersion .WithKind ("JobTemplate" )
7477
78+ var shutdownSignals = []os.Signal {os .Interrupt , syscall .SIGTERM , syscall .SIGINT }
79+
7580// CreateOrUpdateConfigMap creates config map if not present or updates config map if necessary.
7681func CreateOrUpdateConfigMap (job * vcbatch.Job , kubeClients kubernetes.Interface , data map [string ]string , cmName string ) error {
7782 // If ConfigMap does not exist, create one for Job.
@@ -249,14 +254,24 @@ func runServer(server *http.Server, ln net.Listener) error {
249254 return fmt .Errorf ("listener and server must not be nil" )
250255 }
251256
252- stopCh := make (chan os.Signal , 2 )
253- signal .Notify (stopCh , syscall .SIGTERM , syscall .SIGINT )
257+ stopCh := make (chan struct {})
258+ shutdownHandler := make (chan os.Signal , 2 )
259+ signal .Notify (shutdownHandler , shutdownSignals ... )
260+ go func () {
261+ <- shutdownHandler
262+ close (stopCh )
263+ <- shutdownHandler
264+ os .Exit (1 ) // second signal. Exit directly.
265+ }()
254266
255267 go func () {
256268 <- stopCh
257- ctx , cancel := context .WithTimeout (context .Background (), 0 )
258- server .Shutdown (ctx )
259- cancel ()
269+ ctx , cancel := context .WithTimeout (context .Background (), DefaultGracefulShutdownTimeout )
270+ defer cancel ()
271+ if err := server .Shutdown (ctx ); err != nil {
272+ klog .Fatal ("Server Shutdown:" , err )
273+ }
274+ klog .Info ("Server exiting" )
260275 }()
261276
262277 go func () {
@@ -275,7 +290,10 @@ func runServer(server *http.Server, ln net.Listener) error {
275290 case <- stopCh :
276291 klog .Info (msg )
277292 default :
278- klog .Fatalf ("%s due to error: %v" , msg , err )
293+ if ! errors .Is (err , http .ErrServerClosed ) {
294+ klog .Fatalf ("%s due to error: %v" , msg , err )
295+ }
296+ klog .Infof ("http server stopped listening on %s with err: %v" , listener .Addr ().String (), err )
279297 }
280298 }()
281299
0 commit comments