gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "os"
  7. "time"
  8. "code.gitea.io/gitea/modules/private"
  9. "github.com/urfave/cli/v3"
  10. )
  11. var (
  12. // CmdManager represents the manager command
  13. CmdManager = &cli.Command{
  14. Name: "manager",
  15. Usage: "Manage the running gitea process",
  16. Description: "This is a command for managing the running gitea process",
  17. Commands: []*cli.Command{
  18. subcmdShutdown,
  19. subcmdRestart,
  20. subcmdReloadTemplates,
  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. subcmdReloadTemplates = &cli.Command{
  47. Name: "reload-templates",
  48. Usage: "Reload template files in the running process",
  49. Flags: []cli.Flag{
  50. &cli.BoolFlag{
  51. Name: "debug",
  52. },
  53. },
  54. Action: runReloadTemplates,
  55. }
  56. subcmdFlushQueues = &cli.Command{
  57. Name: "flush-queues",
  58. Usage: "Flush queues in the running process",
  59. Action: runFlushQueues,
  60. Flags: []cli.Flag{
  61. &cli.DurationFlag{
  62. Name: "timeout",
  63. Value: 60 * time.Second,
  64. Usage: "Timeout for the flushing process",
  65. },
  66. &cli.BoolFlag{
  67. Name: "non-blocking",
  68. Usage: "Set to true to not wait for flush to complete before returning",
  69. },
  70. &cli.BoolFlag{
  71. Name: "debug",
  72. },
  73. },
  74. }
  75. subCmdProcesses = &cli.Command{
  76. Name: "processes",
  77. Usage: "Display running processes within the current process",
  78. Action: runProcesses,
  79. Flags: []cli.Flag{
  80. &cli.BoolFlag{
  81. Name: "debug",
  82. },
  83. &cli.BoolFlag{
  84. Name: "flat",
  85. Usage: "Show processes as flat table rather than as tree",
  86. },
  87. &cli.BoolFlag{
  88. Name: "no-system",
  89. Usage: "Do not show system processes",
  90. },
  91. &cli.BoolFlag{
  92. Name: "stacktraces",
  93. Usage: "Show stacktraces",
  94. },
  95. &cli.BoolFlag{
  96. Name: "json",
  97. Usage: "Output as json",
  98. },
  99. &cli.StringFlag{
  100. Name: "cancel",
  101. Usage: "Process PID to cancel. (Only available for non-system processes.)",
  102. },
  103. },
  104. }
  105. )
  106. func runShutdown(ctx context.Context, c *cli.Command) error {
  107. setup(ctx, c.Bool("debug"))
  108. extra := private.Shutdown(ctx)
  109. return handleCliResponseExtra(extra)
  110. }
  111. func runRestart(ctx context.Context, c *cli.Command) error {
  112. setup(ctx, c.Bool("debug"))
  113. extra := private.Restart(ctx)
  114. return handleCliResponseExtra(extra)
  115. }
  116. func runReloadTemplates(ctx context.Context, c *cli.Command) error {
  117. setup(ctx, c.Bool("debug"))
  118. extra := private.ReloadTemplates(ctx)
  119. return handleCliResponseExtra(extra)
  120. }
  121. func runFlushQueues(ctx context.Context, c *cli.Command) error {
  122. setup(ctx, c.Bool("debug"))
  123. extra := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
  124. return handleCliResponseExtra(extra)
  125. }
  126. func runProcesses(ctx context.Context, c *cli.Command) error {
  127. setup(ctx, c.Bool("debug"))
  128. extra := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
  129. return handleCliResponseExtra(extra)
  130. }