func slaveLoop(conn redis.Conn, delegate Worker, handle string) { var redisMsg redisMessage logger := logs.NewLogger("redis.slave") logger.Info("Started slave") for { reply, err := redis.Strings(conn.Do("BLPOP", "stop-"+handle, "tasks", 0)) if len(reply) == 0 { panic("Empty task, usually means connection lost, shutting down") } if reply[0] == "stop-"+handle { conn.Do("RPUSH", "stopped-"+handle, true) break } if err == nil { redisMsg.WorkloadContext = context.New() json.Unmarshal([]byte(reply[1]), &redisMsg) go func(experiment string, replyTo string, workloadCtx context.Context) { result := delegate.Time(experiment, workloadCtx) var encoded []byte encoded, err = json.Marshal(result) logger.Debug("Completed slave task, replying") conn.Do("RPUSH", replyTo, string(encoded)) }(redisMsg.Workload, redisMsg.Reply, redisMsg.WorkloadContext) } if err != nil { logger.Warnf("ERROR: slave encountered error: %v", err) } } }
func push(c redis.Conn, guid string, sample *experiment.Sample) { json, _ := json.Marshal(sample) c.Do("RPUSH", "experiment."+guid, json) }