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

server_signals.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }