1
0

signals.go 777 B

1234567891011121314151617181920212223242526272829303132
  1. package task
  2. import (
  3. "os"
  4. "os/signal"
  5. "syscall"
  6. "github.com/go-task/task/v3/internal/logger"
  7. )
  8. const interruptSignalsCount = 3
  9. // NOTE(@andreynering): This function intercepts SIGINT and SIGTERM signals
  10. // so the Task process is not killed immediately and processes running have
  11. // time to do cleanup work.
  12. func (e *Executor) InterceptInterruptSignals() {
  13. ch := make(chan os.Signal, interruptSignalsCount)
  14. signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
  15. go func() {
  16. for i := range interruptSignalsCount {
  17. sig := <-ch
  18. if i+1 >= interruptSignalsCount {
  19. e.Logger.Errf(logger.Red, "task: Signal received for the third time: %q. Forcing shutdown\n", sig)
  20. os.Exit(1)
  21. }
  22. e.Logger.Outf(logger.Yellow, "task: Signal received: %q\n", sig)
  23. }
  24. }()
  25. }