Skip to content

Commit d0b29f3

Browse files
authored
Gracefully handle TERM signal in proxy and other sidecar containers (#2402)
1 parent 83e083a commit d0b29f3

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

cmd/activator/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os/signal"
2525
"strconv"
2626
"strings"
27+
"syscall"
2728
"time"
2829

2930
"github.com/cortexlabs/cortex/pkg/activator"
@@ -167,14 +168,13 @@ func main() {
167168
}
168169

169170
sigint := make(chan os.Signal, 1)
170-
signal.Notify(sigint, os.Interrupt)
171+
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
171172

172173
select {
173174
case err = <-errCh:
174175
exit(log, err, "failed to start activator server")
175176
case <-sigint:
176-
// We received an interrupt signal, shut down.
177-
log.Info("Received TERM signal, handling a graceful shutdown...")
177+
log.Info("Received INT or TERM signal, handling a graceful shutdown...")
178178

179179
for name, server := range servers {
180180
log.Infof("Shutting down %s server", name)

cmd/autoscaler/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os/signal"
2626
"strconv"
2727
"strings"
28+
"syscall"
2829
"time"
2930

3031
"github.com/cortexlabs/cortex/pkg/autoscaler"
@@ -225,14 +226,13 @@ func main() {
225226
}()
226227

227228
sigint := make(chan os.Signal, 1)
228-
signal.Notify(sigint, os.Interrupt)
229+
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
229230

230231
select {
231232
case err = <-errCh:
232233
exit(log, err, "failed to start autoscaler server")
233234
case <-sigint:
234-
// We received an interrupt signal, shut down.
235-
log.Info("Received TERM signal, handling a graceful shutdown...")
235+
log.Info("Received INT or TERM signal, handling a graceful shutdown...")
236236
log.Info("Shutting down server")
237237
if err = server.Shutdown(context.Background()); err != nil {
238238
// Error from closing listeners, or context timeout:

cmd/dequeuer/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"os/signal"
2525
"strconv"
26+
"syscall"
2627

2728
"github.com/DataDog/datadog-go/statsd"
2829
"github.com/cortexlabs/cortex/pkg/consts"
@@ -210,7 +211,7 @@ func main() {
210211
}()
211212

212213
sigint := make(chan os.Signal, 1)
213-
signal.Notify(sigint, os.Interrupt)
214+
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
214215

215216
sqsDequeuer, err := dequeuer.NewSQSDequeuer(dequeuerConfig, awsClient, log)
216217
if err != nil {
@@ -239,7 +240,7 @@ func main() {
239240
case err = <-errCh:
240241
exit(log, err, "error during message dequeueing or error from admin server")
241242
case <-sigint:
242-
log.Info("Received TERM signal, handling a graceful shutdown...")
243+
log.Info("Received INT or TERM signal, handling a graceful shutdown...")
243244
sqsDequeuer.Shutdown()
244245
log.Info("Shutdown complete, exiting...")
245246
}

cmd/proxy/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/signal"
2727
"strconv"
28+
"syscall"
2829
"time"
2930

3031
"github.com/cortexlabs/cortex/pkg/lib/aws"
@@ -167,14 +168,13 @@ func main() {
167168
}
168169

169170
sigint := make(chan os.Signal, 1)
170-
signal.Notify(sigint, os.Interrupt)
171+
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
171172

172173
select {
173174
case err = <-errCh:
174175
exit(log, errors.Wrap(err, "failed to start proxy server"))
175176
case <-sigint:
176-
// We received an interrupt signal, shut down.
177-
log.Info("Received TERM signal, handling a graceful shutdown...")
177+
log.Info("Received INT or TERM signal, handling a graceful shutdown...")
178178

179179
for name, server := range servers {
180180
log.Infof("Shutting down %s server", name)

0 commit comments

Comments
 (0)