From 1f7a225a68cdd289a346621526fa3353a93e05cd Mon Sep 17 00:00:00 2001 From: Danylo Kuvshynov Date: Mon, 19 Apr 2021 15:20:38 +0300 Subject: [PATCH] Correct handling of stop signal for pod termination --- cmd/seleniferous/main.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/seleniferous/main.go b/cmd/seleniferous/main.go index b68e71f..f9cb211 100644 --- a/cmd/seleniferous/main.go +++ b/cmd/seleniferous/main.go @@ -93,11 +93,18 @@ func command() *cobra.Command { Handler: router, } - stop := make(chan os.Signal) + stop := make(chan os.Signal, 1) signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) e := make(chan error) + cancelFunc := func() { + context := context.Background() + client.CoreV1().Pods(namespace).Delete(context, hostname, metav1.DeleteOptions{ + GracePeriodSeconds: pointer.Int64Ptr(15), + }) + } + go func() { e <- srv.ListenAndServe() }() @@ -109,11 +116,9 @@ func command() *cobra.Command { for { select { case <-timeout: - context := context.Background() - client.CoreV1().Pods(namespace).Delete(context, hostname, metav1.DeleteOptions{ - GracePeriodSeconds: pointer.Int64Ptr(15), - }) + shuttingDown = true logger.Warn("session wait timeout exceeded") + cancelFunc() break loop case <-ticker: if storage.IsEmpty() { @@ -128,7 +133,10 @@ func command() *cobra.Command { case err := <-e: logger.Fatalf("failed to start: %v", err) case <-stop: - shuttingDown = true + if !shuttingDown { + logger.Warn("unexpected stop signal received") + defer cancelFunc() + } logger.Warn("stopping seleniferous") }