2022-06-12 02:32:06 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
|
|
|
)
|
|
|
|
|
2024-08-14 15:37:05 +02:00
|
|
|
const interruptSignalsCount = 3
|
|
|
|
|
2022-06-12 02:32:06 +02:00
|
|
|
// NOTE(@andreynering): This function intercepts SIGINT and SIGTERM signals
|
2022-08-17 19:37:58 +02:00
|
|
|
// so the Task process is not killed immediately and processes running have
|
2022-06-12 02:32:06 +02:00
|
|
|
// time to do cleanup work.
|
|
|
|
func (e *Executor) InterceptInterruptSignals() {
|
2024-08-14 15:37:05 +02:00
|
|
|
ch := make(chan os.Signal, interruptSignalsCount)
|
2022-06-12 02:32:06 +02:00
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
2024-08-14 15:37:05 +02:00
|
|
|
for i := range interruptSignalsCount {
|
2022-06-12 02:32:06 +02:00
|
|
|
sig := <-ch
|
|
|
|
|
2024-08-14 15:37:05 +02:00
|
|
|
if i+1 >= interruptSignalsCount {
|
|
|
|
e.Logger.Errf(logger.Red, "task: Signal received for the third time: %q. Forcing shutdown\n", sig)
|
|
|
|
os.Exit(1)
|
2022-06-12 02:32:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-14 15:37:05 +02:00
|
|
|
e.Logger.Outf(logger.Yellow, "task: Signal received: %q\n", sig)
|
2022-06-12 02:32:06 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|