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.

92 lines
2.1 KiB

  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. },
  24. }
  25. subcmdShutdown = cli.Command{
  26. Name: "shutdown",
  27. Usage: "Gracefully shutdown the running process",
  28. Action: runShutdown,
  29. }
  30. subcmdRestart = cli.Command{
  31. Name: "restart",
  32. Usage: "Gracefully restart the running process - (not implemented for windows servers)",
  33. Action: runRestart,
  34. }
  35. subcmdFlushQueues = cli.Command{
  36. Name: "flush-queues",
  37. Usage: "Flush queues in the running process",
  38. Action: runFlushQueues,
  39. Flags: []cli.Flag{
  40. cli.DurationFlag{
  41. Name: "timeout",
  42. Value: 60 * time.Second,
  43. Usage: "Timeout for the flushing process",
  44. },
  45. cli.BoolFlag{
  46. Name: "non-blocking",
  47. Usage: "Set to true to not wait for flush to complete before returning",
  48. },
  49. },
  50. }
  51. )
  52. func runShutdown(c *cli.Context) error {
  53. setup("manager", false)
  54. statusCode, msg := private.Shutdown()
  55. switch statusCode {
  56. case http.StatusInternalServerError:
  57. fail("InternalServerError", msg)
  58. }
  59. fmt.Fprintln(os.Stdout, msg)
  60. return nil
  61. }
  62. func runRestart(c *cli.Context) error {
  63. setup("manager", false)
  64. statusCode, msg := private.Restart()
  65. switch statusCode {
  66. case http.StatusInternalServerError:
  67. fail("InternalServerError", msg)
  68. }
  69. fmt.Fprintln(os.Stdout, msg)
  70. return nil
  71. }
  72. func runFlushQueues(c *cli.Context) error {
  73. setup("manager", false)
  74. statusCode, msg := private.FlushQueues(c.Duration("timeout"), c.Bool("non-blocking"))
  75. switch statusCode {
  76. case http.StatusInternalServerError:
  77. fail("InternalServerError", msg)
  78. }
  79. fmt.Fprintln(os.Stdout, msg)
  80. return nil
  81. }