...

Source file src/edge-infra.dev/pkg/lib/runtime/manager/signals/signal.go

Documentation: edge-infra.dev/pkg/lib/runtime/manager/signals

     1  package signals
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"os/signal"
     7  )
     8  
     9  var onlyOneSignalHandler = make(chan struct{})
    10  
    11  // SetupSignalHandler registers for SIGTERM and SIGINT. A context is returned
    12  // which is canceled on one of these signals. If a second signal is caught, the program
    13  // is terminated with exit code 1.
    14  func SetupSignalHandler() context.Context {
    15  	close(onlyOneSignalHandler) // panics when called twice
    16  
    17  	ctx, cancel := context.WithCancel(context.Background())
    18  
    19  	c := make(chan os.Signal, 2)
    20  	signal.Notify(c, shutdownSignals...)
    21  	go func() {
    22  		<-c
    23  		cancel()
    24  		<-c
    25  		os.Exit(1) // second signal. Exit directly.
    26  	}()
    27  
    28  	return ctx
    29  }
    30  

View as plain text