func SubmitJob(entryPoint string, job driver.Job) { parts := strings.SplitN(entryPoint, "://", 2) c, err := net.Dial(parts[0], parts[1]) if err != nil { log.Fatal(err) } conn := protocol.Conn{Conn: c} defer conn.Close() err = conn.Send(protocol.TYPE_CLIENT.Bytes()) if err != nil { log.Fatal(err) } var msgId = []byte("100") buf := bytes.NewBuffer(nil) buf.Write(msgId) buf.Write(protocol.NULL_CHAR) buf.WriteByte(byte(protocol.SUBMIT_JOB)) buf.Write(protocol.NULL_CHAR) buf.Write(job.Bytes()) err = conn.Send(buf.Bytes()) if err != nil { log.Fatal(err) } payload, err := conn.Receive() if err != nil { log.Fatal(err) } _, cmd, _ := protocol.ParseCommand(payload) fmt.Printf("%s\n", cmd.String()) }
func DropFunc(entryPoint, Func string) { parts := strings.SplitN(entryPoint, "://", 2) c, err := net.Dial(parts[0], parts[1]) if err != nil { log.Fatal(err) } conn := protocol.Conn{Conn: c} defer conn.Close() err = conn.Send(protocol.TYPE_CLIENT.Bytes()) if err != nil { log.Fatal(err) } var msgId = []byte("100") buf := bytes.NewBuffer(nil) buf.Write(msgId) buf.Write(protocol.NULL_CHAR) buf.WriteByte(byte(protocol.DROP_FUNC)) buf.Write(protocol.NULL_CHAR) buf.WriteString(Func) err = conn.Send(buf.Bytes()) if err != nil { log.Fatal(err) } payload, err := conn.Receive() if err != nil { log.Fatal(err) } _, cmd, _ := protocol.ParseCommand(payload) fmt.Printf("%s\n", cmd) }
func (sched *Sched) HandleConnection(conn net.Conn) { c := protocol.Conn{Conn: conn} payload, err := c.Receive() if err != nil { return } switch protocol.ClientType(payload[0]) { case protocol.TYPE_CLIENT: client := NewClient(sched, c) go client.Handle() break case protocol.TYPE_WORKER: worker := NewWorker(sched, c) go worker.Handle() break default: log.Printf("Unsupport client %d\n", payload[0]) c.Close() break } }
func Run(entryPoint, Func, cmd string) { parts := strings.SplitN(entryPoint, "://", 2) for { c, err := net.Dial(parts[0], parts[1]) if err != nil { if err != io.EOF { log.Printf("Error: %s\n", err.Error()) } log.Printf("Wait 5 second to reconnecting") time.Sleep(5 * time.Second) continue } conn := protocol.Conn{Conn: c} err = handleWorker(conn, Func, cmd) if err != nil { if err != io.EOF { log.Printf("Error: %s\n", err.Error()) } } conn.Close() } }
func ShowStatus(entryPoint string) { parts := strings.SplitN(entryPoint, "://", 2) c, err := net.Dial(parts[0], parts[1]) if err != nil { log.Fatal(err) } conn := protocol.Conn{Conn: c} defer conn.Close() err = conn.Send(protocol.TYPE_CLIENT.Bytes()) if err != nil { log.Fatal(err) } var msgId = []byte("100") buf := bytes.NewBuffer(nil) buf.Write(msgId) buf.Write(protocol.NULL_CHAR) buf.Write(protocol.STATUS.Bytes()) err = conn.Send(buf.Bytes()) if err != nil { log.Fatal(err) } payload, err := conn.Receive() if err != nil { log.Fatal(err) } _parts := bytes.SplitN(payload, protocol.NULL_CHAR, 2) if len(_parts) != 2 { err := fmt.Sprint("ParseCommand InvalId %v\n", payload) panic(err) } stats := strings.Split(string(_parts[1]), "\n") for _, stat := range stats { if stat == "" { continue } line := strings.Split(stat, ",") fmt.Printf("Func: %s\tWorker: %s\tJob: %s\tProcessing: %s\n", line[0], line[1], line[2], line[3]) } }
func handleWorker(conn protocol.Conn, Func, cmd string) (err error) { err = conn.Send(protocol.TYPEWORKER.Bytes()) if err != nil { return } var msgID = []byte("100") buf := bytes.NewBuffer(nil) buf.Write(msgID) buf.Write(protocol.NullChar) buf.WriteByte(byte(protocol.CANDO)) buf.Write(protocol.NullChar) buf.WriteString(Func) err = conn.Send(buf.Bytes()) if err != nil { return } var payload []byte var job driver.Job var jobHandle []byte for { buf = bytes.NewBuffer(nil) buf.Write(msgID) buf.Write(protocol.NullChar) buf.Write(protocol.GRABJOB.Bytes()) err = conn.Send(buf.Bytes()) if err != nil { return } payload, err = conn.Receive() if err != nil { return } job, jobHandle, err = extraJob(payload) realCmd := strings.Split(cmd, " ") realCmd = append(realCmd, job.Name) c := exec.Command(realCmd[0], realCmd[1:]...) c.Stdin = strings.NewReader(job.Args) var out bytes.Buffer c.Stdout = &out c.Stderr = os.Stderr err = c.Run() var schedLater int var fail = false for { line, err := out.ReadString([]byte("\n")[0]) if err != nil { break } if strings.HasPrefix(line, "SCHEDLATER") { parts := strings.SplitN(line[:len(line)-1], " ", 2) later := strings.Trim(parts[1], " ") schedLater, _ = strconv.Atoi(later) } else if strings.HasPrefix(line, "FAIL") { fail = true } else { fmt.Print(line) } } buf = bytes.NewBuffer(nil) buf.Write(msgID) buf.Write(protocol.NullChar) if err != nil || fail { buf.WriteByte(byte(protocol.WORKFAIL)) } else if schedLater > 0 { buf.WriteByte(byte(protocol.SCHEDLATER)) } else { buf.WriteByte(byte(protocol.WORKDONE)) } buf.Write(protocol.NullChar) buf.Write(jobHandle) if schedLater > 0 { buf.Write(protocol.NullChar) buf.WriteString(strconv.Itoa(schedLater)) } err = conn.Send(buf.Bytes()) if err != nil { return } } }