1
0
mirror of https://github.com/go-task/task.git synced 2025-11-25 22:32:55 +02:00

Fix behavior of interrupt (SIGINT, SIGTERM) signals

Task will now give time for the processes running to do cleanup work

Ref #458
Ref #479
Fixes #728

Co-authored-by: Marco Molteni <marco.molteni@pix4d.com>
Co-authored-by: aliculPix4D <aleksandar.licul_ext@pix4d.com>
This commit is contained in:
Andrey Nering
2022-06-11 21:32:06 -03:00
parent c9a582fbcc
commit 7989f73f06
9 changed files with 480 additions and 18 deletions

31
signals.go Normal file
View File

@@ -0,0 +1,31 @@
package task
import (
"os"
"os/signal"
"syscall"
"github.com/go-task/task/v3/internal/logger"
)
// NOTE(@andreynering): This function intercepts SIGINT and SIGTERM signals
// so the Task process is not killed immediatelly and processes running have
// time to do cleanup work.
func (e *Executor) InterceptInterruptSignals() {
ch := make(chan os.Signal, 3)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
for i := 1; i <= 3; i++ {
sig := <-ch
if i < 3 {
e.Logger.Outf(logger.Yellow, `task: Signal received: "%s"`, sig)
continue
}
e.Logger.Errf(logger.Red, `task: Signal received for the third time: "%s". Forcing shutdown`, sig)
os.Exit(1)
}
}()
}