Example #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())
}
Example #2
0
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)
}
Example #3
0
func (c *client) handle() {
	var payload []byte
	var err error
	var msgID []byte
	var cmd protocol.Command
	var conn = c.conn
	defer func() {
		if x := recover(); x != nil {
			log.Printf("[client] painc: %v\n", x)
		}
	}()
	defer conn.Close()
	for {
		payload, err = conn.Receive()
		if err != nil {
			if err != io.EOF {
				log.Printf("clientError: %s\n", err.Error())
			}
			return
		}

		msgID, cmd, payload = protocol.ParseCommand(payload)

		switch cmd {
		case protocol.SUBMITJOB:
			err = c.handleSubmitJob(msgID, payload)
			break
		case protocol.STATUS:
			err = c.handleStatus(msgID)
			break
		case protocol.PING:
			err = c.handleCommand(msgID, protocol.PONG)
			break
		case protocol.DROPFUNC:
			err = c.handleDropFunc(msgID, payload)
			break
		case protocol.REMOVEJOB:
			err = c.handleRemoveJob(msgID, payload)
			break
		case protocol.DUMP:
			err = c.handleDump(msgID)
			break
		case protocol.LOAD:
			err = c.handleLoad(msgID, payload)
			break
		default:
			err = c.handleCommand(msgID, protocol.UNKNOWN)
			break
		}
		if err != nil {
			if err != io.EOF {
				log.Printf("clientError: %s\n", err.Error())
			}
			return
		}
	}
}
Example #4
0
func (client *Client) Handle() {
	var payload []byte
	var err error
	var msgId int64
	var cmd protocol.Command
	var conn = client.conn
	defer func() {
		if x := recover(); x != nil {
			log.Printf("[Client] painc: %v\n", x)
		}
	}()
	defer conn.Close()
	for {
		payload, err = conn.Receive()
		if err != nil {
			if err != io.EOF {
				log.Printf("ClientError: %s\n", err.Error())
			}
			return
		}

		msgId, cmd, payload = protocol.ParseCommand(payload)

		switch cmd {
		case protocol.SUBMIT_JOB:
			err = client.HandleSubmitJob(msgId, payload)
			break
		case protocol.STATUS:
			err = client.HandleStatus(msgId)
			break
		case protocol.PING:
			err = client.HandleCommand(msgId, protocol.PONG)
			break
		case protocol.DROP_FUNC:
			err = client.HandleDropFunc(msgId, payload)
			break
		default:
			err = client.HandleCommand(msgId, protocol.UNKNOWN)
			break
		}
		if err != nil {
			if err != io.EOF {
				log.Printf("ClientError: %s\n", err.Error())
			}
			return
		}
	}
}
Example #5
0
func (w *worker) handle() {
	var payload []byte
	var err error
	var conn = w.conn
	var msgId []byte
	var cmd protocol.Command
	defer func() {
		if x := recover(); x != nil {
			log.Printf("[worker] painc: %v\n", x)
		}
	}()
	defer w.Close()
	for {
		payload, err = conn.Receive()
		if err != nil {
			if err != io.EOF {
				log.Printf("workerError: %s\n", err.Error())
			}
			break
		}

		msgId, cmd, payload = protocol.ParseCommand(payload)

		switch cmd {
		case protocol.GRAB_JOB:
			err = w.handleGrabJob(msgId)
			break
		case protocol.WORK_DONE:
			jobId, _ := strconv.ParseInt(string(payload), 10, 0)
			err = w.handleDone(jobId)
			break
		case protocol.WORK_FAIL:
			jobId, _ := strconv.ParseInt(string(payload), 10, 0)
			err = w.handleFail(jobId)
			break
		case protocol.SCHED_LATER:
			parts := bytes.SplitN(payload, protocol.NULL_CHAR, 2)
			if len(parts) != 2 {
				log.Printf("Error: invalid format.")
				break
			}
			jobId, _ := strconv.ParseInt(string(parts[0]), 10, 0)
			delay, _ := strconv.ParseInt(string(parts[1]), 10, 0)
			err = w.handleSchedLater(jobId, delay)
			break
		case protocol.SLEEP:
			err = w.handleCommand(msgId, protocol.NOOP)
			break
		case protocol.PING:
			err = w.handleCommand(msgId, protocol.PONG)
			break
		case protocol.CAN_DO:
			err = w.handleCanDo(string(payload))
			break
		case protocol.CANT_DO:
			err = w.handleCanNoDo(string(payload))
			break
		default:
			err = w.handleCommand(msgId, protocol.UNKNOWN)
			break
		}
		if err != nil {
			if err != io.EOF {
				log.Printf("workerError: %s\n", err.Error())
			}
			break
		}

		if !w.alive {
			break
		}
	}
}