server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 example1
  5. import (
  6. "fmt"
  7. "github.com/SongZihuan/BackendServerTemplate/src/global"
  8. "github.com/SongZihuan/BackendServerTemplate/src/logger"
  9. "github.com/SongZihuan/BackendServerTemplate/src/server/servercontext"
  10. "github.com/SongZihuan/BackendServerTemplate/src/server/serverinterface"
  11. "github.com/SongZihuan/BackendServerTemplate/src/utils/strconvutils"
  12. "sync"
  13. "time"
  14. )
  15. type ServerExample1 struct {
  16. running bool
  17. ctx *servercontext.ServerContext
  18. name string
  19. wg *sync.WaitGroup
  20. stopWaitTime time.Duration
  21. }
  22. type ServerExample1Option struct {
  23. StopWaitTime time.Duration
  24. }
  25. func NewServerExample1(opt *ServerExample1Option) (*ServerExample1, *servercontext.ServerContext, error) {
  26. ctx := servercontext.NewServerContext()
  27. if opt == nil {
  28. opt = &ServerExample1Option{
  29. StopWaitTime: 10 * time.Second,
  30. }
  31. } else {
  32. if opt.StopWaitTime == 0 {
  33. opt.StopWaitTime = 10 * time.Second
  34. }
  35. }
  36. server := &ServerExample1{
  37. ctx: ctx,
  38. running: false,
  39. name: "example1",
  40. wg: new(sync.WaitGroup),
  41. stopWaitTime: opt.StopWaitTime,
  42. }
  43. err := server.init()
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. return server, ctx, nil
  48. }
  49. func (s *ServerExample1) init() error {
  50. return nil
  51. }
  52. func (s *ServerExample1) Name() string {
  53. return s.name
  54. }
  55. func (s *ServerExample1) GetCtx() *servercontext.ServerContext {
  56. return s.ctx
  57. }
  58. func (s *ServerExample1) Run() {
  59. s.running = true
  60. defer func() {
  61. s.running = false
  62. }()
  63. s.wg = new(sync.WaitGroup)
  64. s.wg.Add(1)
  65. defer s.wg.Done()
  66. MainCycle:
  67. for {
  68. //if global.GitTag == "" || global.GitTagCommitHash == "" {
  69. // fmt.Printf("Example1: I am running! BuildDate: '%s' Commit: '%s' Version: '%s' Now: '%s'\n", global.BuildTime.Format(time.DateTime), global.GitCommitHash, global.Version, time.Now().Format(time.DateTime))
  70. //} else {
  71. fmt.Printf("Example1: I am running! BuildDate: '%s' Commit: '%s' Tag: '%s' Tag Commit: '%s' Version: '%s' Now: '%s'\n", global.BuildTime.Format(time.DateTime), global.GitCommitHash, global.GitTag, global.GitTagCommitHash, global.Version, time.Now().Format(time.DateTime))
  72. //}
  73. select {
  74. case <-s.ctx.Listen():
  75. fmt.Println("Example1: I am stop!")
  76. break MainCycle
  77. case <-time.After(1 * time.Second):
  78. continue
  79. }
  80. }
  81. }
  82. func (s *ServerExample1) Stop() {
  83. s.ctx.StopTask()
  84. if s.wg != nil {
  85. wgchan := make(chan any)
  86. go func() {
  87. s.wg.Wait()
  88. close(wgchan)
  89. }()
  90. select {
  91. case <-time.After(s.stopWaitTime):
  92. logger.Errorf("%s - 退出清理超时... (%s)", s.name, strconvutils.TimeDurationToString(s.stopWaitTime))
  93. case <-wgchan:
  94. // pass
  95. }
  96. }
  97. }
  98. func (s *ServerExample1) IsRunning() bool {
  99. return s.running
  100. }
  101. func _test() {
  102. var a serverinterface.Server
  103. var b *ServerExample1
  104. a = b
  105. _ = a
  106. }