From b8ec3fd24242c3e378f4e42f7b083ee8291a06a4 Mon Sep 17 00:00:00 2001 From: mikespook Date: Tue, 27 Mar 2012 22:48:03 +0800 Subject: [PATCH] Modified the api, added the interrupt for the example. --- example/client.go | 2 +- example/worker.go | 11 +++++++++-- gearman/client/client.go | 2 +- gearman/worker/worker.go | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/example/client.go b/example/client.go index 7ead4c5..ec8bd94 100644 --- a/example/client.go +++ b/example/client.go @@ -7,7 +7,7 @@ import ( ) func main() { - client := client.NewClient() + client := client.New() defer client.Close() if err := client.AddServer("127.0.0.1:4730"); err != nil { log.Fatalln(err) diff --git a/example/worker.go b/example/worker.go index 4ddd377..c8b3b5e 100644 --- a/example/worker.go +++ b/example/worker.go @@ -3,6 +3,7 @@ package main import ( "bitbucket.org/mikespook/gearman-go/gearman" "bitbucket.org/mikespook/gearman-go/gearman/worker" + "bitbucket.org/mikespook/golib/util" "fmt" "log" "strings" @@ -14,12 +15,18 @@ func ToUpper(job *worker.WorkerJob) ([]byte, error) { } func main() { - w := worker.NewWorker() - defer w.Close() + w := worker.New() w.AddServer("127.0.0.1:4730") w.AddFunction("ToUpper", ToUpper, 0) w.AddFunction("ToUpperTimeOut5", ToUpper, 5) + // Catch the interrupt to exit the working loop. + sh := util.NewSignalHandler(func() bool { + w.Close() + return true + }, func() bool {return true}) + go sh.Loop() + go func() { log.Println("start worker") for { diff --git a/gearman/client/client.go b/gearman/client/client.go index a5808fc..2ed069a 100644 --- a/gearman/client/client.go +++ b/gearman/client/client.go @@ -31,7 +31,7 @@ type Client struct { } // Create a new client. -func NewClient() (client *Client) { +func New() (client *Client) { client = &Client{JobQueue: make(chan *ClientJob, gearman.QUEUE_CAP), incoming: make(chan []byte, gearman.QUEUE_CAP), UId: 1} diff --git a/gearman/worker/worker.go b/gearman/worker/worker.go index 9e23c2c..40f29e6 100644 --- a/gearman/worker/worker.go +++ b/gearman/worker/worker.go @@ -46,7 +46,7 @@ type Worker struct { } // Get a new worker -func NewWorker() (worker *Worker) { +func New() (worker *Worker) { worker = &Worker{ // job server list clients: make([]*jobAgent, 0, gearman.WORKER_SERVER_CAP),