Exemple #1
0
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.NewClientConn(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())
}
Exemple #2
0
// DropFunc cli drop
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.NewClientConn(c)
	defer conn.Close()
	err = conn.Send(protocol.TYPECLIENT.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	var msgID = []byte("100")
	buf := bytes.NewBuffer(nil)
	buf.Write(msgID)
	buf.Write(protocol.NullChar)
	buf.WriteByte(byte(protocol.DROPFUNC))
	buf.Write(protocol.NullChar)
	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)
}
Exemple #3
0
// Dump cli dump
func Dump(entryPoint, output string) {
	parts := strings.SplitN(entryPoint, "://", 2)
	c, err := net.Dial(parts[0], parts[1])
	if err != nil {
		log.Fatal(err)
	}
	conn := protocol.NewClientConn(c)
	defer conn.Close()
	err = conn.Send(protocol.TYPECLIENT.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	var msgID = []byte("100")
	buf := bytes.NewBuffer(nil)
	buf.Write(msgID)
	buf.Write(protocol.NullChar)
	buf.Write(protocol.DUMP.Bytes())
	err = conn.Send(buf.Bytes())
	if err != nil {
		log.Fatal(err)
	}

	var fp *os.File
	if fp, err = os.Create(output); err != nil {
		log.Fatal(err)
	}

	defer fp.Close()

	for {
		payload, err := conn.Receive()
		if err != nil {
			log.Fatal(err)
		}
		_parts := bytes.SplitN(payload, protocol.NullChar, 2)
		if len(_parts) != 2 {
			err := fmt.Sprint("ParseCommand InvalID %v\n", payload)
			panic(err)
		}
		payload = _parts[1]
		if bytes.Equal(payload, []byte("EOF")) {
			break
		}
		header, _ := protocol.MakeHeader(payload)
		fp.Write(header)
		fp.Write(payload)
	}
}
Exemple #4
0
// ShowStatus cli status
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.NewClientConn(c)
	defer conn.Close()
	err = conn.Send(protocol.TYPECLIENT.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	var msgID = []byte("100")
	buf := bytes.NewBuffer(nil)
	buf.Write(msgID)
	buf.Write(protocol.NullChar)
	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.NullChar, 2)
	if len(_parts) != 2 {
		err := fmt.Sprint("ParseCommand InvalID %v\n", payload)
		panic(err)
	}
	stats := strings.Split(string(_parts[1]), "\n")
	sort.Strings(stats)
	table := uitable.New()
	table.MaxColWidth = 50

	table.AddRow("FUNCTION", "WORKERS", "JOBS", "PROCESSING")
	for _, stat := range stats {
		if stat == "" {
			continue
		}
		line := strings.Split(stat, ",")
		table.AddRow(line[0], line[1], line[2], line[3])
	}
	fmt.Println(table)
}
Exemple #5
0
// Load cli load
func Load(entryPoint, input string) {
	parts := strings.SplitN(entryPoint, "://", 2)
	c, err := net.Dial(parts[0], parts[1])
	if err != nil {
		log.Fatal(err)
	}
	conn := protocol.NewClientConn(c)
	defer conn.Close()
	err = conn.Send(protocol.TYPECLIENT.Bytes())
	if err != nil {
		log.Fatal(err)
	}

	var fp *os.File
	if fp, err = os.Open(input); err != nil {
		log.Fatal(err)
	}

	defer fp.Close()

	var msgID = []byte("100")
	for {
		payload, err := readPatch(fp)
		if err != nil {
			if err == io.EOF {
				break
			}
			log.Fatal(err)
		}

		buf := bytes.NewBuffer(nil)
		buf.Write(msgID)
		buf.Write(protocol.NullChar)
		buf.Write(protocol.LOAD.Bytes())
		buf.Write(protocol.NullChar)
		buf.Write(payload)

		err = conn.Send(buf.Bytes())
		if err != nil {
			log.Fatal(err)
		}
	}
}
Exemple #6
0
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.NewClientConn(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])
	}
}
Exemple #7
0
// Run cli run
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.NewClientConn(c)
		err = handleWorker(conn, Func, cmd)
		if err != nil {
			if err != io.EOF {
				log.Printf("Error: %s\n", err.Error())
			}
		}
		conn.Close()
	}
}