main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package v1
  5. import (
  6. "errors"
  7. "fmt"
  8. "github.com/SongZihuan/BackendServerTemplate/src/config"
  9. "github.com/SongZihuan/BackendServerTemplate/src/consolewatcher"
  10. "github.com/SongZihuan/BackendServerTemplate/src/logger"
  11. "github.com/SongZihuan/BackendServerTemplate/src/restart"
  12. "github.com/SongZihuan/BackendServerTemplate/src/server/example1"
  13. "github.com/SongZihuan/BackendServerTemplate/src/server/servercontext"
  14. "github.com/SongZihuan/BackendServerTemplate/src/signalwatcher"
  15. "github.com/SongZihuan/BackendServerTemplate/src/utils/exitutils"
  16. "github.com/spf13/cobra"
  17. )
  18. func MainV1(cmd *cobra.Command, args []string, inputConfigFilePath string, ppid int) (exitCode error) {
  19. var err error
  20. err = config.InitConfig(&config.ConfigOption{
  21. ConfigFilePath: inputConfigFilePath,
  22. })
  23. if err != nil {
  24. return exitutils.InitFailed("Config file read and parser", err.Error())
  25. }
  26. sigchan := signalwatcher.NewSignalExitChannel()
  27. consolechan, consolewaitexitchan, err := consolewatcher.NewWin32ConsoleExitChannel()
  28. if err != nil {
  29. return exitutils.InitFailed("Win32 console channel", err.Error())
  30. }
  31. ppidchan := restart.PpidWatcher(ppid)
  32. ser, _, err := example1.NewServerExample1(&example1.ServerExample1Option{
  33. StopWaitTime: config.Data().Server.StopWaitTimeDuration,
  34. })
  35. if err != nil {
  36. return exitutils.InitFailed("Server Example1", err.Error())
  37. }
  38. logger.Infof("Start to run server example 1")
  39. go ser.Run()
  40. var stopErr error
  41. select {
  42. case <-restart.RestartChan:
  43. if ppid != 0 {
  44. logger.Warnf("stop to restart")
  45. err = nil
  46. stopErr = nil
  47. } else {
  48. logger.Warnf("stop to restart (error: restart not set)")
  49. err = fmt.Errorf("stop by restart, but restart not set")
  50. stopErr = err
  51. }
  52. case <-ppidchan:
  53. if ppid != 0 {
  54. logger.Warnf("stop by parent process")
  55. err = nil
  56. stopErr = nil
  57. } else {
  58. logger.Warnf("stop by parent process (error: ppid not set)")
  59. err = fmt.Errorf("stop by parent process, but pppid not set")
  60. stopErr = err
  61. }
  62. case sig := <-sigchan:
  63. logger.Warnf("stop by signal (%s)", sig.String())
  64. err = nil
  65. stopErr = nil
  66. case event := <-consolechan:
  67. logger.Infof("stop by console event (%s)", event.String())
  68. err = nil
  69. stopErr = nil
  70. case <-ser.GetCtx().Listen():
  71. err = ser.GetCtx().Error()
  72. if err == nil || errors.Is(err, servercontext.StopAllTask) {
  73. logger.Infof("stop by server")
  74. err = nil
  75. stopErr = nil
  76. } else {
  77. logger.Errorf("stop by server with error: %s", err.Error())
  78. stopErr = err
  79. }
  80. }
  81. ser.Stop()
  82. close(consolewaitexitchan)
  83. if stopErr != nil {
  84. return exitutils.RunError(stopErr.Error())
  85. }
  86. select {
  87. case <-restart.RestartChan:
  88. return exitutils.SuccessExit("all tasks are completed and the main go routine exits", exitutils.ExitCodeReload)
  89. default:
  90. return exitutils.SuccessExit("all tasks are completed and the main go routine exits")
  91. }
  92. }