// TrapSignals tells the process to trap incoming process signals. func TrapSignals(ctx apitypes.Context) { context.RegisterCustomKey(signalContextKey, context.CustomLoggerKey) sigc := make(chan os.Signal, 1) signal.Notify(sigc) go func() { for s := range sigc { ctx := ctx.WithValue(signalContextKey, s.String()) if ok, graceful := IsExitSignal(s); ok && !graceful { ctx.Error("received signal; aborting") os.Exit(1) } func() { sigHandlersRWL.RLock() defer sigHandlersRWL.RUnlock() // execute the signal handlers in reverse order. the first // one registered should be executed last as it was registered // the earliest for i := len(sigHandlers) - 1; i >= 0; i-- { sigHandlers[i](ctx, s) } }() if ok, graceful := IsExitSignal(s); ok && graceful { ctx.Error("received signal; shutting down") os.Exit(0) } } }() }
func init() { context.RegisterCustomKey(transactionHeaderKey, context.CustomHeaderKey) context.RegisterCustomKey(instanceIDHeaderKey, context.CustomHeaderKey) context.RegisterCustomKey(localDevicesHeaderKey, context.CustomHeaderKey) }