Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shutdown.go 841B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package bootstraps
  2. import (
  3. "os"
  4. "os/signal"
  5. "syscall"
  6. "time"
  7. "git.x2erp.com/qdy/go-base/logger"
  8. "git.x2erp.com/qdy/go-db/factory/database"
  9. )
  10. // setupGracefulShutdown 设置优雅关闭
  11. func setupGracefulShutdown(dbFactory *database.DBFactory, cleanupFuncs ...func()) {
  12. signalCh := make(chan os.Signal, 1)
  13. signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
  14. go func() {
  15. <-signalCh
  16. logger.Info("\n接收到关闭信号,正在优雅关闭...")
  17. // 关闭数据库连接
  18. if dbFactory != nil {
  19. if err := dbFactory.Close(); err != nil {
  20. logger.Error("关闭数据库错误: %v", err)
  21. }
  22. logger.Info("数据库连接已关闭")
  23. }
  24. // 执行其他清理函数
  25. for _, cleanup := range cleanupFuncs {
  26. cleanup()
  27. }
  28. // 等待日志写入完成
  29. time.Sleep(100 * time.Millisecond)
  30. os.Exit(0)
  31. }()
  32. }