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.

manager.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2020 The Gitea 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 cmd
  5. import (
  6. "fmt"
  7. "net/http"
  8. "os"
  9. "time"
  10. "code.gitea.io/gitea/modules/private"
  11. "github.com/urfave/cli"
  12. )
  13. var (
  14. // CmdManager represents the manager command
  15. CmdManager = cli.Command{
  16. Name: "manager",
  17. Usage: "Manage the running gitea process",
  18. Description: "This is a command for managing the running gitea process",
  19. Subcommands: []cli.Command{
  20. subcmdShutdown,
  21. subcmdRestart,
  22. subcmdFlushQueues,
  23. subcmdLogging,
  24. subCmdProcesses,
  25. },
  26. }
  27. subcmdShutdown = cli.Command{
  28. Name: "shutdown",
  29. Usage: "Gracefully shutdown the running process",
  30. Flags: []cli.Flag{
  31. cli.BoolFlag{
  32. Name: "debug",
  33. },
  34. },
  35. Action: runShutdown,
  36. }
  37. subcmdRestart = cli.Command{
  38. Name: "restart",
  39. Usage: "Gracefully restart the running process - (not implemented for windows servers)",
  40. Flags: []cli.Flag{
  41. cli.BoolFlag{
  42. Name: "debug",
  43. },
  44. },
  45. Action: runRestart,
  46. }
  47. subcmdFlushQueues = cli.Command{
  48. Name: "flush-queues",
  49. Usage: "Flush queues in the running process",
  50. Action: runFlushQueues,
  51. Flags: []cli.Flag{
  52. cli.DurationFlag{
  53. Name: "timeout",
  54. Value: 60 * time.Second,
  55. Usage: "Timeout for the flushing process",
  56. },
  57. cli.BoolFlag{
  58. Name: "non-blocking",
  59. Usage: "Set to true to not wait for flush to complete before returning",
  60. },
  61. cli.BoolFlag{
  62. Name: "debug",
  63. },
  64. },
  65. }
  66. subCmdProcesses = cli.Command{
  67. Name: "processes",
  68. Usage: "Display running processes within the current process",
  69. Action: runProcesses,
  70. Flags: []cli.Flag{
  71. cli.BoolFlag{
  72. Name: "debug",
  73. },
  74. cli.BoolFlag{
  75. Name: "flat",
  76. Usage: "Show processes as flat table rather than as tree",
  77. },
  78. cli.BoolFlag{
  79. Name: "no-system",
  80. Usage: "Do not show system proceses",
  81. },
  82. cli.BoolFlag{
  83. Name: "stacktraces",
  84. Usage: "Show stacktraces",
  85. },
  86. cli.BoolFlag{
  87. Name: "json",
  88. Usage: "Output as json",
  89. },
  90. cli.StringFlag{
  91. Name: "cancel",
  92. Usage: "Process PID to cancel. (Only available for non-system processes.)",
  93. },
  94. },
  95. }
  96. )
  97. func runShutdown(c *cli.Context) error {
  98. ctx, cancel := installSignals()
  99. defer cancel()
  100. setup("manager", c.Bool("debug"))
  101. statusCode, msg := private.Shutdown(ctx)
  102. switch statusCode {
  103. case http.StatusInternalServerError:
  104. return fail("InternalServerError", msg)
  105. }
  106. fmt.Fprintln(os.Stdout, msg)
  107. return nil
  108. }
  109. func runRestart(c *cli.Context) error {
  110. ctx, cancel := installSignals()
  111. defer cancel()
  112. setup("manager", c.Bool("debug"))
  113. statusCode, msg := private.Restart(ctx)
  114. switch statusCode {
  115. case http.StatusInternalServerError:
  116. return fail("InternalServerError", msg)
  117. }
  118. fmt.Fprintln(os.Stdout, msg)
  119. return nil
  120. }
  121. func runFlushQueues(c *cli.Context) error {
  122. ctx, cancel := installSignals()
  123. defer cancel()
  124. setup("manager", c.Bool("debug"))
  125. statusCode, msg := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
  126. switch statusCode {
  127. case http.StatusInternalServerError:
  128. return fail("InternalServerError", msg)
  129. }
  130. fmt.Fprintln(os.Stdout, msg)
  131. return nil
  132. }
  133. func runProcesses(c *cli.Context) error {
  134. ctx, cancel := installSignals()
  135. defer cancel()
  136. setup("manager", c.Bool("debug"))
  137. statusCode, msg := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
  138. switch statusCode {
  139. case http.StatusInternalServerError:
  140. return fail("InternalServerError", msg)
  141. }
  142. return nil
  143. }