rename the jobClient into jobAgent.

--HG--
rename : src/gearman/jobclient.go => src/gearman/jobagent.go
This commit is contained in:
mikespook 2011-10-11 11:20:01 +08:00
parent e34d969e47
commit 5c13b0923b
4 changed files with 36 additions and 36 deletions

View File

@ -8,7 +8,7 @@ TARG=gearman
GOFILES=\ GOFILES=\
gearman.go\ gearman.go\
workerjob.go\ workerjob.go\
jobclient.go\ jobagent.go\
worker.go\ worker.go\
clientjob.go\ clientjob.go\
client.go\ client.go\

View File

@ -10,34 +10,34 @@ import (
// "log" // "log"
) )
// The client of job server. // The agent of job server.
type jobClient struct { type jobAgent struct {
conn net.Conn conn net.Conn
worker *Worker worker *Worker
running bool running bool
incoming chan []byte incoming chan []byte
} }
// Create the client of job server. // Create the agent of job server.
func newJobClient(addr string, worker *Worker) (jobclient *jobClient, err os.Error) { func newJobAgent(addr string, worker *Worker) (jobagent *jobAgent, err os.Error) {
conn, err := net.Dial(TCP, addr) conn, err := net.Dial(TCP, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
jobclient = &jobClient{conn: conn, worker: worker, running: true, incoming: make(chan []byte, QUEUE_CAP)} jobagent = &jobAgent{conn: conn, worker: worker, running: true, incoming: make(chan []byte, QUEUE_CAP)}
return jobclient, err return jobagent, err
} }
// Internal read // Internal read
func (client *jobClient) read() (data []byte, err os.Error) { func (agent *jobAgent) read() (data []byte, err os.Error) {
if len(client.incoming) > 0 { if len(agent.incoming) > 0 {
// incoming queue is not empty // incoming queue is not empty
data = <-client.incoming data = <-agent.incoming
} else { } else {
for { for {
buf := make([]byte, BUFFER_SIZE) buf := make([]byte, BUFFER_SIZE)
var n int var n int
if n, err = client.conn.Read(buf); err != nil { if n, err = agent.conn.Read(buf); err != nil {
if err == os.EOF && n == 0 { if err == os.EOF && n == 0 {
err = nil err = nil
return return
@ -60,7 +60,7 @@ func (client *jobClient) read() (data []byte, err os.Error) {
if total == tl { if total == tl {
return return
} else { } else {
client.incoming <- data[total:] agent.incoming <- data[total:]
data = data[:total] data = data[:total]
return return
} }
@ -73,21 +73,21 @@ func (client *jobClient) read() (data []byte, err os.Error) {
} }
// Main loop. // Main loop.
func (client *jobClient) Work() { func (agent *jobAgent) Work() {
noop := true noop := true
for client.running { for agent.running {
// got noop msg and incoming queue is zero, grab job // got noop msg and incoming queue is zero, grab job
if noop && len(client.incoming) == 0 { if noop && len(agent.incoming) == 0 {
client.WriteJob(NewWorkerJob(REQ, GRAB_JOB, nil)) agent.WriteJob(NewWorkerJob(REQ, GRAB_JOB, nil))
} }
rel, err := client.read() rel, err := agent.read()
if err != nil { if err != nil {
client.worker.ErrQueue <- err agent.worker.ErrQueue <- err
continue continue
} }
job, err := DecodeWorkerJob(rel) job, err := DecodeWorkerJob(rel)
if err != nil { if err != nil {
client.worker.ErrQueue <- err agent.worker.ErrQueue <- err
continue continue
} else { } else {
switch job.DataType { switch job.DataType {
@ -95,10 +95,10 @@ func (client *jobClient) Work() {
noop = true noop = true
case NO_JOB: case NO_JOB:
noop = false noop = false
client.WriteJob(NewWorkerJob(REQ, PRE_SLEEP, nil)) agent.WriteJob(NewWorkerJob(REQ, PRE_SLEEP, nil))
case ECHO_RES, JOB_ASSIGN_UNIQ, JOB_ASSIGN: case ECHO_RES, JOB_ASSIGN_UNIQ, JOB_ASSIGN:
job.client = client job.agent = agent
client.worker.incoming <- job agent.worker.incoming <- job
} }
} }
} }
@ -106,15 +106,15 @@ func (client *jobClient) Work() {
} }
// Send a job to the job server. // Send a job to the job server.
func (client *jobClient) WriteJob(job *WorkerJob) (err os.Error) { func (agent *jobAgent) WriteJob(job *WorkerJob) (err os.Error) {
return client.write(job.Encode()) return agent.write(job.Encode())
} }
// Internal write the encoded job. // Internal write the encoded job.
func (client *jobClient) write(buf []byte) (err os.Error) { func (agent *jobAgent) write(buf []byte) (err os.Error) {
var n int var n int
for i := 0; i < len(buf); i += n { for i := 0; i < len(buf); i += n {
n, err = client.conn.Write(buf[i:]) n, err = agent.conn.Write(buf[i:])
if err != nil { if err != nil {
return err return err
} }
@ -123,9 +123,9 @@ func (client *jobClient) write(buf []byte) (err os.Error) {
} }
// Close. // Close.
func (client *jobClient) Close() (err os.Error) { func (agent *jobAgent) Close() (err os.Error) {
client.running = false agent.running = false
close(client.incoming) close(agent.incoming)
err = client.conn.Close() err = agent.conn.Close()
return return
} }

View File

@ -34,7 +34,7 @@ func foobar(job *WorkerJob) (data []byte, err os.Error) {
} }
*/ */
type Worker struct { type Worker struct {
clients []*jobClient clients []*jobAgent
functions JobFunctionMap functions JobFunctionMap
running bool running bool
@ -48,7 +48,7 @@ type Worker struct {
func NewWorker() (worker *Worker) { func NewWorker() (worker *Worker) {
worker = &Worker{ worker = &Worker{
// job server list // job server list
clients: make([]*jobClient, 0, WORKER_SERVER_CAP), clients: make([]*jobAgent, 0, WORKER_SERVER_CAP),
// function list // function list
functions: make(JobFunctionMap), functions: make(JobFunctionMap),
incoming: make(chan *WorkerJob, QUEUE_CAP), incoming: make(chan *WorkerJob, QUEUE_CAP),
@ -70,7 +70,7 @@ func (worker *Worker) AddServer(addr string) (err os.Error) {
} }
// Create a new job server's client as a agent of server // Create a new job server's client as a agent of server
server, err := newJobClient(addr, worker) server, err := newJobAgent(addr, worker)
if err != nil { if err != nil {
return err return err
} }

View File

@ -13,7 +13,7 @@ import (
type WorkerJob struct { type WorkerJob struct {
Data []byte Data []byte
Handle, UniqueId string Handle, UniqueId string
client *jobClient agent *jobAgent
magicCode, DataType uint32 magicCode, DataType uint32
} }
@ -72,7 +72,7 @@ func (job *WorkerJob) UpdateData(data []byte, iswaring bool) (err os.Error) {
} else { } else {
datatype = WORK_DATA datatype = WORK_DATA
} }
return job.client.WriteJob(NewWorkerJob(REQ, datatype, result)) return job.agent.WriteJob(NewWorkerJob(REQ, datatype, result))
} }
// Update status. // Update status.
@ -83,5 +83,5 @@ func (job *WorkerJob) UpdateStatus(numerator, denominator int) (err os.Error) {
result := append([]byte(job.Handle), 0) result := append([]byte(job.Handle), 0)
result = append(result, n...) result = append(result, n...)
result = append(result, d...) result = append(result, d...)
return job.client.WriteJob(NewWorkerJob(REQ, WORK_STATUS, result)) return job.agent.WriteJob(NewWorkerJob(REQ, WORK_STATUS, result))
} }