func updateRedis(manager manager.Manager) { for { glog.Infoln("update redis") if connection := redisAvailable(manager); connection != nil { for _, node := range manager.GetNodes() { nodeInJSON, _ := json.Marshal(&node) key := "charmander:nodes:" + node.Hostname sendCommand(connection, "SET", key, fmt.Sprintf("%s", nodeInJSON)) sendCommand(connection, "EXPIRE", key, "30") //timeout after 30s } for _, task := range manager.GetTasks() { if task.NotMetered { continue } taskInJSON, _ := json.Marshal(&task) key := "charmander:tasks-metered:" + task.InternalID sendCommand(connection, "SET", key, fmt.Sprintf("%s", taskInJSON)) sendCommand(connection, "EXPIRE", key, "30") //timeout after 30s } for _, task := range manager.GetTasks() { taskInJSON, _ := json.Marshal(&task) key := "charmander:tasks:" + task.InternalID sendCommand(connection, "SET", key, fmt.Sprintf("%s", taskInJSON)) sendCommand(connection, "EXPIRE", key, "30") //timeout after 30s } for key, value := range *getTaskIntelligence(manager) { parts := strings.Split(key, ":") manager.SetTaskIntelligence(parts[0], parts[1], value) } connection.Close() } time.Sleep(15 * time.Second) } }
func redisAvailable(manager manager.Manager) net.Conn { connection, error := net.DialTimeout("tcp", manager.GetRedisConnectionIPAndPort(), 2*time.Second) if error != nil { return nil } return connection }