Upgrade to weekly.2011-12-14
This commit is contained in:
parent
5c13b0923b
commit
85a02820fc
@ -22,12 +22,12 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
log.Println(handle)
|
log.Println(handle)
|
||||||
/*job := <-client.JobQueue
|
job := <-client.JobQueue
|
||||||
if data, err := job.Result(); err != nil {
|
if data, err := job.Result(); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
log.Println(string(data))
|
log.Println(string(data))
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
known, running, numerator, denominator, err := client.Status(handle)
|
known, running, numerator, denominator, err := client.Status(handle)
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gearman"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gearman"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ToUpper(job *gearman.WorkerJob) ([]byte, os.Error) {
|
func ToUpper(job *gearman.WorkerJob) ([]byte, error) {
|
||||||
data := []byte(strings.ToUpper(string(job.Data)))
|
data := []byte(strings.ToUpper(string(job.Data)))
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,13 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"errors"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
// "log"
|
// "log"
|
||||||
"strconv"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -41,7 +42,7 @@ func NewClient() (client *Client) {
|
|||||||
// Add a server.
|
// Add a server.
|
||||||
// In this version, one client connect to one job server.
|
// In this version, one client connect to one job server.
|
||||||
// Sample is better. Plz do the load balancing by your self.
|
// 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)
|
conn, err := net.Dial(TCP, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -51,7 +52,7 @@ func (client *Client) AddServer(addr string) (err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Internal read
|
// Internal read
|
||||||
func (client *Client) read() (data []byte, err os.Error) {
|
func (client *Client) read() (data []byte, err error) {
|
||||||
if len(client.incoming) > 0 {
|
if len(client.incoming) > 0 {
|
||||||
// incoming queue is not empty
|
// incoming queue is not empty
|
||||||
data = <-client.incoming
|
data = <-client.incoming
|
||||||
@ -61,7 +62,7 @@ func (client *Client) read() (data []byte, err os.Error) {
|
|||||||
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 = client.conn.Read(buf); err != nil {
|
||||||
if err == os.EOF && n == 0 {
|
if err == io.EOF && n == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -91,13 +92,13 @@ func (client *Client) read() (data []byte, err os.Error) {
|
|||||||
end++
|
end++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.NewError("Invalid data struct.")
|
err = errors.New("Invalid data struct.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a job from job server.
|
// Read a job from job server.
|
||||||
// This function will return the job, and add it to the job queue.
|
// 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
|
var rel []byte
|
||||||
if rel, err = client.read(); err != nil {
|
if rel, err = client.read(); err != nil {
|
||||||
return
|
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,
|
// flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH,
|
||||||
// and if it is background job: JOB_BG.
|
// and if it is background job: JOB_BG.
|
||||||
// JOB_LOW | JOB_BG means the job is running with low level in background.
|
// 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
|
var datatype uint32
|
||||||
if flag&JOB_LOW == JOB_LOW {
|
if flag&JOB_LOW == JOB_LOW {
|
||||||
if flag&JOB_BG == JOB_BG {
|
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
|
// 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 {
|
for {
|
||||||
if job, err = client.ReadJob(); err != nil {
|
if job, err = client.ReadJob(); err != nil {
|
||||||
return
|
return
|
||||||
@ -189,14 +190,14 @@ func (client *Client) readLastJob(datatype uint32) (job *ClientJob, err os.Error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if job.DataType != datatype {
|
if job.DataType != datatype {
|
||||||
err = os.NewError("No job got.")
|
err = errors.New("No job got.")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get job status from job server.
|
// Get job status from job server.
|
||||||
// !!!Not fully tested.!!!
|
// !!!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 {
|
if err = client.WriteJob(NewClientJob(REQ, GET_STATUS, []byte(handle))); err != nil {
|
||||||
return
|
return
|
||||||
@ -207,26 +208,26 @@ func (client *Client) Status(handle string) (known, running bool, numerator, den
|
|||||||
}
|
}
|
||||||
data := bytes.SplitN(job.Data, []byte{'\x00'}, 5)
|
data := bytes.SplitN(job.Data, []byte{'\x00'}, 5)
|
||||||
if len(data) != 5 {
|
if len(data) != 5 {
|
||||||
err = os.NewError("Data Error.")
|
err = errors.New("Data Error.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if handle != string(data[0]) {
|
if handle != string(data[0]) {
|
||||||
err = os.NewError("Invalid handle.")
|
err = errors.New("Invalid handle.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
known = data[1][0] == '1'
|
known = data[1][0] == '1'
|
||||||
running = data[2][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
|
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
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a something out, get the samething back.
|
// 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 {
|
if err = client.WriteJob(NewClientJob(REQ, ECHO_REQ, data)); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -254,12 +255,12 @@ func (client *Client) LastJob() (job *ClientJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send the job to job server.
|
// 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())
|
return client.write(job.Encode())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal write
|
// Internal write
|
||||||
func (client *Client) write(buf []byte) (err os.Error) {
|
func (client *Client) write(buf []byte) (err 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 = client.conn.Write(buf[i:])
|
||||||
@ -271,7 +272,7 @@ func (client *Client) write(buf []byte) (err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close.
|
// Close.
|
||||||
func (client *Client) Close() (err os.Error) {
|
func (client *Client) Close() (err error) {
|
||||||
err = client.conn.Close()
|
err = client.conn.Close()
|
||||||
close(client.JobQueue)
|
close(client.JobQueue)
|
||||||
return
|
return
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
// "log"
|
// "log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,15 +25,15 @@ func NewClientJob(magiccode, datatype uint32, data []byte) (job *ClientJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decode a job from byte slice
|
// 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 {
|
if len(data) < 12 {
|
||||||
err = os.NewError("Data length is too small.")
|
err = errors.New("Data length is too small.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
||||||
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
||||||
if len(data[12:]) != int(l) {
|
if len(data[12:]) != int(l) {
|
||||||
err = os.NewError("Invalid data length.")
|
err = errors.New("Invalid data length.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data = data[12:]
|
data = data[12:]
|
||||||
@ -56,42 +56,42 @@ func (job *ClientJob) Encode() (data []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract the job's result.
|
// 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 {
|
switch job.DataType {
|
||||||
case WORK_FAIL:
|
case WORK_FAIL:
|
||||||
job.Handle = string(job.Data)
|
job.Handle = string(job.Data)
|
||||||
err = os.NewError("Work fail.")
|
err = errors.New("Work fail.")
|
||||||
return
|
return
|
||||||
case WORK_EXCEPTION:
|
case WORK_EXCEPTION:
|
||||||
err = os.NewError("Work exception.")
|
err = errors.New("Work exception.")
|
||||||
fallthrough
|
fallthrough
|
||||||
case WORK_COMPLETE:
|
case WORK_COMPLETE:
|
||||||
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
|
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
|
||||||
if len(s) != 2 {
|
if len(s) != 2 {
|
||||||
err = os.NewError("Invalid data.")
|
err = errors.New("Invalid data.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
job.Handle = string(s[0])
|
job.Handle = string(s[0])
|
||||||
data = s[1]
|
data = s[1]
|
||||||
default:
|
default:
|
||||||
err = os.NewError("The job is not a result.")
|
err = errors.New("The job is not a result.")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the job's update
|
// 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 {
|
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
|
return
|
||||||
}
|
}
|
||||||
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
|
s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
|
||||||
if len(s) != 2 {
|
if len(s) != 2 {
|
||||||
err = os.NewError("Invalid data.")
|
err = errors.New("Invalid data.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if job.DataType == WORK_WARNING {
|
if job.DataType == WORK_WARNING {
|
||||||
err = os.NewError("Work warning")
|
err = errors.New("Work warning")
|
||||||
}
|
}
|
||||||
job.Handle = string(s[0])
|
job.Handle = string(s[0])
|
||||||
data = s[1]
|
data = s[1]
|
||||||
|
@ -11,7 +11,8 @@ package gearman
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"errors"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -80,15 +81,15 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Extract the error message
|
// 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)
|
rel := bytes.SplitN(data, []byte{'\x00'}, 2)
|
||||||
if len(rel) != 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
|
return
|
||||||
}
|
}
|
||||||
l := len(rel[0])
|
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]}))
|
eno = syscall.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]))
|
err = errors.New(string(rel[1]))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,10 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
// "log"
|
// "log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,7 +21,7 @@ type jobAgent struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the agent of job server.
|
// 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)
|
conn, err := net.Dial(TCP, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -29,7 +31,7 @@ func newJobAgent(addr string, worker *Worker) (jobagent *jobAgent, err os.Error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Internal read
|
// Internal read
|
||||||
func (agent *jobAgent) read() (data []byte, err os.Error) {
|
func (agent *jobAgent) read() (data []byte, err error) {
|
||||||
if len(agent.incoming) > 0 {
|
if len(agent.incoming) > 0 {
|
||||||
// incoming queue is not empty
|
// incoming queue is not empty
|
||||||
data = <-agent.incoming
|
data = <-agent.incoming
|
||||||
@ -38,7 +40,7 @@ func (agent *jobAgent) read() (data []byte, err os.Error) {
|
|||||||
buf := make([]byte, BUFFER_SIZE)
|
buf := make([]byte, BUFFER_SIZE)
|
||||||
var n int
|
var n int
|
||||||
if n, err = agent.conn.Read(buf); err != nil {
|
if n, err = agent.conn.Read(buf); err != nil {
|
||||||
if err == os.EOF && n == 0 {
|
if err == io.EOF && n == 0 {
|
||||||
err = nil
|
err = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -68,7 +70,7 @@ func (agent *jobAgent) read() (data []byte, err os.Error) {
|
|||||||
start++
|
start++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.NewError("Invalid data struct.")
|
err = errors.New("Invalid data struct.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,12 +108,12 @@ func (agent *jobAgent) Work() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send a job to the job server.
|
// 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())
|
return agent.write(job.Encode())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal write the encoded job.
|
// 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
|
var n int
|
||||||
for i := 0; i < len(buf); i += n {
|
for i := 0; i < len(buf); i += n {
|
||||||
n, err = agent.conn.Write(buf[i:])
|
n, err = agent.conn.Write(buf[i:])
|
||||||
@ -123,7 +125,7 @@ func (agent *jobAgent) write(buf []byte) (err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close.
|
// Close.
|
||||||
func (agent *jobAgent) Close() (err os.Error) {
|
func (agent *jobAgent) Close() (err error) {
|
||||||
agent.running = false
|
agent.running = false
|
||||||
close(agent.incoming)
|
close(agent.incoming)
|
||||||
err = agent.conn.Close()
|
err = agent.conn.Close()
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The definition of the callback function.
|
// 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.
|
// Map for added function.
|
||||||
type JobFunctionMap map[string]JobFunction
|
type JobFunctionMap map[string]JobFunction
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ type Worker struct {
|
|||||||
incoming chan *WorkerJob
|
incoming chan *WorkerJob
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
JobQueue chan *WorkerJob
|
JobQueue chan *WorkerJob
|
||||||
ErrQueue chan os.Error
|
ErrQueue chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a new worker
|
// Get a new worker
|
||||||
@ -53,7 +53,7 @@ func NewWorker() (worker *Worker) {
|
|||||||
functions: make(JobFunctionMap),
|
functions: make(JobFunctionMap),
|
||||||
incoming: make(chan *WorkerJob, QUEUE_CAP),
|
incoming: make(chan *WorkerJob, QUEUE_CAP),
|
||||||
JobQueue: 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,
|
running: true,
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -61,12 +61,12 @@ func NewWorker() (worker *Worker) {
|
|||||||
|
|
||||||
// Add a server. The addr should be 'host:port' format.
|
// Add a server. The addr should be 'host:port' format.
|
||||||
// The connection is established at this time.
|
// 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()
|
worker.mutex.Lock()
|
||||||
defer worker.mutex.Unlock()
|
defer worker.mutex.Unlock()
|
||||||
|
|
||||||
if len(worker.clients) == cap(worker.clients) {
|
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
|
// 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.
|
// Plz added job servers first, then functions.
|
||||||
// The API will tell every connected job server that 'I can do this'
|
// The API will tell every connected job server that 'I can do this'
|
||||||
func (worker *Worker) AddFunction(funcname string,
|
func (worker *Worker) AddFunction(funcname string,
|
||||||
f JobFunction, timeout uint32) (err os.Error) {
|
f JobFunction, timeout uint32) (err error) {
|
||||||
if len(worker.clients) < 1 {
|
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()
|
worker.mutex.Lock()
|
||||||
defer worker.mutex.Unlock()
|
defer worker.mutex.Unlock()
|
||||||
@ -111,14 +111,14 @@ f JobFunction, timeout uint32) (err os.Error) {
|
|||||||
|
|
||||||
// Remove a function.
|
// Remove a function.
|
||||||
// Tell job servers 'I can not do this now' at the same time.
|
// 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()
|
worker.mutex.Lock()
|
||||||
defer worker.mutex.Unlock()
|
defer worker.mutex.Unlock()
|
||||||
|
|
||||||
if worker.functions[funcname] == nil {
|
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))
|
job := NewWorkerJob(REQ, CANT_DO, []byte(funcname))
|
||||||
worker.WriteJob(job)
|
worker.WriteJob(job)
|
||||||
return
|
return
|
||||||
@ -171,7 +171,7 @@ func (worker *Worker) LastJob() (job *WorkerJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close.
|
// Close.
|
||||||
func (worker *Worker) Close() (err os.Error) {
|
func (worker *Worker) Close() (err error) {
|
||||||
worker.running = false
|
worker.running = false
|
||||||
for _, v := range worker.clients {
|
for _, v := range worker.clients {
|
||||||
err = v.Close()
|
err = v.Close()
|
||||||
@ -183,8 +183,8 @@ func (worker *Worker) Close() (err os.Error) {
|
|||||||
// Write a job to job server.
|
// Write a job to job server.
|
||||||
// Here, the job's mean is not the oraginal mean.
|
// 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.
|
// 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) {
|
func (worker *Worker) WriteJob(job *WorkerJob) (err error) {
|
||||||
e := make(chan os.Error)
|
e := make(chan error)
|
||||||
for _, v := range worker.clients {
|
for _, v := range worker.clients {
|
||||||
go func() {
|
go func() {
|
||||||
e <- v.WriteJob(job)
|
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.
|
// 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)
|
job := NewWorkerJob(REQ, ECHO_REQ, data)
|
||||||
return worker.WriteJob(job)
|
return worker.WriteJob(job)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove all of functions.
|
// Remove all of functions.
|
||||||
// Both from the worker or job servers.
|
// 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)
|
job := NewWorkerJob(REQ, RESET_ABILITIES, nil)
|
||||||
err = worker.WriteJob(job)
|
err = worker.WriteJob(job)
|
||||||
worker.functions = make(JobFunctionMap)
|
worker.functions = make(JobFunctionMap)
|
||||||
@ -209,13 +209,13 @@ func (worker *Worker) Reset() (err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the worker's unique id.
|
// 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))
|
job := NewWorkerJob(REQ, SET_CLIENT_ID, []byte(id))
|
||||||
return worker.WriteJob(job)
|
return worker.WriteJob(job)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the job. And send back the result.
|
// 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
|
var limit int
|
||||||
if job.DataType == JOB_ASSIGN {
|
if job.DataType == JOB_ASSIGN {
|
||||||
limit = 3
|
limit = 3
|
||||||
@ -233,7 +233,7 @@ func (worker *Worker) exec(job *WorkerJob) (err os.Error) {
|
|||||||
}
|
}
|
||||||
f := worker.functions[funcname]
|
f := worker.functions[funcname]
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return os.NewError("function is nil")
|
return errors.New("function is nil")
|
||||||
}
|
}
|
||||||
result, err := f(job)
|
result, err := f(job)
|
||||||
var datatype uint32
|
var datatype uint32
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"testing"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
var worker *Worker
|
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
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,15 +25,15 @@ func NewWorkerJob(magiccode, datatype uint32, data []byte) (job *WorkerJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decode job from byte slice
|
// 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 {
|
if len(data) < 12 {
|
||||||
err = os.NewError("Data length is too small.")
|
err = errors.New("Data length is too small.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
datatype := byteToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
||||||
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
l := byteToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
||||||
if len(data[12:]) != int(l) {
|
if len(data[12:]) != int(l) {
|
||||||
err = os.NewError("Invalid data length.")
|
err = errors.New("Invalid data length.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data = data[12:]
|
data = data[12:]
|
||||||
@ -63,7 +63,7 @@ func (job *WorkerJob) Encode() (data []byte) {
|
|||||||
|
|
||||||
// Send some datas to client.
|
// Send some datas to client.
|
||||||
// Using this in a job's executing.
|
// 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([]byte(job.Handle), 0)
|
||||||
result = append(result, data...)
|
result = append(result, data...)
|
||||||
var datatype uint32
|
var datatype uint32
|
||||||
@ -77,7 +77,7 @@ func (job *WorkerJob) UpdateData(data []byte, iswaring bool) (err os.Error) {
|
|||||||
|
|
||||||
// Update status.
|
// Update status.
|
||||||
// Tall client how many percent job has been executed.
|
// 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))
|
n := []byte(strconv.Itoa(numerator))
|
||||||
d := []byte(strconv.Itoa(denominator))
|
d := []byte(strconv.Itoa(denominator))
|
||||||
result := append([]byte(job.Handle), 0)
|
result := append([]byte(job.Handle), 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user