go fmt
This commit is contained in:
parent
2a27eca7b7
commit
124e686699
@ -265,7 +265,7 @@ func (client *Client) Status(handle string) (status *Status, err error) {
|
||||
wg.Add(1)
|
||||
client.mutex.Lock()
|
||||
client.lastcall = "s" + handle
|
||||
client.innerHandler["s" + handle] = ResponseHandler(func(resp *Response) {
|
||||
client.innerHandler["s"+handle] = ResponseHandler(func(resp *Response) {
|
||||
defer wg.Done()
|
||||
defer client.mutex.Unlock()
|
||||
var err error
|
||||
|
@ -6,9 +6,9 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -40,3 +40,6 @@ func GetError(data []byte) (err error) {
|
||||
err = errors.New(fmt.Sprintf("%s: %s", rel[0], rel[1]))
|
||||
return
|
||||
}
|
||||
|
||||
// Error handler
|
||||
type ErrorHandler func(error)
|
||||
|
@ -1,11 +0,0 @@
|
||||
package client
|
||||
|
||||
// Response handler
|
||||
type ResponseHandler func(*Response)
|
||||
|
||||
// Error handler
|
||||
type ErrorHandler func(error)
|
||||
|
||||
// Status handler
|
||||
// handle, known, running, numerator, denominator
|
||||
type StatusHandler func(string, bool, bool, uint64, uint64)
|
@ -1,9 +1,9 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"time"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -40,7 +40,7 @@ func TestPoolEcho(t *testing.T) {
|
||||
|
||||
func TestPoolDoBg(t *testing.T) {
|
||||
addr, handle, err := pool.DoBg("ToUpper",
|
||||
[]byte("abcdef"), JOB_LOW);
|
||||
[]byte("abcdef"), JOB_LOW)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
|
@ -6,12 +6,15 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Response handler
|
||||
type ResponseHandler func(*Response)
|
||||
|
||||
// response
|
||||
type Response struct {
|
||||
DataType uint32
|
||||
|
@ -1,5 +1,9 @@
|
||||
package client
|
||||
|
||||
// Status handler
|
||||
// handle, known, running, numerator, denominator
|
||||
type StatusHandler func(string, bool, bool, uint64, uint64)
|
||||
|
||||
type Status struct {
|
||||
Handle string
|
||||
Known, Running bool
|
||||
|
@ -1,54 +0,0 @@
|
||||
// Copyright 2011 - 2012 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 common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"errors"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrJobTimeOut = errors.New("Do a job time out")
|
||||
ErrInvalidData = errors.New("Invalid data")
|
||||
ErrWorkWarning = errors.New("Work warning")
|
||||
ErrWorkFail = errors.New("Work fail")
|
||||
ErrWorkException = errors.New("Work exeption")
|
||||
ErrDataType = errors.New("Invalid data type")
|
||||
ErrOutOfCap = errors.New("Out of the capability")
|
||||
ErrNotConn = errors.New("Did not connect to job server")
|
||||
ErrFuncNotFound = errors.New("The function was not found")
|
||||
ErrConnection = errors.New("Connection error")
|
||||
ErrNoActiveAgent = errors.New("No active agent")
|
||||
ErrTimeOut = errors.New("Executing time out")
|
||||
ErrUnknown = errors.New("Unknown error")
|
||||
ErrConnClosed = errors.New("Connection closed")
|
||||
)
|
||||
func DisablePanic() {recover()}
|
||||
|
||||
// Extract the error message
|
||||
func GetError(data []byte) (eno syscall.Errno, err error) {
|
||||
rel := bytes.SplitN(data, []byte{'\x00'}, 2)
|
||||
if len(rel) != 2 {
|
||||
err = Errorf("Not a error data: %V", data)
|
||||
return
|
||||
}
|
||||
l := len(rel[0])
|
||||
eno = syscall.Errno(BytesToUint32([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
|
||||
}
|
||||
|
||||
// Get a formated error
|
||||
func Errorf(format string, msg ... interface{}) error {
|
||||
return errors.New(fmt.Sprintf(format, msg ... ))
|
||||
}
|
||||
|
||||
// An error handler
|
||||
type ErrorHandler func(error)
|
||||
|
||||
|
@ -1,86 +0,0 @@
|
||||
// Copyright 2011 - 2012 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 common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
const (
|
||||
NETWORK = "tcp"
|
||||
// queue size
|
||||
QUEUE_SIZE = 8
|
||||
// read buffer size
|
||||
BUFFER_SIZE = 1024
|
||||
// min packet length
|
||||
PACKET_LEN = 12
|
||||
|
||||
// \x00REQ
|
||||
REQ = 5391697
|
||||
REQ_STR = "\x00REQ"
|
||||
// \x00RES
|
||||
RES = 5391699
|
||||
RES_STR = "\x00RES"
|
||||
|
||||
// package data type
|
||||
CAN_DO = 1
|
||||
CANT_DO = 2
|
||||
RESET_ABILITIES = 3
|
||||
PRE_SLEEP = 4
|
||||
NOOP = 6
|
||||
JOB_CREATED = 8
|
||||
GRAB_JOB = 9
|
||||
NO_JOB = 10
|
||||
JOB_ASSIGN = 11
|
||||
WORK_STATUS = 12
|
||||
WORK_COMPLETE = 13
|
||||
WORK_FAIL = 14
|
||||
GET_STATUS = 15
|
||||
ECHO_REQ = 16
|
||||
ECHO_RES = 17
|
||||
ERROR = 19
|
||||
STATUS_RES = 20
|
||||
SET_CLIENT_ID = 22
|
||||
CAN_DO_TIMEOUT = 23
|
||||
WORK_EXCEPTION = 25
|
||||
WORK_DATA = 28
|
||||
WORK_WARNING = 29
|
||||
GRAB_JOB_UNIQ = 30
|
||||
JOB_ASSIGN_UNIQ = 31
|
||||
|
||||
SUBMIT_JOB = 7
|
||||
SUBMIT_JOB_BG = 18
|
||||
SUBMIT_JOB_HIGH = 21
|
||||
SUBMIT_JOB_HIGH_BG = 32
|
||||
SUBMIT_JOB_LOW = 33
|
||||
SUBMIT_JOB_LOW_BG = 34
|
||||
)
|
||||
|
||||
// Decode [4]byte to uint32
|
||||
func BytesToUint32(buf [4]byte) uint32 {
|
||||
var r uint32
|
||||
b := bytes.NewBuffer(buf[:])
|
||||
err := binary.Read(b, binary.BigEndian, &r)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// Encode uint32 to [4]byte
|
||||
func Uint32ToBytes(i uint32) [4]byte {
|
||||
buf := new(bytes.Buffer)
|
||||
err := binary.Write(buf, binary.BigEndian, i)
|
||||
if err != nil {
|
||||
return [4]byte{0, 0, 0, 0}
|
||||
}
|
||||
var r [4]byte
|
||||
for k, v := range buf.Bytes() {
|
||||
r[k] = v
|
||||
}
|
||||
return r
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
testCase = map[uint32][4]byte {
|
||||
0: [...]byte{0, 0, 0, 0},
|
||||
1: [...]byte{0, 0, 0, 1},
|
||||
256: [...]byte{0, 0, 1, 0},
|
||||
256 * 256: [...]byte{0, 1, 0, 0},
|
||||
256 * 256 * 256: [...]byte{1, 0, 0, 0},
|
||||
256 * 256 * 256 + 256 * 256 + 256 + 1: [...]byte{1, 1, 1, 1},
|
||||
4294967295 : [...]byte{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
}
|
||||
)
|
||||
|
||||
func TestUint32ToBytes(t *testing.T) {
|
||||
for k, v := range testCase {
|
||||
b := Uint32ToBytes(k)
|
||||
if bytes.Compare(b[:], v[:]) != 0 {
|
||||
t.Errorf("%v was expected, but %v was got", v, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesToUint32s(t *testing.T) {
|
||||
for k, v := range testCase {
|
||||
u := BytesToUint32([4]byte(v))
|
||||
if u != k {
|
||||
t.Errorf("%v was expected, but %v was got", k, u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkByteToUnit32(b * testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
BytesToUint32([4]byte{0xF, 0xF, 0xF, 0xF});
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUint32ToByte(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Uint32ToBytes(123456);
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ The protocol was implemented by native way.
|
||||
package gearman
|
||||
|
||||
import (
|
||||
_ "github.com/mikespook/gearman-go/common"
|
||||
_ "github.com/mikespook/gearman-go/client"
|
||||
_ "github.com/mikespook/gearman-go/common"
|
||||
_ "github.com/mikespook/gearman-go/worker"
|
||||
)
|
||||
|
@ -10,15 +10,15 @@ The protocol was implemented by native way.
|
||||
package gearman
|
||||
|
||||
import (
|
||||
"time"
|
||||
"sync"
|
||||
"testing"
|
||||
"strings"
|
||||
"github.com/mikespook/gearman-go/client"
|
||||
"github.com/mikespook/gearman-go/worker"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const(
|
||||
const (
|
||||
STR = "The gearman-go is a pure go implemented library."
|
||||
GEARMAND = "127.0.0.1:4730"
|
||||
)
|
||||
@ -33,7 +33,6 @@ func Sleep(job *worker.Job) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
func TestJobs(t *testing.T) {
|
||||
w := worker.New(worker.Unlimited)
|
||||
if err := w.AddServer(GEARMAND); err != nil {
|
||||
@ -72,7 +71,7 @@ func TestJobs(t *testing.T) {
|
||||
var w sync.WaitGroup
|
||||
jobHandler := func(job *client.Response) {
|
||||
upper := strings.ToUpper(STR)
|
||||
if (string(job.Data) != upper) {
|
||||
if string(job.Data) != upper {
|
||||
t.Errorf("%s expected, got %s", upper, job.Data)
|
||||
}
|
||||
w.Done()
|
||||
|
@ -7,7 +7,6 @@ package worker
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"github.com/mikespook/gearman-go/common"
|
||||
)
|
||||
|
||||
// The agent of job server.
|
||||
|
56
worker/common.go
Normal file
56
worker/common.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2011 - 2012 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
|
||||
|
||||
const (
|
||||
NETWORK = "tcp"
|
||||
// queue size
|
||||
QUEUE_SIZE = 8
|
||||
// read buffer size
|
||||
BUFFER_SIZE = 1024
|
||||
// min packet length
|
||||
MIN_PACKET_LEN = 12
|
||||
|
||||
// \x00REQ
|
||||
REQ = 5391697
|
||||
REQ_STR = "\x00REQ"
|
||||
// \x00RES
|
||||
RES = 5391699
|
||||
RES_STR = "\x00RES"
|
||||
|
||||
// package data type
|
||||
CAN_DO = 1
|
||||
CANT_DO = 2
|
||||
RESET_ABILITIES = 3
|
||||
PRE_SLEEP = 4
|
||||
NOOP = 6
|
||||
JOB_CREATED = 8
|
||||
GRAB_JOB = 9
|
||||
NO_JOB = 10
|
||||
JOB_ASSIGN = 11
|
||||
WORK_STATUS = 12
|
||||
WORK_COMPLETE = 13
|
||||
WORK_FAIL = 14
|
||||
GET_STATUS = 15
|
||||
ECHO_REQ = 16
|
||||
ECHO_RES = 17
|
||||
ERROR = 19
|
||||
STATUS_RES = 20
|
||||
SET_CLIENT_ID = 22
|
||||
CAN_DO_TIMEOUT = 23
|
||||
WORK_EXCEPTION = 25
|
||||
WORK_DATA = 28
|
||||
WORK_WARNING = 29
|
||||
GRAB_JOB_UNIQ = 30
|
||||
JOB_ASSIGN_UNIQ = 31
|
||||
|
||||
SUBMIT_JOB = 7
|
||||
SUBMIT_JOB_BG = 18
|
||||
SUBMIT_JOB_HIGH = 21
|
||||
SUBMIT_JOB_HIGH_BG = 32
|
||||
SUBMIT_JOB_LOW = 33
|
||||
SUBMIT_JOB_LOW_BG = 34
|
||||
)
|
45
worker/error.go
Normal file
45
worker/error.go
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright 2011 - 2012 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 (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrJobTimeOut = errors.New("Do a job time out")
|
||||
ErrInvalidData = errors.New("Invalid data")
|
||||
ErrWorkWarning = errors.New("Work warning")
|
||||
ErrWorkFail = errors.New("Work fail")
|
||||
ErrWorkException = errors.New("Work exeption")
|
||||
ErrDataType = errors.New("Invalid data type")
|
||||
ErrOutOfCap = errors.New("Out of the capability")
|
||||
ErrNotConn = errors.New("Did not connect to job server")
|
||||
ErrFuncNotFound = errors.New("The function was not found")
|
||||
ErrConnection = errors.New("Connection error")
|
||||
ErrNoActiveAgent = errors.New("No active agent")
|
||||
ErrTimeOut = errors.New("Executing time out")
|
||||
ErrUnknown = errors.New("Unknown error")
|
||||
ErrConnClosed = errors.New("Connection closed")
|
||||
)
|
||||
|
||||
func DisablePanic() { recover() }
|
||||
|
||||
// Extract the error message
|
||||
func GetError(data []byte) (err error) {
|
||||
rel := bytes.SplitN(data, []byte{'\x00'}, 2)
|
||||
if len(rel) != 2 {
|
||||
err = fmt.Errorf("Not a error data: %V", data)
|
||||
return
|
||||
}
|
||||
err = errors.New(fmt.Sprintf("%s: %s", rel[0], rel[1]))
|
||||
return
|
||||
}
|
||||
|
||||
// An error handler
|
||||
type ErrorHandler func(error)
|
@ -1,8 +1,8 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"encoding/json"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type systemInfo struct {
|
||||
|
134
worker/job.go
134
worker/job.go
@ -1,134 +0,0 @@
|
||||
// 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 (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"github.com/mikespook/gearman-go/common"
|
||||
)
|
||||
|
||||
// Worker side job
|
||||
type Job struct {
|
||||
Data []byte
|
||||
Handle, UniqueId, Fn string
|
||||
agent *agent
|
||||
magicCode, DataType uint32
|
||||
c chan bool
|
||||
}
|
||||
|
||||
// Create a new job
|
||||
func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
|
||||
return &Job{magicCode: magiccode,
|
||||
DataType: datatype,
|
||||
Data: data,
|
||||
c: make(chan bool),}
|
||||
}
|
||||
|
||||
// Decode job from byte slice
|
||||
func decodeJob(data []byte) (job *Job, err error) {
|
||||
if len(data) < 12 {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
||||
l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
||||
if len(data[12:]) != int(l) {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
data = data[12:]
|
||||
job = &Job{magicCode: common.RES, DataType: datatype, c: make(chan bool),}
|
||||
switch datatype {
|
||||
case common.JOB_ASSIGN:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 3)
|
||||
if len(s) == 3 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
data = s[2]
|
||||
}
|
||||
case common.JOB_ASSIGN_UNIQ:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 4)
|
||||
if len(s) == 4 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
job.UniqueId = string(s[2])
|
||||
data = s[3]
|
||||
}
|
||||
}
|
||||
job.Data = data
|
||||
return
|
||||
}
|
||||
|
||||
// Encode a job to byte slice
|
||||
func (job *Job) Encode() (data []byte) {
|
||||
var l int
|
||||
if job.DataType == common.WORK_FAIL {
|
||||
l = len(job.Handle)
|
||||
} else {
|
||||
l = len(job.Data)
|
||||
if job.Handle != "" {
|
||||
l += len(job.Handle) + 1
|
||||
}
|
||||
}
|
||||
data = make([]byte, 0, l + 12)
|
||||
|
||||
magiccode := common.Uint32ToBytes(job.magicCode)
|
||||
datatype := common.Uint32ToBytes(job.DataType)
|
||||
datalength := common.Uint32ToBytes(uint32(l))
|
||||
|
||||
data = append(data, magiccode[:]...)
|
||||
data = append(data, datatype[:]...)
|
||||
data = append(data, datalength[:]...)
|
||||
if job.Handle != "" {
|
||||
data = append(data, []byte(job.Handle)...)
|
||||
if job.DataType != common.WORK_FAIL {
|
||||
data = append(data, 0)
|
||||
}
|
||||
}
|
||||
data = append(data, job.Data...)
|
||||
return
|
||||
}
|
||||
|
||||
// Send some datas to client.
|
||||
// Using this in a job's executing.
|
||||
func (job *Job) UpdateData(data []byte, iswarning bool) {
|
||||
result := append([]byte(job.Handle), 0)
|
||||
result = append(result, data...)
|
||||
var datatype uint32
|
||||
if iswarning {
|
||||
datatype = common.WORK_WARNING
|
||||
} else {
|
||||
datatype = common.WORK_DATA
|
||||
}
|
||||
job.agent.WriteJob(newJob(common.REQ, datatype, result))
|
||||
}
|
||||
|
||||
// Update status.
|
||||
// Tall client how many percent job has been executed.
|
||||
func (job *Job) UpdateStatus(numerator, denominator int) {
|
||||
n := []byte(strconv.Itoa(numerator))
|
||||
d := []byte(strconv.Itoa(denominator))
|
||||
result := append([]byte(job.Handle), '\x00')
|
||||
result = append(result, n...)
|
||||
result = append(result, '\x00')
|
||||
result = append(result, d...)
|
||||
job.agent.WriteJob(newJob(common.REQ, common.WORK_STATUS, result))
|
||||
}
|
||||
|
||||
// close the job
|
||||
func (job *Job) Close() {
|
||||
close(job.c)
|
||||
}
|
||||
|
||||
// cancel the job executing
|
||||
func (job *Job) cancel() {
|
||||
defer func() {recover()}()
|
||||
job.c <- true
|
||||
}
|
||||
|
||||
// When a job was canceled, return a true form a channel
|
||||
func (job *Job) Canceled() <-chan bool {
|
||||
return job.c
|
||||
}
|
133
worker/request.go
Normal file
133
worker/request.go
Normal file
@ -0,0 +1,133 @@
|
||||
// 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 (
|
||||
"bytes"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Worker side job
|
||||
type Job struct {
|
||||
Data []byte
|
||||
Handle, UniqueId, Fn string
|
||||
agent *agent
|
||||
magicCode, DataType uint32
|
||||
c chan bool
|
||||
}
|
||||
|
||||
// Create a new job
|
||||
func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
|
||||
return &Job{magicCode: magiccode,
|
||||
DataType: datatype,
|
||||
Data: data,
|
||||
c: make(chan bool)}
|
||||
}
|
||||
|
||||
// Decode job from byte slice
|
||||
func decodeJob(data []byte) (job *Job, err error) {
|
||||
if len(data) < 12 {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
||||
l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
||||
if len(data[12:]) != int(l) {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
data = data[12:]
|
||||
job = &Job{magicCode: common.RES, DataType: datatype, c: make(chan bool)}
|
||||
switch datatype {
|
||||
case common.JOB_ASSIGN:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 3)
|
||||
if len(s) == 3 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
data = s[2]
|
||||
}
|
||||
case common.JOB_ASSIGN_UNIQ:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 4)
|
||||
if len(s) == 4 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
job.UniqueId = string(s[2])
|
||||
data = s[3]
|
||||
}
|
||||
}
|
||||
job.Data = data
|
||||
return
|
||||
}
|
||||
|
||||
// Encode a job to byte slice
|
||||
func (job *Job) Encode() (data []byte) {
|
||||
var l int
|
||||
if job.DataType == common.WORK_FAIL {
|
||||
l = len(job.Handle)
|
||||
} else {
|
||||
l = len(job.Data)
|
||||
if job.Handle != "" {
|
||||
l += len(job.Handle) + 1
|
||||
}
|
||||
}
|
||||
data = make([]byte, 0, l+12)
|
||||
|
||||
magiccode := common.Uint32ToBytes(job.magicCode)
|
||||
datatype := common.Uint32ToBytes(job.DataType)
|
||||
datalength := common.Uint32ToBytes(uint32(l))
|
||||
|
||||
data = append(data, magiccode[:]...)
|
||||
data = append(data, datatype[:]...)
|
||||
data = append(data, datalength[:]...)
|
||||
if job.Handle != "" {
|
||||
data = append(data, []byte(job.Handle)...)
|
||||
if job.DataType != common.WORK_FAIL {
|
||||
data = append(data, 0)
|
||||
}
|
||||
}
|
||||
data = append(data, job.Data...)
|
||||
return
|
||||
}
|
||||
|
||||
// Send some datas to client.
|
||||
// Using this in a job's executing.
|
||||
func (job *Job) UpdateData(data []byte, iswarning bool) {
|
||||
result := append([]byte(job.Handle), 0)
|
||||
result = append(result, data...)
|
||||
var datatype uint32
|
||||
if iswarning {
|
||||
datatype = common.WORK_WARNING
|
||||
} else {
|
||||
datatype = common.WORK_DATA
|
||||
}
|
||||
job.agent.WriteJob(newJob(common.REQ, datatype, result))
|
||||
}
|
||||
|
||||
// Update status.
|
||||
// Tall client how many percent job has been executed.
|
||||
func (job *Job) UpdateStatus(numerator, denominator int) {
|
||||
n := []byte(strconv.Itoa(numerator))
|
||||
d := []byte(strconv.Itoa(denominator))
|
||||
result := append([]byte(job.Handle), '\x00')
|
||||
result = append(result, n...)
|
||||
result = append(result, '\x00')
|
||||
result = append(result, d...)
|
||||
job.agent.WriteJob(newJob(common.REQ, common.WORK_STATUS, result))
|
||||
}
|
||||
|
||||
// close the job
|
||||
func (job *Job) Close() {
|
||||
close(job.c)
|
||||
}
|
||||
|
||||
// cancel the job executing
|
||||
func (job *Job) cancel() {
|
||||
defer func() { recover() }()
|
||||
job.c <- true
|
||||
}
|
||||
|
||||
// When a job was canceled, return a true form a channel
|
||||
func (job *Job) Canceled() <-chan bool {
|
||||
return job.c
|
||||
}
|
133
worker/response.go
Normal file
133
worker/response.go
Normal file
@ -0,0 +1,133 @@
|
||||
// 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 (
|
||||
"bytes"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Worker side job
|
||||
type Job struct {
|
||||
Data []byte
|
||||
Handle, UniqueId, Fn string
|
||||
agent *agent
|
||||
magicCode, DataType uint32
|
||||
c chan bool
|
||||
}
|
||||
|
||||
// Create a new job
|
||||
func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
|
||||
return &Job{magicCode: magiccode,
|
||||
DataType: datatype,
|
||||
Data: data,
|
||||
c: make(chan bool)}
|
||||
}
|
||||
|
||||
// Decode job from byte slice
|
||||
func decodeJob(data []byte) (job *Job, err error) {
|
||||
if len(data) < 12 {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
||||
l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
||||
if len(data[12:]) != int(l) {
|
||||
return nil, common.Errorf("Invalid data: %V", data)
|
||||
}
|
||||
data = data[12:]
|
||||
job = &Job{magicCode: common.RES, DataType: datatype, c: make(chan bool)}
|
||||
switch datatype {
|
||||
case common.JOB_ASSIGN:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 3)
|
||||
if len(s) == 3 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
data = s[2]
|
||||
}
|
||||
case common.JOB_ASSIGN_UNIQ:
|
||||
s := bytes.SplitN(data, []byte{'\x00'}, 4)
|
||||
if len(s) == 4 {
|
||||
job.Handle = string(s[0])
|
||||
job.Fn = string(s[1])
|
||||
job.UniqueId = string(s[2])
|
||||
data = s[3]
|
||||
}
|
||||
}
|
||||
job.Data = data
|
||||
return
|
||||
}
|
||||
|
||||
// Encode a job to byte slice
|
||||
func (job *Job) Encode() (data []byte) {
|
||||
var l int
|
||||
if job.DataType == common.WORK_FAIL {
|
||||
l = len(job.Handle)
|
||||
} else {
|
||||
l = len(job.Data)
|
||||
if job.Handle != "" {
|
||||
l += len(job.Handle) + 1
|
||||
}
|
||||
}
|
||||
data = make([]byte, 0, l+12)
|
||||
|
||||
magiccode := common.Uint32ToBytes(job.magicCode)
|
||||
datatype := common.Uint32ToBytes(job.DataType)
|
||||
datalength := common.Uint32ToBytes(uint32(l))
|
||||
|
||||
data = append(data, magiccode[:]...)
|
||||
data = append(data, datatype[:]...)
|
||||
data = append(data, datalength[:]...)
|
||||
if job.Handle != "" {
|
||||
data = append(data, []byte(job.Handle)...)
|
||||
if job.DataType != common.WORK_FAIL {
|
||||
data = append(data, 0)
|
||||
}
|
||||
}
|
||||
data = append(data, job.Data...)
|
||||
return
|
||||
}
|
||||
|
||||
// Send some datas to client.
|
||||
// Using this in a job's executing.
|
||||
func (job *Job) UpdateData(data []byte, iswarning bool) {
|
||||
result := append([]byte(job.Handle), 0)
|
||||
result = append(result, data...)
|
||||
var datatype uint32
|
||||
if iswarning {
|
||||
datatype = common.WORK_WARNING
|
||||
} else {
|
||||
datatype = common.WORK_DATA
|
||||
}
|
||||
job.agent.WriteJob(newJob(common.REQ, datatype, result))
|
||||
}
|
||||
|
||||
// Update status.
|
||||
// Tall client how many percent job has been executed.
|
||||
func (job *Job) UpdateStatus(numerator, denominator int) {
|
||||
n := []byte(strconv.Itoa(numerator))
|
||||
d := []byte(strconv.Itoa(denominator))
|
||||
result := append([]byte(job.Handle), '\x00')
|
||||
result = append(result, n...)
|
||||
result = append(result, '\x00')
|
||||
result = append(result, d...)
|
||||
job.agent.WriteJob(newJob(common.REQ, common.WORK_STATUS, result))
|
||||
}
|
||||
|
||||
// close the job
|
||||
func (job *Job) Close() {
|
||||
close(job.c)
|
||||
}
|
||||
|
||||
// cancel the job executing
|
||||
func (job *Job) cancel() {
|
||||
defer func() { recover() }()
|
||||
job.c <- true
|
||||
}
|
||||
|
||||
// When a job was canceled, return a true form a channel
|
||||
func (job *Job) Canceled() <-chan bool {
|
||||
return job.c
|
||||
}
|
@ -6,7 +6,6 @@ package worker
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/mikespook/gearman-go/common"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -19,6 +18,7 @@ const (
|
||||
var (
|
||||
ErrConnection = common.ErrConnection
|
||||
)
|
||||
|
||||
// Job handler
|
||||
type JobHandler func(*Job) error
|
||||
|
||||
@ -78,7 +78,7 @@ func New(l int) (worker *Worker) {
|
||||
}
|
||||
|
||||
//
|
||||
func (worker *Worker)err(e error) {
|
||||
func (worker *Worker) err(e error) {
|
||||
if worker.ErrHandler != nil {
|
||||
worker.ErrHandler(e)
|
||||
}
|
||||
@ -109,7 +109,7 @@ func (worker *Worker) broadcast(job *Job) {
|
||||
// Plz added job servers first, then functions.
|
||||
// The API will tell every connected job server that 'I can do this'
|
||||
func (worker *Worker) AddFunc(funcname string,
|
||||
f JobFunc, timeout uint32) (err error) {
|
||||
f JobFunc, timeout uint32) (err error) {
|
||||
if _, ok := worker.funcs[funcname]; ok {
|
||||
return common.Errorf("The function already exists: %s", funcname)
|
||||
}
|
||||
@ -250,7 +250,7 @@ func (worker *Worker) exec(job *Job) (err error) {
|
||||
err = common.ErrUnknown
|
||||
}
|
||||
}
|
||||
} ()
|
||||
}()
|
||||
f, ok := worker.funcs[job.Fn]
|
||||
if !ok {
|
||||
return common.Errorf("The function does not exist: %s", job.Fn)
|
||||
@ -258,9 +258,9 @@ func (worker *Worker) exec(job *Job) (err error) {
|
||||
var r *result
|
||||
if f.timeout == 0 {
|
||||
d, e := f.f(job)
|
||||
r = &result{data:d, err: e}
|
||||
r = &result{data: d, err: e}
|
||||
} else {
|
||||
r = execTimeout(f.f, job, time.Duration(f.timeout) * time.Second)
|
||||
r = execTimeout(f.f, job, time.Duration(f.timeout)*time.Second)
|
||||
}
|
||||
var datatype uint32
|
||||
if r.err == nil {
|
||||
@ -286,7 +286,7 @@ func (worker *Worker) exec(job *Job) (err error) {
|
||||
func (worker *Worker) removeAgent(a *agent) {
|
||||
for k, v := range worker.agents {
|
||||
if v == a {
|
||||
worker.agents = append(worker.agents[:k], worker.agents[k + 1:] ...)
|
||||
worker.agents = append(worker.agents[:k], worker.agents[k+1:]...)
|
||||
}
|
||||
}
|
||||
if len(worker.agents) == 0 {
|
||||
@ -303,7 +303,7 @@ func execTimeout(f JobFunc, job *Job, timeout time.Duration) (r *result) {
|
||||
rslt := make(chan *result)
|
||||
defer close(rslt)
|
||||
go func() {
|
||||
defer func() {recover()}()
|
||||
defer func() { recover() }()
|
||||
d, e := f(job)
|
||||
rslt <- &result{data: d, err: e}
|
||||
}()
|
||||
@ -311,7 +311,7 @@ func execTimeout(f JobFunc, job *Job, timeout time.Duration) (r *result) {
|
||||
case r = <-rslt:
|
||||
case <-time.After(timeout):
|
||||
go job.cancel()
|
||||
return &result{err:common.ErrTimeOut}
|
||||
return &result{err: common.ErrTimeOut}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user