| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:03 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 18:03:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/db" | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:03 +08:00
										 |  |  | func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool { | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | 	const cacheKey = "db-healthy" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if cached, found := hs.CacheService.Get(cacheKey); found { | 
					
						
							|  |  |  | 		return cached.(bool) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 18:03:36 +08:00
										 |  |  | 	err := hs.SQLStore.WithDbSession(ctx, func(session *db.Session) error { | 
					
						
							|  |  |  | 		_, err := session.Exec("SELECT 1") | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	healthy := err == nil | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	hs.CacheService.Set(cacheKey, healthy, time.Second*5) | 
					
						
							|  |  |  | 	return healthy | 
					
						
							|  |  |  | } |