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

manager_windows.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // +build windows
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler
  6. package graceful
  7. import (
  8. "context"
  9. "os"
  10. "strconv"
  11. "sync"
  12. "time"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "golang.org/x/sys/windows/svc"
  16. "golang.org/x/sys/windows/svc/debug"
  17. )
  18. var WindowsServiceName = "gitea"
  19. const (
  20. hammerCode = 128
  21. hammerCmd = svc.Cmd(hammerCode)
  22. acceptHammerCode = svc.Accepted(hammerCode)
  23. )
  24. type gracefulManager struct {
  25. ctx context.Context
  26. isChild bool
  27. lock *sync.RWMutex
  28. state state
  29. shutdown chan struct{}
  30. hammer chan struct{}
  31. terminate chan struct{}
  32. runningServerWaitGroup sync.WaitGroup
  33. createServerWaitGroup sync.WaitGroup
  34. terminateWaitGroup sync.WaitGroup
  35. }
  36. func newGracefulManager(ctx context.Context) *gracefulManager {
  37. manager := &gracefulManager{
  38. isChild: false,
  39. lock: &sync.RWMutex{},
  40. ctx: ctx,
  41. }
  42. manager.createServerWaitGroup.Add(numberOfServersToCreate)
  43. manager.Run()
  44. return manager
  45. }
  46. func (g *gracefulManager) Run() {
  47. g.setState(stateRunning)
  48. if skip, _ := strconv.ParseBool(os.Getenv("SKIP_MINWINSVC")); skip {
  49. return
  50. }
  51. run := svc.Run
  52. isInteractive, err := svc.IsAnInteractiveSession()
  53. if err != nil {
  54. log.Error("Unable to ascertain if running as an Interactive Session: %v", err)
  55. return
  56. }
  57. if isInteractive {
  58. run = debug.Run
  59. }
  60. go run(WindowsServiceName, g)
  61. }
  62. // Execute makes gracefulManager implement svc.Handler
  63. func (g *gracefulManager) Execute(args []string, changes <-chan svc.ChangeRequest, status chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) {
  64. if setting.StartupTimeout > 0 {
  65. status <- svc.Status{State: svc.StartPending}
  66. } else {
  67. status <- svc.Status{State: svc.StartPending, WaitHint: uint32(setting.StartupTimeout / time.Millisecond)}
  68. }
  69. // Now need to wait for everything to start...
  70. if !g.awaitServer(setting.StartupTimeout) {
  71. return false, 1
  72. }
  73. // We need to implement some way of svc.AcceptParamChange/svc.ParamChange
  74. status <- svc.Status{
  75. State: svc.Running,
  76. Accepts: svc.AcceptStop | svc.AcceptShutdown | acceptHammerCode,
  77. }
  78. waitTime := 30 * time.Second
  79. loop:
  80. for {
  81. select {
  82. case <-g.ctx.Done():
  83. g.doShutdown()
  84. waitTime += setting.GracefulHammerTime
  85. break loop
  86. case change := <-changes:
  87. switch change.Cmd {
  88. case svc.Interrogate:
  89. status <- change.CurrentStatus
  90. case svc.Stop, svc.Shutdown:
  91. g.doShutdown()
  92. waitTime += setting.GracefulHammerTime
  93. break loop
  94. case hammerCode:
  95. g.doShutdown()
  96. g.doHammerTime(0 * time.Second)
  97. break loop
  98. default:
  99. log.Debug("Unexpected control request: %v", change.Cmd)
  100. }
  101. }
  102. }
  103. status <- svc.Status{
  104. State: svc.StopPending,
  105. WaitHint: uint32(waitTime / time.Millisecond),
  106. }
  107. hammerLoop:
  108. for {
  109. select {
  110. case change := <-changes:
  111. switch change.Cmd {
  112. case svc.Interrogate:
  113. status <- change.CurrentStatus
  114. case svc.Stop, svc.Shutdown, hammerCmd:
  115. g.doHammerTime(0 * time.Second)
  116. break hammerLoop
  117. default:
  118. log.Debug("Unexpected control request: %v", change.Cmd)
  119. }
  120. case <-g.hammer:
  121. break hammerLoop
  122. }
  123. }
  124. return false, 0
  125. }
  126. func (g *gracefulManager) RegisterServer() {
  127. g.runningServerWaitGroup.Add(1)
  128. }
  129. func (g *gracefulManager) awaitServer(limit time.Duration) bool {
  130. c := make(chan struct{})
  131. go func() {
  132. defer close(c)
  133. g.createServerWaitGroup.Wait()
  134. }()
  135. if limit > 0 {
  136. select {
  137. case <-c:
  138. return true // completed normally
  139. case <-time.After(limit):
  140. return false // timed out
  141. case <-g.IsShutdown():
  142. return false
  143. }
  144. } else {
  145. select {
  146. case <-c:
  147. return true // completed normally
  148. case <-g.IsShutdown():
  149. return false
  150. }
  151. }
  152. }