signals.go 490 B

123456789101112131415161718192021222324
  1. package redisqueue
  2. import (
  3. "os"
  4. "os/signal"
  5. "syscall"
  6. )
  7. // newSignalHandler registered for SIGTERM and SIGINT. A stop channel is
  8. // returned which is closed on one of these signals. If a second signal is
  9. // caught, the program is terminated with exit code 1.
  10. func newSignalHandler() <-chan struct{} {
  11. stop := make(chan struct{})
  12. c := make(chan os.Signal, 2)
  13. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  14. go func() {
  15. <-c
  16. close(stop)
  17. <-c
  18. os.Exit(1)
  19. }()
  20. return stop
  21. }