refactoring complated

This commit is contained in:
Xing Xing 2013-12-24 14:35:33 +08:00
parent 2ee2be0891
commit 2da5f29cd1
4 changed files with 69 additions and 70 deletions

View File

@ -1,12 +1,12 @@
package main
import (
"os"
"log"
"time"
"strings"
"github.com/mikespook/golib/signal"
"github.com/mikespook/gearman-go/worker"
"github.com/mikespook/golib/signal"
"log"
"os"
"strings"
"time"
)
func ToUpper(job worker.Job) ([]byte, error) {
@ -49,8 +49,12 @@ func main() {
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
if err := w.Ready(); err != nil {
log.Fatal(err)
return
}
go w.Work()
sh := signal.NewHandler()
sh.Bind(os.Interrupt, func() bool {return true})
sh.Bind(os.Interrupt, func() bool { return true })
sh.Loop()
}

View File

@ -15,7 +15,6 @@ type agent struct {
worker *Worker
in chan []byte
net, addr string
isConn bool
}
// Create the agent of job server.
@ -34,16 +33,16 @@ func (a *agent) Connect() (err error) {
if err != nil {
return
}
a.isConn = true
go a.work()
return
}
func (a *agent) Work() {
func (a *agent) work() {
var inpack *inPack
var l int
var err error
var data, leftdata []byte
for a.isConn {
for {
if data, err = a.read(BUFFER_SIZE); err != nil {
if err == ErrConnClosed {
break
@ -72,9 +71,7 @@ func (a *agent) Work() {
}
func (a *agent) Close() {
if a.conn != nil {
a.conn.Close()
}
}
func (a *agent) Grab() {
@ -96,13 +93,11 @@ func (a *agent) read(length int) (data []byte, err error) {
// read until data can be unpacked
for i := length; i > 0 || len(data) < MIN_PACKET_LEN; i -= n {
if n, err = a.conn.Read(buf); err != nil {
if !a.isConn {
err = ErrConnClosed
return
}
if err == io.EOF && n == 0 {
if data == nil {
err = ErrConnection
} else {
err = ErrConnClosed
}
}
return

View File

@ -6,6 +6,7 @@
package worker
import (
// "fmt"
"encoding/binary"
)
@ -43,8 +44,10 @@ func (outpack *outPack) Encode() (data []byte) {
if outpack.dataType != WORK_FAIL {
data[hi] = '\x00'
}
i = i + hi
i = hi + 1
}
if outpack.dataType != WORK_FAIL {
copy(data[i:], outpack.data)
}
return
}

View File

@ -174,9 +174,7 @@ func (worker *Worker) Ready() (err error) {
if err = v.Connect(); err != nil {
return
}
go v.Work()
}
worker.Reset()
for funcname, f := range worker.funcs {
worker.addFunc(funcname, f.timeout)
}
@ -185,12 +183,10 @@ func (worker *Worker) Ready() (err error) {
// Main loop
func (worker *Worker) Work() {
defer func() {
for _, v := range worker.agents {
v.Close()
}
}()
worker.running = true
for _, v := range worker.agents {
v.Grab()
}
var inpack *inPack
for inpack = range worker.in {
go worker.handleInPack(inpack)
@ -263,19 +259,20 @@ func (worker *Worker) exec(inpack *inPack) (err error) {
} else {
r = execTimeout(f.f, inpack, time.Duration(f.timeout)*time.Second)
}
if worker.running {
outpack := getOutPack()
if r.err == nil {
outpack.dataType = WORK_COMPLETE
} else {
if r.data == nil {
if len(r.data) == 0 {
outpack.dataType = WORK_FAIL
} else {
outpack.dataType = WORK_EXCEPTION
}
err = r.err
}
outpack.handle = inpack.handle
outpack.data = r.data
if worker.running {
inpack.a.write(outpack)
inpack.a.Grab()
}