gearman-go/worker/agent.go

121 lines
2.1 KiB
Go
Raw Normal View History

package worker
import (
2013-08-30 12:36:57 +08:00
"io"
"net"
)
// The agent of job server.
type agent struct {
2013-12-23 17:05:42 +08:00
conn net.Conn
worker *Worker
in chan []byte
net, addr string
}
// Create the agent of job server.
2013-08-30 18:01:10 +08:00
func newAgent(net, addr string, worker *Worker) (a *agent, err error) {
2013-08-30 12:36:57 +08:00
a = &agent{
2013-12-23 17:05:42 +08:00
net: net,
2013-08-30 12:36:57 +08:00
addr: addr,
2013-08-30 18:01:10 +08:00
worker: worker,
in: make(chan []byte, QUEUE_SIZE),
2013-08-30 12:36:57 +08:00
}
return
}
2013-08-30 18:01:10 +08:00
func (a *agent) Connect() (err error) {
a.conn, err = net.Dial(a.net, a.addr)
if err != nil {
return
2013-08-30 12:36:57 +08:00
}
2013-12-24 14:35:33 +08:00
go a.work()
2013-08-30 18:01:10 +08:00
return
}
2013-12-24 14:35:33 +08:00
func (a *agent) work() {
var inpack *inPack
2013-08-30 18:01:10 +08:00
var l int
var err error
var data, leftdata []byte
2013-12-24 14:35:33 +08:00
for {
if data, err = a.read(BUFFER_SIZE); err != nil {
if err == ErrConnClosed {
break
}
a.worker.err(err)
continue
}
2013-08-30 18:01:10 +08:00
if len(leftdata) > 0 { // some data left for processing
data = append(leftdata, data...)
2013-08-30 12:36:57 +08:00
}
if len(data) < MIN_PACKET_LEN { // not enough data
2013-08-30 18:01:10 +08:00
leftdata = data
2013-08-30 12:36:57 +08:00
continue
}
if inpack, l, err = decodeInPack(data); err != nil {
2013-08-30 12:36:57 +08:00
a.worker.err(err)
continue
}
2013-08-30 18:01:10 +08:00
leftdata = nil
inpack.a = a
a.worker.in <- inpack
2013-08-30 18:01:10 +08:00
if len(data) > l {
leftdata = data[l:]
}
}
}
2012-05-28 10:34:16 +08:00
func (a *agent) Close() {
2013-12-24 14:35:33 +08:00
a.conn.Close()
2013-12-23 17:01:01 +08:00
}
func (a *agent) Grab() {
outpack := getOutPack()
outpack.dataType = GRAB_JOB_UNIQ
a.write(outpack)
}
func (a *agent) PreSleep() {
outpack := getOutPack()
outpack.dataType = PRE_SLEEP
a.write(outpack)
2012-05-28 10:34:16 +08:00
}
2013-08-30 18:01:10 +08:00
// read length bytes from the socket
func (a *agent) read(length int) (data []byte, err error) {
2013-08-30 12:36:57 +08:00
n := 0
2013-08-30 18:01:10 +08:00
buf := getBuffer(BUFFER_SIZE)
2013-08-30 12:36:57 +08:00
// read until data can be unpacked
2013-08-30 18:01:10 +08:00
for i := length; i > 0 || len(data) < MIN_PACKET_LEN; i -= n {
2013-08-30 12:36:57 +08:00
if n, err = a.conn.Read(buf); err != nil {
if err == io.EOF && n == 0 {
if data == nil {
2013-08-30 18:01:10 +08:00
err = ErrConnection
2013-12-24 14:35:33 +08:00
} else {
err = ErrConnClosed
2013-08-30 12:36:57 +08:00
}
}
return
}
data = append(data, buf[0:n]...)
2013-08-30 18:01:10 +08:00
if n < BUFFER_SIZE {
2013-08-30 12:36:57 +08:00
break
}
}
return
2012-12-28 21:19:58 +08:00
}
// Internal write the encoded job.
func (a *agent) write(outpack *outPack) (err error) {
2013-08-30 12:36:57 +08:00
var n int
buf := outpack.Encode()
2013-08-30 12:36:57 +08:00
for i := 0; i < len(buf); i += n {
n, err = a.conn.Write(buf[i:])
if err != nil {
return err
}
}
return
}