mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	Add health check endpoint (#18465)
* chore: add health check endpoint docs: update document about health check fix: fix up Sqlite3 ping. current ping will success even if the db file is missing fix: do not expose privacy information in output field * refactor: remove HealthChecker struct * Added `/api/healthz` to install routes. This was needed for using /api/healthz endpoint in Docker healthchecks, otherwise, Docker would never become healthy if using healthz endpoint and users would not be able to complete the installation of Gitea. * Update modules/cache/cache.go * fine tune * Remove unnecessary test code. Now there are 2 routes for installation (and maybe more in future) Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Marcos de Oliveira <marcossantos@furb.br>
This commit is contained in:
		
							
								
								
									
										37
									
								
								modules/cache/cache.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										37
									
								
								modules/cache/cache.go
									
									
									
									
										vendored
									
									
								
							| @@ -5,6 +5,7 @@ | ||||
| package cache | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"strconv" | ||||
|  | ||||
| @@ -34,25 +35,37 @@ func NewContext() error { | ||||
| 		if conn, err = newCache(setting.CacheService.Cache); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		const testKey = "__gitea_cache_test" | ||||
| 		const testVal = "test-value" | ||||
| 		if err = conn.Put(testKey, testVal, 10); err != nil { | ||||
| 		if err = Ping(); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		val := conn.Get(testKey) | ||||
| 		if valStr, ok := val.(string); !ok || valStr != testVal { | ||||
| 			// If the cache is full, the Get may not read the expected value stored by Put. | ||||
| 			// Since we have checked that Put can success, so we just show a warning here, do not return an error to panic. | ||||
| 			log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'", | ||||
| 				setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn, | ||||
| 				testVal, val, | ||||
| 			) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // Ping checks if the cache service works or not, it not, it returns an error | ||||
| func Ping() error { | ||||
| 	if conn == nil { | ||||
| 		return errors.New("cache not available") | ||||
| 	} | ||||
| 	var err error | ||||
| 	const testKey = "__gitea_cache_test" | ||||
| 	const testVal = "test-value" | ||||
| 	if err = conn.Put(testKey, testVal, 10); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	val := conn.Get(testKey) | ||||
| 	if valStr, ok := val.(string); !ok || valStr != testVal { | ||||
| 		// If the cache is full, the Get may not read the expected value stored by Put. | ||||
| 		// Since we have checked that Put can success, so we just show a warning here, do not return an error to panic. | ||||
| 		log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'", | ||||
| 			setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn, | ||||
| 			testVal, val, | ||||
| 		) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // GetCache returns the currently configured cache | ||||
| func GetCache() mc.Cache { | ||||
| 	return conn | ||||
|   | ||||
		Reference in New Issue
	
	Block a user