1
0
mirror of https://github.com/go-task/task.git synced 2025-01-08 04:04:08 +02:00

Refactor signal handling to improve clarity and correctness

- Renamed interruptSignalsCount to maxInterruptSignals for better readability.
- Fixed the loop condition to correctly iterate from 0 to maxInterruptSignals.
This commit is contained in:
EinoPlasma 2024-12-31 13:04:58 +08:00
parent dc2eceb634
commit 6be393e064

View File

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