Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

manager_unix.go 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. //go:build !windows
  4. package graceful
  5. import (
  6. "context"
  7. "errors"
  8. "os"
  9. "os/signal"
  10. "runtime/pprof"
  11. "strconv"
  12. "sync"
  13. "syscall"
  14. "time"
  15. "code.gitea.io/gitea/modules/graceful/releasereopen"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/process"
  18. "code.gitea.io/gitea/modules/setting"
  19. )
  20. // Manager manages the graceful shutdown process
  21. type Manager struct {
  22. isChild bool
  23. forked bool
  24. lock *sync.RWMutex
  25. state state
  26. shutdownCtx context.Context
  27. hammerCtx context.Context
  28. terminateCtx context.Context
  29. managerCtx context.Context
  30. shutdownCtxCancel context.CancelFunc
  31. hammerCtxCancel context.CancelFunc
  32. terminateCtxCancel context.CancelFunc
  33. managerCtxCancel context.CancelFunc
  34. runningServerWaitGroup sync.WaitGroup
  35. createServerWaitGroup sync.WaitGroup
  36. terminateWaitGroup sync.WaitGroup
  37. toRunAtShutdown []func()
  38. toRunAtTerminate []func()
  39. }
  40. func newGracefulManager(ctx context.Context) *Manager {
  41. manager := &Manager{
  42. isChild: len(os.Getenv(listenFDsEnv)) > 0 && os.Getppid() > 1,
  43. lock: &sync.RWMutex{},
  44. }
  45. manager.createServerWaitGroup.Add(numberOfServersToCreate)
  46. manager.start(ctx)
  47. return manager
  48. }
  49. type systemdNotifyMsg string
  50. const (
  51. readyMsg systemdNotifyMsg = "READY=1"
  52. stoppingMsg systemdNotifyMsg = "STOPPING=1"
  53. reloadingMsg systemdNotifyMsg = "RELOADING=1"
  54. watchdogMsg systemdNotifyMsg = "WATCHDOG=1"
  55. )
  56. func statusMsg(msg string) systemdNotifyMsg {
  57. return systemdNotifyMsg("STATUS=" + msg)
  58. }
  59. func pidMsg() systemdNotifyMsg {
  60. return systemdNotifyMsg("MAINPID=" + strconv.Itoa(os.Getpid()))
  61. }
  62. // Notify systemd of status via the notify protocol
  63. func (g *Manager) notify(msg systemdNotifyMsg) {
  64. conn, err := getNotifySocket()
  65. if err != nil {
  66. // the err is logged in getNotifySocket
  67. return
  68. }
  69. if conn == nil {
  70. return
  71. }
  72. defer conn.Close()
  73. if _, err = conn.Write([]byte(msg)); err != nil {
  74. log.Warn("Failed to notify NOTIFY_SOCKET: %v", err)
  75. return
  76. }
  77. }
  78. func (g *Manager) start(ctx context.Context) {
  79. // Make contexts
  80. g.terminateCtx, g.terminateCtxCancel = context.WithCancel(ctx)
  81. g.shutdownCtx, g.shutdownCtxCancel = context.WithCancel(ctx)
  82. g.hammerCtx, g.hammerCtxCancel = context.WithCancel(ctx)
  83. g.managerCtx, g.managerCtxCancel = context.WithCancel(ctx)
  84. // Next add pprof labels to these contexts
  85. g.terminateCtx = pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "with-terminate"))
  86. g.shutdownCtx = pprof.WithLabels(g.shutdownCtx, pprof.Labels("graceful-lifecycle", "with-shutdown"))
  87. g.hammerCtx = pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "with-hammer"))
  88. g.managerCtx = pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "with-manager"))
  89. // Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
  90. pprof.SetGoroutineLabels(g.managerCtx)
  91. defer pprof.SetGoroutineLabels(ctx)
  92. // Set the running state & handle signals
  93. g.setState(stateRunning)
  94. g.notify(statusMsg("Starting Gitea"))
  95. g.notify(pidMsg())
  96. go g.handleSignals(g.managerCtx)
  97. // Handle clean up of unused provided listeners and delayed start-up
  98. startupDone := make(chan struct{})
  99. go func() {
  100. defer close(startupDone)
  101. // Wait till we're done getting all of the listeners and then close
  102. // the unused ones
  103. g.createServerWaitGroup.Wait()
  104. // Ignore the error here there's not much we can do with it
  105. // They're logged in the CloseProvidedListeners function
  106. _ = CloseProvidedListeners()
  107. g.notify(readyMsg)
  108. }()
  109. if setting.StartupTimeout > 0 {
  110. go func() {
  111. select {
  112. case <-startupDone:
  113. return
  114. case <-g.IsShutdown():
  115. func() {
  116. // When waitgroup counter goes negative it will panic - we don't care about this so we can just ignore it.
  117. defer func() {
  118. _ = recover()
  119. }()
  120. // Ensure that the createServerWaitGroup stops waiting
  121. for {
  122. g.createServerWaitGroup.Done()
  123. }
  124. }()
  125. return
  126. case <-time.After(setting.StartupTimeout):
  127. log.Error("Startup took too long! Shutting down")
  128. g.notify(statusMsg("Startup took too long! Shutting down"))
  129. g.notify(stoppingMsg)
  130. g.doShutdown()
  131. }
  132. }()
  133. }
  134. }
  135. func (g *Manager) handleSignals(ctx context.Context) {
  136. ctx, _, finished := process.GetManager().AddTypedContext(ctx, "Graceful: HandleSignals", process.SystemProcessType, true)
  137. defer finished()
  138. signalChannel := make(chan os.Signal, 1)
  139. signal.Notify(
  140. signalChannel,
  141. syscall.SIGHUP,
  142. syscall.SIGUSR1,
  143. syscall.SIGUSR2,
  144. syscall.SIGINT,
  145. syscall.SIGTERM,
  146. syscall.SIGTSTP,
  147. )
  148. watchdogTimeout := getWatchdogTimeout()
  149. t := &time.Ticker{}
  150. if watchdogTimeout != 0 {
  151. g.notify(watchdogMsg)
  152. t = time.NewTicker(watchdogTimeout / 2)
  153. }
  154. pid := syscall.Getpid()
  155. for {
  156. select {
  157. case sig := <-signalChannel:
  158. switch sig {
  159. case syscall.SIGHUP:
  160. log.Info("PID: %d. Received SIGHUP. Attempting GracefulRestart...", pid)
  161. g.DoGracefulRestart()
  162. case syscall.SIGUSR1:
  163. log.Warn("PID %d. Received SIGUSR1. Releasing and reopening logs", pid)
  164. g.notify(statusMsg("Releasing and reopening logs"))
  165. if err := releasereopen.GetManager().ReleaseReopen(); err != nil {
  166. log.Error("Error whilst releasing and reopening logs: %v", err)
  167. }
  168. case syscall.SIGUSR2:
  169. log.Warn("PID %d. Received SIGUSR2. Hammering...", pid)
  170. g.DoImmediateHammer()
  171. case syscall.SIGINT:
  172. log.Warn("PID %d. Received SIGINT. Shutting down...", pid)
  173. g.DoGracefulShutdown()
  174. case syscall.SIGTERM:
  175. log.Warn("PID %d. Received SIGTERM. Shutting down...", pid)
  176. g.DoGracefulShutdown()
  177. case syscall.SIGTSTP:
  178. log.Info("PID %d. Received SIGTSTP.", pid)
  179. default:
  180. log.Info("PID %d. Received %v.", pid, sig)
  181. }
  182. case <-t.C:
  183. g.notify(watchdogMsg)
  184. case <-ctx.Done():
  185. log.Warn("PID: %d. Background context for manager closed - %v - Shutting down...", pid, ctx.Err())
  186. g.DoGracefulShutdown()
  187. return
  188. }
  189. }
  190. }
  191. func (g *Manager) doFork() error {
  192. g.lock.Lock()
  193. if g.forked {
  194. g.lock.Unlock()
  195. return errors.New("another process already forked. Ignoring this one")
  196. }
  197. g.forked = true
  198. g.lock.Unlock()
  199. g.notify(reloadingMsg)
  200. // We need to move the file logs to append pids
  201. setting.RestartLogsWithPIDSuffix()
  202. _, err := RestartProcess()
  203. return err
  204. }
  205. // DoGracefulRestart causes a graceful restart
  206. func (g *Manager) DoGracefulRestart() {
  207. if setting.GracefulRestartable {
  208. log.Info("PID: %d. Forking...", os.Getpid())
  209. err := g.doFork()
  210. if err != nil {
  211. if err.Error() == "another process already forked. Ignoring this one" {
  212. g.DoImmediateHammer()
  213. } else {
  214. log.Error("Error whilst forking from PID: %d : %v", os.Getpid(), err)
  215. }
  216. }
  217. // doFork calls RestartProcess which starts a new Gitea process, so this parent process needs to exit
  218. // Otherwise some resources (eg: leveldb lock) will be held by this parent process and the new process will fail to start
  219. log.Info("PID: %d. Shutting down after forking ...", os.Getpid())
  220. g.doShutdown()
  221. } else {
  222. log.Info("PID: %d. Not set restartable. Shutting down...", os.Getpid())
  223. g.notify(stoppingMsg)
  224. g.doShutdown()
  225. }
  226. }
  227. // DoImmediateHammer causes an immediate hammer
  228. func (g *Manager) DoImmediateHammer() {
  229. g.notify(statusMsg("Sending immediate hammer"))
  230. g.doHammerTime(0 * time.Second)
  231. }
  232. // DoGracefulShutdown causes a graceful shutdown
  233. func (g *Manager) DoGracefulShutdown() {
  234. g.lock.Lock()
  235. if !g.forked {
  236. g.lock.Unlock()
  237. g.notify(stoppingMsg)
  238. } else {
  239. g.lock.Unlock()
  240. g.notify(statusMsg("Shutting down after fork"))
  241. }
  242. g.doShutdown()
  243. }
  244. // RegisterServer registers the running of a listening server, in the case of unix this means that the parent process can now die.
  245. // Any call to RegisterServer must be matched by a call to ServerDone
  246. func (g *Manager) RegisterServer() {
  247. KillParent()
  248. g.runningServerWaitGroup.Add(1)
  249. }