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

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