本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

96 rindas
2.1KB

  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. package graceful
  6. import (
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. var hookableSignals []os.Signal
  15. func init() {
  16. hookableSignals = []os.Signal{
  17. syscall.SIGHUP,
  18. syscall.SIGUSR1,
  19. syscall.SIGUSR2,
  20. syscall.SIGINT,
  21. syscall.SIGTERM,
  22. syscall.SIGTSTP,
  23. }
  24. }
  25. // handleSignals listens for os Signals and calls any hooked in function that the
  26. // user had registered with the signal.
  27. func (srv *Server) handleSignals() {
  28. var sig os.Signal
  29. signal.Notify(
  30. srv.sigChan,
  31. hookableSignals...,
  32. )
  33. pid := syscall.Getpid()
  34. for {
  35. sig = <-srv.sigChan
  36. srv.preSignalHooks(sig)
  37. switch sig {
  38. case syscall.SIGHUP:
  39. if setting.GracefulRestartable {
  40. log.Info("PID: %d. Received SIGHUP. Forking...", pid)
  41. err := srv.fork()
  42. if err != nil && err.Error() != "another process already forked. Ignoring this one" {
  43. log.Error("Error whilst forking from PID: %d : %v", pid, err)
  44. }
  45. } else {
  46. log.Info("PID: %d. Received SIGHUP. Not set restartable. Shutting down...", pid)
  47. srv.shutdown()
  48. }
  49. case syscall.SIGUSR1:
  50. log.Info("PID %d. Received SIGUSR1.", pid)
  51. case syscall.SIGUSR2:
  52. log.Warn("PID %d. Received SIGUSR2. Hammering...", pid)
  53. srv.hammerTime(0 * time.Second)
  54. case syscall.SIGINT:
  55. log.Warn("PID %d. Received SIGINT. Shutting down...", pid)
  56. srv.shutdown()
  57. case syscall.SIGTERM:
  58. log.Warn("PID %d. Received SIGTERM. Shutting down...", pid)
  59. srv.shutdown()
  60. case syscall.SIGTSTP:
  61. log.Info("PID %d. Received SIGTSTP.")
  62. default:
  63. log.Info("PID %d. Received %v.", sig)
  64. }
  65. srv.postSignalHooks(sig)
  66. }
  67. }
  68. func (srv *Server) preSignalHooks(sig os.Signal) {
  69. if _, notSet := srv.PreSignalHooks[sig]; !notSet {
  70. return
  71. }
  72. for _, f := range srv.PreSignalHooks[sig] {
  73. f()
  74. }
  75. }
  76. func (srv *Server) postSignalHooks(sig os.Signal) {
  77. if _, notSet := srv.PostSignalHooks[sig]; !notSet {
  78. return
  79. }
  80. for _, f := range srv.PostSignalHooks[sig] {
  81. f()
  82. }
  83. }
上海开阖软件有限公司 沪ICP备12045867号-1