Upgrade to weekly.2011-12-14

This commit is contained in:
mikespook 2011-12-28 17:59:44 +08:00
parent 5c13b0923b
commit 85a02820fc
9 changed files with 754 additions and 754 deletions

View File

@ -22,12 +22,12 @@ func main() {
log.Fatalln(err)
} else {
log.Println(handle)
/*job := <-client.JobQueue
job := <-client.JobQueue
if data, err := job.Result(); err != nil {
log.Fatalln(err)
} else {
log.Println(string(data))
}*/
}
}
known, running, numerator, denominator, err := client.Status(handle)

View File

@ -1,14 +1,13 @@
package main
import (
"gearman"
"fmt"
"gearman"
"log"
"os"
"strings"
)
func ToUpper(job *gearman.WorkerJob) ([]byte, os.Error) {
func ToUpper(job *gearman.WorkerJob) ([]byte, error) {
data := []byte(strings.ToUpper(string(job.Data)))
return data, nil
}

View File

@ -5,12 +5,13 @@
package gearman
import (
"os"
"errors"
"io"
"net"
"sync"
// "log"
"strconv"
"bytes"
"strconv"
)
/*
@ -41,7 +42,7 @@ func NewClient() (client *Client) {
// Add a server.
// In this version, one client connect to one job server.
// Sample is better. Plz do the load balancing by your self.
func (client *Client) AddServer(addr string) (err os.Error) {
func (client *Client) AddServer(addr string) (err error) {
conn, err := net.Dial(TCP, addr)
if err != nil {
return
@ -51,7 +52,7 @@ func (client *Client) AddServer(addr string) (err os.Error) {
}
// Internal read
func (client *Client) read() (data []byte, err os.Error) {
func (client *Client) read() (data []byte, err error) {
if len(client.incoming) > 0 {
// incoming queue is not empty
data = <-client.incoming
@ -61,7 +62,7 @@ func (client *Client) read() (data []byte, err os.Error) {
buf := make([]byte, BUFFER_SIZE)
var n int
if n, err = client.conn.Read(buf); err != nil {
if err == os.EOF && n == 0 {
if err == io.EOF && n == 0 {
break
}
return
@ -91,13 +92,13 @@ func (client *Client) read() (data []byte, err os.Error) {
end++
}
}
err = os.NewError("Invalid data struct.")
err = errors.New("Invalid data struct.")
return
}
// Read a job from job server.
// This function will return the job, and add it to the job queue.
func (client *Client) ReadJob() (job *ClientJob, err os.Error) {
func (client *Client) ReadJob() (job *ClientJob, err error) {
var rel []byte
if rel, err = client.read(); err != nil {
return
@ -122,7 +123,7 @@ func (client *Client) ReadJob() (job *ClientJob, err os.Error) {
// flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH,
// and if it is background job: JOB_BG.
// JOB_LOW | JOB_BG means the job is running with low level in background.
func (client *Client) Do(funcname string, data []byte, flag byte) (handle string, err os.Error) {
func (client *Client) Do(funcname string, data []byte, flag byte) (handle string, err error) {
var datatype uint32
if flag&JOB_LOW == JOB_LOW {
if flag&JOB_BG == JOB_BG {
@ -179,7 +180,7 @@ func (client *Client) Do(funcname string, data []byte, flag byte) (handle string
}
// Internal read last job
func (client *Client) readLastJob(datatype uint32) (job *ClientJob, err os.Error) {
func (client *Client) readLastJob(datatype uint32) (job *ClientJob, err error) {
for {
if job, err = client.ReadJob(); err != nil {
return
@ -189,14 +190,14 @@ func (client *Client) readLastJob(datatype uint32) (job *ClientJob, err os.Error
}
}
if job.DataType != datatype {
err = os.NewError("No job got.")
err = errors.New("No job got.")
}
return
}
// Get job status from job server.
// !!!Not fully tested.!!!
func (client *Client) Status(handle string) (known, running bool, numerator, denominator uint, err os.Error) {
func (client *Client) Status(handle string) (known, running bool, numerator, denominator uint64, err error) {
if err = client.WriteJob(NewClientJob(REQ, GET_STATUS, []byte(handle))); err != nil {
return
@ -207,26 +208,26 @@ func (client *Client) Status(handle string) (known, running bool, numerator, den
}
data := bytes.SplitN(job.Data, []byte{'\x00'}, 5)
if len(data) != 5 {
err = os.NewError("Data Error.")
err = errors.New("Data Error.")
return
}
if handle != string(data[0]) {
err = os.NewError("Invalid handle.")
err = errors.New("Invalid handle.")
return
}
known = data[1][0] == '1'
running = data[2][0] == '1'
if numerator, err = strconv.Atoui(string(data[3][0])); err != nil {
if numerator, err = strconv.ParseUint(string(data[3][0]), 10, 0); err != nil {
return
}
if denominator, err = strconv.Atoui(string(data[4][0])); err != nil {
if denominator, err = strconv.ParseUint(string(data[4][0]), 10, 0); err != nil {
return
}
return
}
// Send a something out, get the samething back.
func (client *Client) Echo(data []byte) (echo []byte, err os.Error) {
func (client *Client) Echo(data []byte) (echo []byte, err error) {
if err = client.WriteJob(NewClientJob(REQ, ECHO_REQ, data)); err != nil {
return
}
@ -254,12 +255,12 @@ func (client *Client) LastJob() (job *ClientJob) {
}
// Send the job to job server.
func (client *Client) WriteJob(job *ClientJob) (err os.Error) {
func (client *Client) WriteJob(job *ClientJob) (err error) {
return client.write(job.Encode())
}
// Internal write
func (client *Client) write(buf []byte) (err os.Error) {
func (client *Client) write(buf []byte) (err error) {
var n int
for i := 0; i < len(buf); i += n {
n, err = client.conn.Write(buf[i:])
@ -271,7 +272,7 @@ func (client *Client) write(buf []byte) (err os.Error) {
}
// Close.
func (client *Client) Close() (err os.Error) {
func (client *Client) Close() (err error) {
err = client.conn.Close()
close(client.JobQueue)
return

View File

@ -5,8 +5,8 @@
package gearman
import (
"os"
"bytes"
"errors"
// "log"
)
@ -25,15 +25,15 @@ func NewClientJob(magiccode, datatype uint32, data []byte) (job *ClientJob) {
}
// Decode a job from byte slice
func DecodeClientJob(data []byte) (job *ClientJob, err os.Error) {
func DecodeClientJob(data []byte) (job *ClientJob, err error) {
if len(data) < 12 {
err = os.NewError("Data length is too small.")
err = errors.New("Data length is too small.")
return
}
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
if len(data[12:]) != int(l) {
err = os.NewError("Invalid data length.")
err = errors.New("Invalid data length.")
return
}
data = data[12:]
@ -56,42 +56,42 @@ func (job *ClientJob) Encode() (data []byte) {
}
// Extract the job's result.
func (job *ClientJob) Result() (data []byte, err os.Error) {
func (job *ClientJob) Result() (data []byte, err error) {
switch job.DataType {
case WORK_FAIL:
job.Handle = string(job.Data)
err = os.NewError("Work fail.")
err = errors.New("Work fail.")
return
case WORK_EXCEPTION:
err = os.NewError("Work exception.")
err = errors.New("Work exception.")
fallthrough
case WORK_COMPLETE:
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
if len(s) != 2 {
err = os.NewError("Invalid data.")
err = errors.New("Invalid data.")
return
}
job.Handle = string(s[0])
data = s[1]
default:
err = os.NewError("The job is not a result.")
err = errors.New("The job is not a result.")
}
return
}
// Extract the job's update
func (job *ClientJob) Update() (data []byte, err os.Error) {
func (job *ClientJob) Update() (data []byte, err error) {
if job.DataType != WORK_DATA && job.DataType != WORK_WARNING {
err = os.NewError("The job is not a update.")
err = errors.New("The job is not a update.")
return
}
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
if len(s) != 2 {
err = os.NewError("Invalid data.")
err = errors.New("Invalid data.")
return
}
if job.DataType == WORK_WARNING {
err = os.NewError("Work warning")
err = errors.New("Work warning")
}
job.Handle = string(s[0])
data = s[1]

View File

@ -11,7 +11,8 @@ package gearman
import (
"bytes"
"os"
"errors"
"syscall"
)
const (
@ -80,15 +81,15 @@ const (
)
// Extract the error message
func getError(data []byte) (eno os.Errno, err os.Error) {
func getError(data []byte) (eno syscall.Errno, err error) {
rel := bytes.SplitN(data, []byte{'\x00'}, 2)
if len(rel) != 2 {
err = os.NewError("The input is not a error data.")
err = errors.New("The input is not a error data.")
return
}
l := len(rel[0])
eno = os.Errno(byteToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l-1]}))
err = os.NewError(string(rel[1]))
eno = syscall.Errno(byteToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l-1]}))
err = errors.New(string(rel[1]))
return
}

View File

@ -5,8 +5,10 @@
package gearman
import (
"errors"
"io"
"net"
"os"
// "log"
)
@ -19,7 +21,7 @@ type jobAgent struct {
}
// Create the agent of job server.
func newJobAgent(addr string, worker *Worker) (jobagent *jobAgent, err os.Error) {
func newJobAgent(addr string, worker *Worker) (jobagent *jobAgent, err error) {
conn, err := net.Dial(TCP, addr)
if err != nil {
return nil, err
@ -29,7 +31,7 @@ func newJobAgent(addr string, worker *Worker) (jobagent *jobAgent, err os.Error)
}
// Internal read
func (agent *jobAgent) read() (data []byte, err os.Error) {
func (agent *jobAgent) read() (data []byte, err error) {
if len(agent.incoming) > 0 {
// incoming queue is not empty
data = <-agent.incoming
@ -38,7 +40,7 @@ func (agent *jobAgent) read() (data []byte, err os.Error) {
buf := make([]byte, BUFFER_SIZE)
var n int
if n, err = agent.conn.Read(buf); err != nil {
if err == os.EOF && n == 0 {
if err == io.EOF && n == 0 {
err = nil
return
}
@ -68,7 +70,7 @@ func (agent *jobAgent) read() (data []byte, err os.Error) {
start++
}
}
err = os.NewError("Invalid data struct.")
err = errors.New("Invalid data struct.")
return
}
@ -106,12 +108,12 @@ func (agent *jobAgent) Work() {
}
// Send a job to the job server.
func (agent *jobAgent) WriteJob(job *WorkerJob) (err os.Error) {
func (agent *jobAgent) WriteJob(job *WorkerJob) (err error) {
return agent.write(job.Encode())
}
// Internal write the encoded job.
func (agent *jobAgent) write(buf []byte) (err os.Error) {
func (agent *jobAgent) write(buf []byte) (err error) {
var n int
for i := 0; i < len(buf); i += n {
n, err = agent.conn.Write(buf[i:])
@ -123,7 +125,7 @@ func (agent *jobAgent) write(buf []byte) (err os.Error) {
}
// Close.
func (agent *jobAgent) Close() (err os.Error) {
func (agent *jobAgent) Close() (err error) {
agent.running = false
close(agent.incoming)
err = agent.conn.Close()

View File

@ -5,13 +5,13 @@
package gearman
import (
"os"
"sync"
"bytes"
"errors"
"sync"
)
// The definition of the callback function.
type JobFunction func(job *WorkerJob) ([]byte, os.Error)
type JobFunction func(job *WorkerJob) ([]byte, error)
// Map for added function.
type JobFunctionMap map[string]JobFunction
@ -41,7 +41,7 @@ type Worker struct {
incoming chan *WorkerJob
mutex sync.Mutex
JobQueue chan *WorkerJob
ErrQueue chan os.Error
ErrQueue chan error
}
// Get a new worker
@ -53,7 +53,7 @@ func NewWorker() (worker *Worker) {
functions: make(JobFunctionMap),
incoming: make(chan *WorkerJob, QUEUE_CAP),
JobQueue: make(chan *WorkerJob, QUEUE_CAP),
ErrQueue: make(chan os.Error, QUEUE_CAP),
ErrQueue: make(chan error, QUEUE_CAP),
running: true,
}
return
@ -61,12 +61,12 @@ func NewWorker() (worker *Worker) {
// Add a server. The addr should be 'host:port' format.
// The connection is established at this time.
func (worker *Worker) AddServer(addr string) (err os.Error) {
func (worker *Worker) AddServer(addr string) (err error) {
worker.mutex.Lock()
defer worker.mutex.Unlock()
if len(worker.clients) == cap(worker.clients) {
return os.NewError("To many servers added.")
return errors.New("To many servers added.")
}
// Create a new job server's client as a agent of server
@ -85,9 +85,9 @@ func (worker *Worker) AddServer(addr string) (err os.Error) {
// Plz added job servers first, then functions.
// The API will tell every connected job server that 'I can do this'
func (worker *Worker) AddFunction(funcname string,
f JobFunction, timeout uint32) (err os.Error) {
f JobFunction, timeout uint32) (err error) {
if len(worker.clients) < 1 {
return os.NewError("Did not connect to Job Server.")
return errors.New("Did not connect to Job Server.")
}
worker.mutex.Lock()
defer worker.mutex.Unlock()
@ -111,14 +111,14 @@ f JobFunction, timeout uint32) (err os.Error) {
// Remove a function.
// Tell job servers 'I can not do this now' at the same time.
func (worker *Worker) RemoveFunction(funcname string) (err os.Error) {
func (worker *Worker) RemoveFunction(funcname string) (err error) {
worker.mutex.Lock()
defer worker.mutex.Unlock()
if worker.functions[funcname] == nil {
return os.NewError("No function named: " + funcname)
return errors.New("No function named: " + funcname)
}
worker.functions[funcname] = nil, false
delete(worker.functions, funcname)
job := NewWorkerJob(REQ, CANT_DO, []byte(funcname))
worker.WriteJob(job)
return
@ -171,7 +171,7 @@ func (worker *Worker) LastJob() (job *WorkerJob) {
}
// Close.
func (worker *Worker) Close() (err os.Error) {
func (worker *Worker) Close() (err error) {
worker.running = false
for _, v := range worker.clients {
err = v.Close()
@ -183,8 +183,8 @@ func (worker *Worker) Close() (err os.Error) {
// Write a job to job server.
// Here, the job's mean is not the oraginal mean.
// Just looks like a network package for job's result or tell job server, there was a fail.
func (worker *Worker) WriteJob(job *WorkerJob) (err os.Error) {
e := make(chan os.Error)
func (worker *Worker) WriteJob(job *WorkerJob) (err error) {
e := make(chan error)
for _, v := range worker.clients {
go func() {
e <- v.WriteJob(job)
@ -194,14 +194,14 @@ func (worker *Worker) WriteJob(job *WorkerJob) (err os.Error) {
}
// Send a something out, get the samething back.
func (worker *Worker) Echo(data []byte) (err os.Error) {
func (worker *Worker) Echo(data []byte) (err error) {
job := NewWorkerJob(REQ, ECHO_REQ, data)
return worker.WriteJob(job)
}
// Remove all of functions.
// Both from the worker or job servers.
func (worker *Worker) Reset() (err os.Error) {
func (worker *Worker) Reset() (err error) {
job := NewWorkerJob(REQ, RESET_ABILITIES, nil)
err = worker.WriteJob(job)
worker.functions = make(JobFunctionMap)
@ -209,13 +209,13 @@ func (worker *Worker) Reset() (err os.Error) {
}
// Set the worker's unique id.
func (worker *Worker) SetId(id string) (err os.Error) {
func (worker *Worker) SetId(id string) (err error) {
job := NewWorkerJob(REQ, SET_CLIENT_ID, []byte(id))
return worker.WriteJob(job)
}
// Execute the job. And send back the result.
func (worker *Worker) exec(job *WorkerJob) (err os.Error) {
func (worker *Worker) exec(job *WorkerJob) (err error) {
var limit int
if job.DataType == JOB_ASSIGN {
limit = 3
@ -233,7 +233,7 @@ func (worker *Worker) exec(job *WorkerJob) (err os.Error) {
}
f := worker.functions[funcname]
if f == nil {
return os.NewError("function is nil")
return errors.New("function is nil")
}
result, err := f(job)
var datatype uint32

View File

@ -1,9 +1,6 @@
package gearman
import (
"testing"
"os"
)
import "testing"
var worker *Worker
@ -23,7 +20,7 @@ func TestWorkerAddServer(t *testing.T) {
}
}
func foobar(job *WorkerJob) ([]byte, os.Error) {
func foobar(job *WorkerJob) ([]byte, error) {
return nil, nil
}

View File

@ -5,7 +5,7 @@
package gearman
import (
"os"
"errors"
"strconv"
)
@ -25,15 +25,15 @@ func NewWorkerJob(magiccode, datatype uint32, data []byte) (job *WorkerJob) {
}
// Decode job from byte slice
func DecodeWorkerJob(data []byte) (job *WorkerJob, err os.Error) {
func DecodeWorkerJob(data []byte) (job *WorkerJob, err error) {
if len(data) < 12 {
err = os.NewError("Data length is too small.")
err = errors.New("Data length is too small.")
return
}
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
if len(data[12:]) != int(l) {
err = os.NewError("Invalid data length.")
err = errors.New("Invalid data length.")
return
}
data = data[12:]
@ -63,7 +63,7 @@ func (job *WorkerJob) Encode() (data []byte) {
// Send some datas to client.
// Using this in a job's executing.
func (job *WorkerJob) UpdateData(data []byte, iswaring bool) (err os.Error) {
func (job *WorkerJob) UpdateData(data []byte, iswaring bool) (err error) {
result := append([]byte(job.Handle), 0)
result = append(result, data...)
var datatype uint32
@ -77,7 +77,7 @@ func (job *WorkerJob) UpdateData(data []byte, iswaring bool) (err os.Error) {
// Update status.
// Tall client how many percent job has been executed.
func (job *WorkerJob) UpdateStatus(numerator, denominator int) (err os.Error) {
func (job *WorkerJob) UpdateStatus(numerator, denominator int) (err error) {
n := []byte(strconv.Itoa(numerator))
d := []byte(strconv.Itoa(denominator))
result := append([]byte(job.Handle), 0)