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