//DiscoveryRegister - registers a new service func DiscoveryRegister(w http.ResponseWriter, r *http.Request) { requestService := new(models.RegisterService) decoder := json.NewDecoder(r.Body) decoder.Decode(&requestService) if len(requestService.BaseURI) == 0 { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Base URI is missing")) return } if len(requestService.ServiceName) == 0 { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Service Name is missing")) return } marshelledService, _ := json.Marshal(requestService) client := redis.NewRedis() client.Set("Bill-Service-LastUpdated", requestService.ServiceName, time.Minute) client.HSet("Bill-Service", requestService.ServiceName, string(marshelledService)) w.WriteHeader(http.StatusOK) }
func getResultFromCache(service string) []byte { result, err := redis.NewRedis().HGet("health", service).Bytes() if err != nil { panic(err) } return result }
func monitorNewService() chan error { result := make(chan error) go func() { result <- func() error { client := redis.NewRedis() client.ConfigSet("notify-keyspace-events", "KEA") sub, err := client.PSubscribe("__keyspace@0__:Bill-Service") defer sub.Close() if err != nil { return err } for { _, err := sub.ReceiveMessage() lastUpdated, _ := lastUpdatedService() serviceDetails, err := client.HGet("Bill-Service", lastUpdated).Result() if err != nil { return err } var requestService models.RegisterService if err = json.Unmarshal([]byte(serviceDetails), &requestService); err != nil { return err } go loadServicesJobs(requestService) } }() }() return result }
func lastUpdatedService() (string, error) { serviceName, err := redis.NewRedis().Get("Bill-Service-LastUpdated").Result() if err != nil { return "", err } return serviceName, nil }
//InitialiseJobs - loads up the current jobs from Redis and starts a schedule for them. func InitialiseJobs() error { services, err := redis.NewRedis().HGetAllMap("Bill-Service").Result() if err != nil { return err } defer c.Start() go loadCurrentService(services) go monitorNewService() return nil }
package jobRunner import ( "encoding/json" "goBill/models" "goBill/util/redis" "log" "strconv" "time" "github.com/parnurzeal/gorequest" ) var redisClient = redis.NewRedis() func checkJob(job models.Job, serviceName string) { r := getRequest(job) log.Println("Checking " + job.Name) var now = time.Now() resp, _, err := r.End() responseTime := time.Since(now).Nanoseconds() errorMessage := "" status := "" if err != nil { for _, e := range err { errorMessage += e.Error() } }