gitea源码

check.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package healthcheck
  4. import (
  5. "context"
  6. "net/http"
  7. "os"
  8. "time"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/modules/cache"
  11. "code.gitea.io/gitea/modules/json"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. type status string
  16. const (
  17. // pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot)
  18. // fail unhealthy (acceptable aliases: "error" to support Node's Terminus and "down" for Java's SpringBoot), and
  19. // warn healthy, with some concerns.
  20. //
  21. // ref https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check#section-3.1
  22. // status: (required) indicates whether the service status is acceptable
  23. // or not. API publishers SHOULD use following values for the field:
  24. // The value of the status field is case-insensitive and is tightly
  25. // related with the HTTP response code returned by the health endpoint.
  26. // For "pass" status, HTTP response code in the 2xx-3xx range MUST be
  27. // used. For "fail" status, HTTP response code in the 4xx-5xx range
  28. // MUST be used. In case of the "warn" status, endpoints MUST return
  29. // HTTP status in the 2xx-3xx range, and additional information SHOULD
  30. // be provided, utilizing optional fields of the response.
  31. pass status = "pass"
  32. fail status = "fail"
  33. warn status = "warn"
  34. )
  35. func (s status) ToHTTPStatus() int {
  36. if s == pass || s == warn {
  37. return http.StatusOK
  38. }
  39. return http.StatusFailedDependency
  40. }
  41. type checks map[string][]componentStatus
  42. // response is the data returned by the health endpoint, which will be marshaled to JSON format
  43. type response struct {
  44. Status status `json:"status"`
  45. Description string `json:"description"` // a human-friendly description of the service
  46. Checks checks `json:"checks,omitempty"` // The Checks Object, should be omitted on installation route
  47. }
  48. // componentStatus presents one status of a single check object
  49. // an object that provides detailed health statuses of additional downstream systems and endpoints
  50. // which can affect the overall health of the main API.
  51. type componentStatus struct {
  52. Status status `json:"status"`
  53. Time string `json:"time"` // the date-time, in ISO8601 format
  54. Output string `json:"output,omitempty"` // this field SHOULD be omitted for "pass" state.
  55. }
  56. // Check is the health check API handler
  57. func Check(w http.ResponseWriter, r *http.Request) {
  58. rsp := response{
  59. Status: pass,
  60. Description: setting.AppName,
  61. Checks: make(checks),
  62. }
  63. statuses := make([]status, 0)
  64. if setting.InstallLock {
  65. statuses = append(statuses, checkDatabase(r.Context(), rsp.Checks))
  66. statuses = append(statuses, checkCache(rsp.Checks))
  67. }
  68. for _, s := range statuses {
  69. if s != pass {
  70. rsp.Status = fail
  71. break
  72. }
  73. }
  74. data, _ := json.MarshalIndent(rsp, "", " ")
  75. w.Header().Set("Content-Type", "application/json")
  76. w.WriteHeader(rsp.Status.ToHTTPStatus())
  77. _, _ = w.Write(data)
  78. }
  79. // database checks gitea database status
  80. func checkDatabase(ctx context.Context, checks checks) status {
  81. st := componentStatus{}
  82. if err := db.GetEngine(ctx).Ping(); err != nil {
  83. st.Status = fail
  84. st.Time = getCheckTime()
  85. log.Error("database ping failed with error: %v", err)
  86. } else {
  87. st.Status = pass
  88. st.Time = getCheckTime()
  89. }
  90. if setting.Database.Type.IsSQLite3() && st.Status == pass {
  91. if !setting.EnableSQLite3 {
  92. st.Status = fail
  93. st.Time = getCheckTime()
  94. log.Error("SQLite3 health check failed with error: %v", "this Gitea binary is built without SQLite3 enabled")
  95. } else {
  96. if _, err := os.Stat(setting.Database.Path); err != nil {
  97. st.Status = fail
  98. st.Time = getCheckTime()
  99. log.Error("SQLite3 file exists check failed with error: %v", err)
  100. }
  101. }
  102. }
  103. checks["database:ping"] = []componentStatus{st}
  104. return st.Status
  105. }
  106. // cache checks gitea cache status
  107. func checkCache(checks checks) status {
  108. st := componentStatus{}
  109. if err := cache.GetCache().Ping(); err != nil {
  110. st.Status = fail
  111. st.Time = getCheckTime()
  112. log.Error("cache ping failed with error: %v", err)
  113. } else {
  114. st.Status = pass
  115. st.Time = getCheckTime()
  116. }
  117. checks["cache:ping"] = []componentStatus{st}
  118. return st.Status
  119. }
  120. func getCheckTime() string {
  121. return time.Now().UTC().Format(time.RFC3339)
  122. }