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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "time"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // Shutdown calls the internal shutdown function
  15. func Shutdown(ctx context.Context) ResponseExtra {
  16. reqURL := setting.LocalURL + "api/internal/manager/shutdown"
  17. req := newInternalRequest(ctx, reqURL, "POST")
  18. return requestJSONClientMsg(req, "Shutting down")
  19. }
  20. // Restart calls the internal restart function
  21. func Restart(ctx context.Context) ResponseExtra {
  22. reqURL := setting.LocalURL + "api/internal/manager/restart"
  23. req := newInternalRequest(ctx, reqURL, "POST")
  24. return requestJSONClientMsg(req, "Restarting")
  25. }
  26. // ReloadTemplates calls the internal reload-templates function
  27. func ReloadTemplates(ctx context.Context) ResponseExtra {
  28. reqURL := setting.LocalURL + "api/internal/manager/reload-templates"
  29. req := newInternalRequest(ctx, reqURL, "POST")
  30. return requestJSONClientMsg(req, "Reloaded")
  31. }
  32. // FlushOptions represents the options for the flush call
  33. type FlushOptions struct {
  34. Timeout time.Duration
  35. NonBlocking bool
  36. }
  37. // FlushQueues calls the internal flush-queues function
  38. func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ResponseExtra {
  39. reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
  40. req := newInternalRequest(ctx, reqURL, "POST", FlushOptions{Timeout: timeout, NonBlocking: nonBlocking})
  41. if timeout > 0 {
  42. req.SetReadWriteTimeout(timeout + 10*time.Second)
  43. }
  44. return requestJSONClientMsg(req, "Flushed")
  45. }
  46. // PauseLogging pauses logging
  47. func PauseLogging(ctx context.Context) ResponseExtra {
  48. reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
  49. req := newInternalRequest(ctx, reqURL, "POST")
  50. return requestJSONClientMsg(req, "Logging Paused")
  51. }
  52. // ResumeLogging resumes logging
  53. func ResumeLogging(ctx context.Context) ResponseExtra {
  54. reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
  55. req := newInternalRequest(ctx, reqURL, "POST")
  56. return requestJSONClientMsg(req, "Logging Restarted")
  57. }
  58. // ReleaseReopenLogging releases and reopens logging files
  59. func ReleaseReopenLogging(ctx context.Context) ResponseExtra {
  60. reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
  61. req := newInternalRequest(ctx, reqURL, "POST")
  62. return requestJSONClientMsg(req, "Logging Restarted")
  63. }
  64. // SetLogSQL sets database logging
  65. func SetLogSQL(ctx context.Context, on bool) ResponseExtra {
  66. reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
  67. req := newInternalRequest(ctx, reqURL, "POST")
  68. return requestJSONClientMsg(req, "Log SQL setting set")
  69. }
  70. // LoggerOptions represents the options for the add logger call
  71. type LoggerOptions struct {
  72. Logger string
  73. Writer string
  74. Mode string
  75. Config map[string]any
  76. }
  77. // AddLogger adds a logger
  78. func AddLogger(ctx context.Context, logger, writer, mode string, config map[string]any) ResponseExtra {
  79. reqURL := setting.LocalURL + "api/internal/manager/add-logger"
  80. req := newInternalRequest(ctx, reqURL, "POST", LoggerOptions{
  81. Logger: logger,
  82. Writer: writer,
  83. Mode: mode,
  84. Config: config,
  85. })
  86. return requestJSONClientMsg(req, "Added")
  87. }
  88. // RemoveLogger removes a logger
  89. func RemoveLogger(ctx context.Context, logger, writer string) ResponseExtra {
  90. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(logger), url.PathEscape(writer))
  91. req := newInternalRequest(ctx, reqURL, "POST")
  92. return requestJSONClientMsg(req, "Removed")
  93. }
  94. // Processes return the current processes from this gitea instance
  95. func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces, json bool, cancel string) ResponseExtra {
  96. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/processes?flat=%t&no-system=%t&stacktraces=%t&json=%t&cancel-pid=%s", flat, noSystem, stacktraces, json, url.QueryEscape(cancel))
  97. req := newInternalRequest(ctx, reqURL, "GET")
  98. callback := func(resp *http.Response, extra *ResponseExtra) {
  99. _, extra.Error = io.Copy(out, resp.Body)
  100. }
  101. _, extra := requestJSONResp(req, &responseCallback{callback})
  102. return extra
  103. }