func getSkinFromReply(resp *redis.Reply) (minecraft.Skin, error) { respBytes, respErr := resp.Bytes() if respErr != nil { return minecraft.Skin{}, respErr } imgBuf := bytes.NewReader(respBytes) skin, skinErr := minecraft.DecodeSkin(imgBuf) if skinErr != nil { return minecraft.Skin{}, skinErr } return skin, nil }
// Parses a reply from redis INFO into a nice map. func parseStats(resp *redis.Reply) (map[string]string, error) { r, err := resp.Bytes() if err != nil { return nil, err } raw := strings.Split(string(r), "\r\n") output := map[string]string{} for _, line := range raw { // Skip blank lines or comment lines if len(line) == 0 || string(line[0]) == "#" { continue } // Get the position the seperator breaks sep := strings.Index(line, ":") if sep == -1 { return nil, errors.New("Invalid line: " + line) } output[line[:sep]] = line[sep+1:] } return output, nil }
func GetRedisHashStringValue(redisClient *redis.Client, hashKey string, key string) (string, error) { var reply *redis.Reply = redisClient.Cmd("HGET", hashKey, key) return reply.Str() }
func GetRedisStringValue(redisClient *redis.Client, key string) (string, error) { var reply *redis.Reply = redisClient.Cmd("GET", key) return reply.Str() }
func SetMembers(redisClient *redis.Client, key string) ([]string, error) { var reply *redis.Reply = redisClient.Cmd("SMEMBERS", key) return reply.List() }
func ListItems(redisClient *redis.Client, key string, offset int64, number int64) ([]string, error) { var reply *redis.Reply = redisClient.Cmd("LRANGE", key, offset, number) return reply.List() }
func main() { var yml_config_file string y_c, e := os.LookupEnv("REDSHIFT_CONFIG") if e == false { yml_config_file = "config.yml" } else { yml_config_file = y_c } settings, err := yaml.Open(yml_config_file) s.CheckError(err, true) debug := settings.Get("debug").(bool) senders := make([]string, 0) k := settings.Get("sends") for m, _ := range k.(map[interface{}]interface{}) { n := fmt.Sprintf("%sGo", strings.Title(m.(string))) senders = append(senders, n) } // There's probably a better way to do this redis_is_cluster := settings.Get("redis_is_cluster").(bool) redis_list := settings.Get("redis_list").(string) redis_watch_interval := settings.Get("redis_watch_interval").(int) t := time.Now() ts := t.Format("Mon Jan 2 15:04:05 -0700 MST 2006") var rClient *redis.Client var rCluster *cluster.Cluster if redis_is_cluster { if debug { fmt.Printf("[%s] INFO Starting up in cluster mode\n", ts) } rCluster = PullCluster(settings) defer rCluster.Close() } else { redis_db := settings.Get("redis_db").(int) if debug { fmt.Printf("[%s] INFO Starting up in single node mode\n", ts) } rClient = PullNode(settings) defer rClient.Close() rClient.Cmd("SELECT", redis_db) } var r *redis.Reply if redis_is_cluster { r = rCluster.Cmd("RPOP", redis_list) } else { r = rClient.Cmd("RPOP", redis_list) } for { if redis_is_cluster { r = rCluster.Cmd("RPOP", redis_list) } else { r = rClient.Cmd("RPOP", redis_list) } t = time.Now() ts = t.Format("Mon Jan 2 15:04:05 -0700 MST 2006") // If the response from redis is actual data, process and then pop another response off // If not, who cares, go through the old switch case for r.Type == redis.BulkReply { t = time.Now() ts = t.Format("Mon Jan 2 15:04:05 -0700 MST 2006") data, err := r.Bytes() if debug { fmt.Printf("[%s] INFO BulkReply reply received. Data length %d\n", ts, len(data)) } if err != nil { fmt.Printf("[%s] ERROR Error received: %s\n", ts, err) } else { err := Send(ts, data, settings, senders) if err != nil { fmt.Printf(err.Error()) } } if redis_is_cluster { r = rCluster.Cmd("RPOP", redis_list) } else { r = rClient.Cmd("RPOP", redis_list) } } switch r.Type { case redis.ErrorReply: fmt.Printf("[%s] ERROR ErrorReply received: %s\n", ts, r.Err.Error()) case redis.NilReply: if debug { fmt.Printf("[%s] INFO NilReply reply received\n", ts) } case redis.StatusReply: if debug { fmt.Printf("[%s] INFO StatusReply reply received: not processing\n", ts) } case redis.MultiReply: if debug { fmt.Printf("[%s] INFO MultiReply reply received: not processing\n", ts) } case redis.IntegerReply: if debug { fmt.Printf("[%s] INFO IntegerReply reply received: not processing\n", ts) } default: if debug { fmt.Printf("[%s] INFO Unknown reply received: not processing\n", ts) } } time.Sleep(time.Duration(redis_watch_interval) * time.Millisecond) } }