2018-04-18 19:36:26 -04:00
|
|
|
package appcontext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var appContextCache context.Context
|
|
|
|
var appContextOnce sync.Once
|
|
|
|
|
|
|
|
// Context returns a static context that reacts to termination signals of the
|
|
|
|
// running process. Useful in CLI tools.
|
|
|
|
func Context() context.Context {
|
|
|
|
appContextOnce.Do(func() {
|
|
|
|
signals := make(chan os.Signal, 2048)
|
|
|
|
signal.Notify(signals, terminationSignals...)
|
|
|
|
|
|
|
|
const exitLimit = 3
|
|
|
|
retries := 0
|
|
|
|
|
2022-03-24 08:40:22 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
for _, f := range inits {
|
|
|
|
ctx = f(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-04-18 19:36:26 -04:00
|
|
|
appContextCache = ctx
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-signals
|
|
|
|
cancel()
|
|
|
|
retries++
|
|
|
|
if retries >= exitLimit {
|
|
|
|
logrus.Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
return appContextCache
|
|
|
|
}
|