refactor: utils.WaitExit

This commit is contained in:
yusing 2025-03-28 05:59:04 +08:00
parent 40aa937f54
commit fd223c7542
2 changed files with 18 additions and 11 deletions

View file

@ -138,17 +138,7 @@ func main() {
config.WatchChanges()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT)
signal.Notify(sig, syscall.SIGTERM)
signal.Notify(sig, syscall.SIGHUP)
// wait for signal
<-sig
// gracefully shutdown
logging.Info().Msg("shutting down")
_ = task.GracefulShutdown(time.Second * time.Duration(cfg.Value().TimeoutShutdown))
task.WaitExit(cfg.Value().TimeoutShutdown)
}
func prepareDirectory(dir string) {

View file

@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/yusing/go-proxy/internal/logging"
@ -73,3 +76,17 @@ func GracefulShutdown(timeout time.Duration) (err error) {
}
}
}
func WaitExit(shutdownTimeout int) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT)
signal.Notify(sig, syscall.SIGTERM)
signal.Notify(sig, syscall.SIGHUP)
// wait for signal
<-sig
// gracefully shutdown
logging.Info().Msg("shutting down")
_ = GracefulShutdown(time.Second * time.Duration(shutdownTimeout))
}