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 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 private
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "time"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // Shutdown calls the internal shutdown function
  13. func Shutdown() (int, string) {
  14. reqURL := setting.LocalURL + "api/internal/manager/shutdown"
  15. req := newInternalRequest(reqURL, "POST")
  16. resp, err := req.Response()
  17. if err != nil {
  18. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  19. }
  20. defer resp.Body.Close()
  21. if resp.StatusCode != http.StatusOK {
  22. return resp.StatusCode, decodeJSONError(resp).Err
  23. }
  24. return http.StatusOK, "Shutting down"
  25. }
  26. // Restart calls the internal restart function
  27. func Restart() (int, string) {
  28. reqURL := setting.LocalURL + "api/internal/manager/restart"
  29. req := newInternalRequest(reqURL, "POST")
  30. resp, err := req.Response()
  31. if err != nil {
  32. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  33. }
  34. defer resp.Body.Close()
  35. if resp.StatusCode != http.StatusOK {
  36. return resp.StatusCode, decodeJSONError(resp).Err
  37. }
  38. return http.StatusOK, "Restarting"
  39. }
  40. // FlushOptions represents the options for the flush call
  41. type FlushOptions struct {
  42. Timeout time.Duration
  43. NonBlocking bool
  44. }
  45. // FlushQueues calls the internal flush-queues function
  46. func FlushQueues(timeout time.Duration, nonBlocking bool) (int, string) {
  47. reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
  48. req := newInternalRequest(reqURL, "POST")
  49. if timeout > 0 {
  50. req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
  51. }
  52. req = req.Header("Content-Type", "application/json")
  53. jsonBytes, _ := json.Marshal(FlushOptions{
  54. Timeout: timeout,
  55. NonBlocking: nonBlocking,
  56. })
  57. req.Body(jsonBytes)
  58. resp, err := req.Response()
  59. if err != nil {
  60. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  61. }
  62. defer resp.Body.Close()
  63. if resp.StatusCode != http.StatusOK {
  64. return resp.StatusCode, decodeJSONError(resp).Err
  65. }
  66. return http.StatusOK, "Flushed"
  67. }