2012-03-26 13:32:59 +08:00
|
|
|
// Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
2012-05-23 17:45:52 +08:00
|
|
|
"bitbucket.org/mikespook/gearman-go/common"
|
2012-03-26 13:32:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// The agent of job server.
|
2012-05-24 16:49:35 +08:00
|
|
|
type agent struct {
|
|
|
|
conn net.Conn
|
|
|
|
worker *Worker
|
|
|
|
in chan []byte
|
|
|
|
out chan *Job
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the agent of job server.
|
2012-05-24 16:49:35 +08:00
|
|
|
func newAgent(addr string, worker *Worker) (a *agent, err error) {
|
2012-05-23 17:45:52 +08:00
|
|
|
conn, err := net.Dial(common.NETWORK, addr)
|
2012-03-26 13:32:59 +08:00
|
|
|
if err != nil {
|
2012-05-24 16:49:35 +08:00
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
a = &agent{
|
2012-05-23 17:45:52 +08:00
|
|
|
conn: conn,
|
|
|
|
worker: worker,
|
|
|
|
in: make(chan []byte, common.QUEUE_SIZE),
|
2012-05-24 16:49:35 +08:00
|
|
|
out: make(chan *Job, common.QUEUE_SIZE),
|
2012-05-23 17:45:52 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputing loop
|
|
|
|
func (a *agent) outLoop() {
|
|
|
|
ok := true
|
|
|
|
for ok {
|
|
|
|
if job, ok := <-a.out; ok {
|
|
|
|
if err := a.write(job.Encode()); err != nil {
|
|
|
|
a.worker.err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputing loop
|
|
|
|
func (a *agent) inLoop() {
|
|
|
|
defer func() {
|
2012-05-28 10:34:16 +08:00
|
|
|
recover()
|
2012-05-24 16:49:35 +08:00
|
|
|
close(a.in)
|
|
|
|
close(a.out)
|
|
|
|
a.worker.removeAgent(a)
|
|
|
|
}()
|
|
|
|
noop := true
|
|
|
|
for a.worker.running {
|
|
|
|
// got noop msg and in queue is zero, grab job
|
|
|
|
if noop && len(a.in) == 0 {
|
|
|
|
a.WriteJob(newJob(common.REQ, common.GRAB_JOB, nil))
|
|
|
|
}
|
|
|
|
rel, err := a.read()
|
|
|
|
if err != nil {
|
|
|
|
if err == common.ErrEmptyReading {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
a.worker.err(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
job, err := decodeJob(rel)
|
|
|
|
if err != nil {
|
|
|
|
a.worker.err(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch job.DataType {
|
|
|
|
case common.NOOP:
|
|
|
|
noop = true
|
|
|
|
case common.NO_JOB:
|
|
|
|
noop = false
|
|
|
|
a.WriteJob(newJob(common.REQ, common.PRE_SLEEP, nil))
|
|
|
|
case common.ERROR, common.ECHO_RES, common.JOB_ASSIGN_UNIQ, common.JOB_ASSIGN:
|
|
|
|
job.agent = a
|
|
|
|
a.worker.in <- job
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-28 10:34:16 +08:00
|
|
|
func (a *agent) Close() {
|
|
|
|
a.conn.Close()
|
|
|
|
}
|
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
func (a *agent) Work() {
|
|
|
|
go a.outLoop()
|
|
|
|
go a.inLoop()
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal read
|
2012-05-24 16:49:35 +08:00
|
|
|
func (a *agent) read() (data []byte, err error) {
|
|
|
|
if len(a.in) > 0 {
|
2012-05-23 17:45:52 +08:00
|
|
|
// in queue is not empty
|
2012-05-24 16:49:35 +08:00
|
|
|
data = <-a.in
|
2012-03-26 13:32:59 +08:00
|
|
|
} else {
|
|
|
|
for {
|
2012-05-23 17:45:52 +08:00
|
|
|
buf := make([]byte, common.BUFFER_SIZE)
|
2012-03-26 13:32:59 +08:00
|
|
|
var n int
|
2012-05-24 16:49:35 +08:00
|
|
|
if n, err = a.conn.Read(buf); err != nil {
|
2012-03-26 13:32:59 +08:00
|
|
|
if err == io.EOF && n == 0 {
|
2012-05-24 16:49:35 +08:00
|
|
|
if data == nil {
|
|
|
|
err = common.ErrEmptyReading
|
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data = append(data, buf[0:n]...)
|
2012-05-23 17:45:52 +08:00
|
|
|
if n < common.BUFFER_SIZE {
|
2012-03-26 13:32:59 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// split package
|
|
|
|
tl := len(data)
|
2012-05-24 16:49:35 +08:00
|
|
|
start := 0
|
2012-03-26 13:32:59 +08:00
|
|
|
for i := 0; i < tl; i++ {
|
2012-05-23 17:45:52 +08:00
|
|
|
if string(data[start:start+4]) == common.RES_STR {
|
2012-05-24 16:49:35 +08:00
|
|
|
l := int(common.BytesToUint32([4]byte{data[start+8],
|
|
|
|
data[start+9], data[start+10], data[start+11]}))
|
2012-03-26 13:32:59 +08:00
|
|
|
total := l + 12
|
|
|
|
if total == tl {
|
|
|
|
return
|
|
|
|
} else {
|
2012-05-24 16:49:35 +08:00
|
|
|
a.in <- data[total:]
|
2012-03-26 13:32:59 +08:00
|
|
|
data = data[:total]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start++
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
return nil, common.Errorf("Invalid data: %V", data)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a job to the job server.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (a *agent) WriteJob(job *Job) {
|
|
|
|
a.out <- job
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal write the encoded job.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (a *agent) write(buf []byte) (err error) {
|
2012-03-26 13:32:59 +08:00
|
|
|
var n int
|
|
|
|
for i := 0; i < len(buf); i += n {
|
2012-05-24 16:49:35 +08:00
|
|
|
n, err = a.conn.Write(buf[i:])
|
2012-03-26 13:32:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|