use the new store in the watchdog

This commit is contained in:
Chris Heppell 2020-12-31 11:49:41 +00:00
parent c3bc375ff1
commit f8e1fc25a4

View File

@ -10,13 +10,11 @@ import (
"github.com/TwinProduction/gatus/config"
"github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/metric"
"github.com/TwinProduction/gatus/storage"
)
var (
serviceStatuses = make(map[string]*core.ServiceStatus)
// serviceStatusesMutex is used to prevent concurrent map access
serviceStatusesMutex sync.RWMutex
store = storage.NewInMemoryStore()
// monitoringMutex is used to prevent multiple services from being evaluated at the same time.
// Without this, conditions using response time may become inaccurate.
@ -26,18 +24,16 @@ var (
// GetJSONEncodedServiceStatuses returns a list of core.ServiceStatus for each services encoded using json.Marshal.
// The reason why the encoding is done here is because we use a mutex to prevent concurrent map access.
func GetJSONEncodedServiceStatuses() ([]byte, error) {
serviceStatusesMutex.RLock()
serviceStatuses := store.GetAll()
data, err := json.Marshal(serviceStatuses)
serviceStatusesMutex.RUnlock()
return data, err
}
// GetUptimeByServiceGroupAndName returns the uptime of a service based on its group and name
func GetUptimeByServiceGroupAndName(group, name string) *core.Uptime {
key := fmt.Sprintf("%s_%s", group, name)
serviceStatusesMutex.RLock()
serviceStatuses := store.GetAll()
serviceStatus, exists := serviceStatuses[key]
serviceStatusesMutex.RUnlock()
if !exists {
return nil
}
@ -93,13 +89,5 @@ func monitor(service *core.Service) {
// UpdateServiceStatuses updates the slice of service statuses
func UpdateServiceStatuses(service *core.Service, result *core.Result) {
key := fmt.Sprintf("%s_%s", service.Group, service.Name)
serviceStatusesMutex.Lock()
serviceStatus, exists := serviceStatuses[key]
if !exists {
serviceStatus = core.NewServiceStatus(service)
serviceStatuses[key] = serviceStatus
}
serviceStatus.AddResult(result)
serviceStatusesMutex.Unlock()
store.Insert(service, result)
}